This is a migrated thread and some comments may be shown as answers.

Find Control in FormTemplate

1 Answer 143 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert Bross
Top achievements
Rank 1
Robert Bross asked on 21 Jul 2010, 07:22 PM
I have a simple FormTemplate that I am trying to grab the values from to perform an insert/update. I am trying to grab a Listbox to create a comma delimited list of the values that it contains. When I try to access it I get a null pointer. However, I can successfully access other controls in the same FormTemplate. Anyone have any ideas?

The control I am trying to access is the lstSelectedUsers control.

ASPX page:

<EditFormSettings EditFormType="Template" InsertCaption="Create new User Group" CaptionDataField="group_name" CaptionFormatString="Editing Group: <b>{0}</b>" PopUpSettings-Width="610px">
                            <FormTemplate>
                                <div style="padding-left:5px">
                                    <table border="0" cellpadding="2" cellspacing="0">
                                        <tr>
                                            <td>
                                                Name:
                                            </td>
                                            <td>
                                                <asp:TextBox runat="server" ID="GroupName" Text="<%# Bind('group_name') %>"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="vertical-align: top">
                                                Description:
                                            </td>
                                            <td>
                                                <asp:TextBox runat="server" ID="Description" Text="<%# Bind('description') %>" Wrap="true"
                                                    Width="300px" Height="35px" TextMode="MultiLine"></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                Public:
                                            </td>
                                            <td>
                                                <asp:CheckBox ID="isPublic" runat="server" Checked='<%# (DataBinder.Eval(Container.DataItem,"is_public").ToString()!="0"?true:false) %>'
                                                    TabIndex="1" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                Users:
                                            </td>
                                            <td>
                                                <table cellpadding="0" cellspacing="0">
                                                    <tr class="LightBackground">
                                                        <td style="border: 1px solid gray;border-bottom:none;border-right:none;padding:3px"><b>Available</b></td>
                                                        <td style="border: 1px solid gray;border-bottom:none;border-left:none;padding:3px"><b>Selected</b></td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <telerik:RadListBox runat="server" ID="lstAvailableUsers" AutoPostBackOnTransfer="false" Sort="Ascending" ButtonSettings-VerticalAlign="Middle" SelectionMode="Multiple" AllowTransfer="true" Width="240px" Height="150px" TransferMode="Move" AllowTransferOnDoubleClick="true" TransferToID="lstSelectedUsers" DataSourceID="dsAvailableUsers" DataKeyField="user_id" DataTextField="display_name"></telerik:RadListBox>
                                                        </td>
                                                        <td>
                                                            <telerik:RadListBox runat="server" ID="lstSelectedUsers" AutoPostBackOnTransfer="false" Sort="Ascending" Width="240px" Height="150px" SelectionMode="Multiple" AllowTransferOnDoubleClick="true" TransferToID="lstAvailableUsers"  DataSourceID="dsSelectedUsers" DataKeyField="user_id" DataTextField="display_name"></telerik:RadListBox>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                            </td>
                                            <td>
                                                <br />
                                                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
                                                </asp:Button>&nbsp;
                                                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                    CommandName="Cancel"></asp:Button>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </FormTemplate>
                        </EditFormSettings>


************************ CodeBehind ************************************************************

protected void grdUserGroups_UpdateCommand(object source, GridCommandEventArgs e)
    {
        //Get the GridEditFormInsertItem of the RadGrid     
        GridEditFormItem item = (GridEditFormItem)e.Item;

        try
        {
            SqlConnection conn = Database.getConnection();

            String groupId = (item.KeyValues.Substring(11, item.KeyValues.Length - 13));

            string sql = "Update USER_GROUPS set group_name=@groupName, description=@description, is_public=@isPublic where group_id=@groupId";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@groupName", (item.FindControl("groupName") as TextBox).Text);
            cmd.Parameters.AddWithValue("@description", (item.FindControl("description") as TextBox).Text);
            cmd.Parameters.AddWithValue("@isPublic", (item.FindControl("isPublic") as CheckBox).Checked ? "1" : "0");
            cmd.Parameters.AddWithValue("@groupId", groupId);
            cmd.ExecuteNonQuery();

********* THIS IS THE LINE THAT FAILS - The other FindControls up above work fine **************************************
            RadListBox lstSel = item.FindControl("lstSelectedUsers") as RadListBox;
            StringBuilder userIds = new StringBuilder();
            foreach (RadListBoxItem lstItem in lstSel.Items)
            {
                if (userIds.Length > 0)
                    userIds.Append(",");

                userIds.Append(lstItem.Value);
            }

            cmd = new SqlCommand("INSERT INTO USER_GROUP_MEMBERS (user_id, group_id) SELECT user_id, @groupId FROM users WHERE user_id in (" + userIds + ")", conn);
            cmd.Parameters.AddWithValue("@groupId", groupId);
            cmd.ExecuteNonQuery();

            conn.Close();
        }
        catch (Exception ex)
        { HandleGridStatus(ex, "inserted"); }
    }

1 Answer, 1 is accepted

Sort by
0
Robert Bross
Top achievements
Rank 1
answered on 21 Jul 2010, 07:27 PM
Ok, Nevermind. As soon as I submitted this forum post it started working LOL.
Tags
Grid
Asked by
Robert Bross
Top achievements
Rank 1
Answers by
Robert Bross
Top achievements
Rank 1
Share this question
or