我有一个ASP。. NET GridView使用了一个EmptyDataTemplate。在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>
</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>
try this
Button Insert_Details = gv.Controls[0].Controls[0].FindControl("btn") as Button ;
你可以看到示例
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);