1

我有一个使用 EmptyDataTemplate 的 ASP.NET GridView。在 EmptyDataTemplate 内,我有一些文本框和一个用于插入记录的提交按钮。当我尝试访问代码隐藏文件中的 ID 时,它们没有显示出来。

我的代码:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" BackColor="White"
        BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black"
        GridLines="Vertical">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="BankName" HeaderText="Bank Name" />
            <asp:BoundField DataField="IFSCCode" HeaderText="IFSC Code" />
            <asp:BoundField DataField="MICRCODE" HeaderText="MICR Code" />
            <asp:BoundField DataField="BranchName" HeaderText="Branch Name" />
            <asp:BoundField DataField="Address" HeaderText="Address" />
        </Columns>
        <EmptyDataTemplate>
            <div style="border: 1px solid #ccc; width: auto; margin: 20px 470px;">
                <table border="0" cellpadding="2" cellspacing="0">
                    <tr>
                        <th colspan="3" bgcolor="#1e7c9a" style="color: White">
                            Please Enter Your Bank Details
                        </th>
                    </tr>
                    <tr>
                        <td>
                            Bank Name
                        </td>
                        <td>
                            <asp:TextBox ID="txtBankname" runat="server" Width="180px" />
                        </td>
                        <td>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="Required"
                                ForeColor="Red" ControlToValidate="txtBankname" runat="server" Width="120" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            IFSC Code
                        </td>
                        <td>
                            <asp:TextBox ID="txtIfsccode" runat="server" Width="180px" />
                        </td>
                        <td>
                           <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ErrorMessage="Required"
                                ForeColor="Red" ControlToValidate="txtIfsccode" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            MICR Code
                        </td>
                        <td>
                            <asp:TextBox ID="txtMicrcode" runat="server" Width="180px" />
                        </td>
                        <td>
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Branch Name
                        </td>
                        <td>
                            <asp:TextBox ID="txtBranchName" runat="server" Width="180px" />
                        </td>
                        <td>
                           <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ErrorMessage="Required"
                                Display="Dynamic" ForeColor="Red" ControlToValidate="txtBranchName" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Address
                        </td>
                        <td>
                            <asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" />
                        </td>
                        <td>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" ErrorMessage="Required"
                                ForeColor="Red" ControlToValidate="txtAddress" runat="server" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                        </td>
                        <td>
                            <asp:Button ID="btn" Text="Submit" runat="server" OnClick="InsertDetails"/>
                        </td>
                        <td>
                        </td>
                    </tr>
                </table>
            </div>
        </EmptyDataTemplate>
        <FooterStyle BackColor="#CCCC99" />
        <HeaderStyle BackColor="#1e7c9a" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FBFBF2" />
        <SortedAscendingHeaderStyle BackColor="#848384" />
        <SortedDescendingCellStyle BackColor="#EAEAD3" />
        <SortedDescendingHeaderStyle BackColor="#575357" />
    </asp:GridView>
4

2 回答 2

2

尝试这个

Button Insert_Details = gv.Controls[0].Controls[0].FindControl("btn") as Button ;

你可以看到这个示例

于 2014-06-10T08:17:02.147 回答
0

OP的解决方案。

完成如下:

TextBox BankName = gv.Controls[0].Controls[0].FindControl("txtBankname") as TextBox;
cmd.Parameters.AddWithValue("@BankName", BankName.Text );
TextBox IFSCcode = gv.Controls[0].Controls[0].FindControl("txtIfsccode") as TextBox;
cmd.Parameters.AddWithValue("@IFSCcode", IFSCcode.Text);
TextBox MICROCODE = gv.Controls[0].Controls[0].FindControl("txtMicrcode") as TextBox;
cmd.Parameters.AddWithValue("@MICROCODE",MICROCODE.Text);
TextBox BranchName = gv.Controls[0].Controls[0].FindControl("txtBranchName") as     TextBox;
cmd.Parameters.AddWithValue("@BranchName",BranchName.Text);
TextBox BankAddress = gv.Controls[0].Controls[0].FindControl("txtAddress") as TextBox;
 cmd.Parameters.AddWithValue("@BankAddress", BankAddress.Text);
于 2017-10-06T02:29:21.187 回答