如何在rad网格中访问(设置、获取或绑定)控件

本文关键字:获取 绑定 控件 设置 rad 网格 访问 | 更新日期: 2023-09-27 18:23:57

这是我第一次使用RadGrid


 <telerik:radgrid id="gv_about" runat="server" cssclass="RadGrid" allowpaging="True"
            pagesize="20" allowsorting="True" autogeneratecolumns="False" showstatusbar="True"
            allowautomaticdeletes="True" allowautomaticinserts="True" allowautomaticupdates="True"
            datasourceid="ObjectDataSource1" onitemdeleted="gv_about_ItemDeleted" oniteminserted="gv_about_ItemInserted"
            onitemupdated="gv_about_ItemUpdated" onitemcommand="gv_about_ItemCommand" onprerender="gv_about_PreRender">
        <MasterTableView CommandItemDisplay="TopAndBottom" DataSourceID="ObjectDataSource1">
            <commanditemsettings addnewrecordtext="&#1575;&#1590;&#1575;&#1601;&#1577;" showrefreshbutton="False" />
            <Columns>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn UniqueName="emp_num" HeaderText="&#1585;&#1602;&#1605; &#1575;&#1604;&#1605;&#1581;&#1575;&#1590;&#1585;" DataField="emp_num">
                    <HeaderStyle ForeColor="Silver" Width="25px"></HeaderStyle>
                    <ItemStyle ForeColor="Gray"></ItemStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="name" HeaderText="&#1575;&#1604;&#1575;&#1587;&#1605;" DataField="name">
                    <HeaderStyle Width="220px"></HeaderStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="degree_name" HeaderText="&#1575;&#1604;&#1583;&#1585;&#1580;&#1577; &#1575;&#1604;&#1593;&#1604;&#1605;&#1610;&#1577;" DataField="degree_name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="college" HeaderText="&#1575;&#1604;&#1603;&#1604;&#1610;&#1577; &#1608;&#1575;&#1604;&#1580;&#1575;&#1605;&#1593;&#1577;" DataField="college">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn UniqueName="type" HeaderText="&#1606;&#1608;&#1593; &#1575;&#1604;&#1605;&#1581;&#1575;&#1590;&#1585;" DataField="type">
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn CommandName="Delete" Text="&#1581;&#1584;&#1601;" UniqueName="column">
                </telerik:GridButtonColumn>
            </Columns>
            <EditFormSettings EditFormType="Template">
<EditColumn UniqueName="EditCommandColumn1" FilterControlAltText="Filter EditCommandColumn1 column"></EditColumn>
                <FormTemplate>
                    <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none"
                        style="border-collapse: collapse;">
                        <tr class="EditFormHeader">
                            <td colspan="2" style="font-size: small">
                                <b>&#1575;&#1587;&#1578;&#1605;&#1575;&#1585;&#1577; &#1578;&#1593;&#1575;&#1585;&#1601;</b>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table id="Table3" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
                                    <tr>
                                        <td>
                                        </td>
                                        <td>
                                        </td>
                                     </tr>
                                    <tr>
                                        <td>
                                            &#1606;&#1608;&#1593; &#1575;&#1604;&#1605;&#1581;&#1575;&#1590;&#1585;:
                                        </td>
                                        <td>
                                        <div id="div_lect_type">
        <asp:RadioButtonList ID="rbt_lect_type" runat="server" AppendDataBoundItems="true"
            AutoPostBack="true" CausesValidation="false" RepeatDirection="Horizontal" OnSelectedIndexChanged="rbt_lect_type_SelectedIndexChanged">
            <asp:ListItem Value="1" Text="&#1605;&#1593;&#1610;&#1606;" Selected="True"></asp:ListItem>
            <asp:ListItem Value="2" Text="&#1605;&#1606;&#1578;&#1583;&#1576; &#1582;&#1575;&#1585;&#1580;&#1610;"></asp:ListItem>
            <asp:ListItem Value="3" Text="&#1580;&#1583;&#1610;&#1583;"></asp:ListItem>
        </asp:RadioButtonList>
    </div>
    <div id="div_lect">
        <asp:TextBox ID="txt_lect_in" runat="server" AutoPostBack="True" OnTextChanged="txt_lect_in_TextChanged"></asp:TextBox>
        <asp:AutoCompleteExtender ID="txt_lect_in_AutoCompleteExtender" runat="server" DelimiterCharacters=""
            Enabled="True" MinimumPrefixLength="4" ServiceMethod="Get_Emp_AutoComplete" ServicePath="~/LectIn.asmx"
            TargetControlID="txt_lect_in" BehaviorID="ACE_txt_lect_in" CompletionListCssClass="autocomplete_completionListElement"
            CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" CompletionListItemCssClass="autocomplete_listItem"
            EnableCaching="False">
        </asp:AutoCompleteExtender>
    </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            City:
                                        </td>
                                        <td>
                                           <%-- <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("City") %>' TabIndex="1">
                                            </asp:TextBox>--%>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Region:
                                        </td>
                                        <td>
                                           <%-- <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Region") %>' TabIndex="2">
                                            </asp:TextBox>--%>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Home Phone:
                                        </td>
                                        <td>
                                          <%--  <telerik:RadMaskedTextBox ID="HomePhoneBox" runat="server" SelectionOnFocus="SelectAll"
                                                Text='<%# Bind("HomePhone") %>' PromptChar="_" Width="300px" Mask="(###) ###-####"
                                                TabIndex="3">
                                            </telerik:RadMaskedTextBox>--%>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Birth Date:
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Title Of Courtesy
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td>
                                <table id="Table1" cellspacing="1" cellpadding="1" width="250" border="0" class="module">
                                    <tr>
                                        <td>
                                            Notes:
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Address:
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td align="right" colspan="2">
                                <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>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
        </ClientSettings>
    </telerik:radgrid>

主要问题是如何从代码背后访问这个网格中的控件,以及如何绑定其中的一些控件或处理其中的事件??

例如,如何从代码背后访问txt_lect_in

如何在rad网格中访问(设置、获取或绑定)控件

请尝试使用以下代码片段。

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
        GridEditFormItem item = e.Item as GridEditFormItem;
        TextBox txt_lect_in = item.FindControl("txt_lect_in") as TextBox;
        //Access your textbox heer
    }
}
//OR
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
        GridEditFormItem item = e.Item as GridEditFormItem;
        TextBox txt_lect_in = item.FindControl("txt_lect_in") as TextBox;
        //Access your textbox heer
    }
}