如何从C#中gridview页脚中的控件中获取值?
下面问题的解决方案,似乎对我不起作用 这是我的表格如何从C#中gridview页脚中的控件中获取值?,c#,asp.net,gridview,textbox,C#,Asp.net,Gridview,Textbox,下面问题的解决方案,似乎对我不起作用 这是我的表格 <asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false" OnRowCommand="gridOccupants_RowCommand"> <asp:TemplateField HeaderText="Name" ItemStyle-Width="15
<asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false"
OnRowCommand="gridOccupants_RowCommand">
<asp:TemplateField HeaderText="Name" ItemStyle-Width="15px" FooterStyle-Width="15px">
<asp:TemplateField HeaderText="Id" ItemStyle-Width="15px" FooterStyle-Width="15px">
<itemtemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label>
</itemtemplate>
<footertemplate>
<asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" OnClick="InsertOccupant_Click"
UseSubmitBehavior="False" />
</footertemplate>
</asp:TemplateField>
<asp:TemplateField>
<itemtemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label>
</itemtemplate>
<footertemplate>
<asp:TextBox ID="txtname2" runat="server" Width="50px" > </asp:TextBox>
</footertemplate>
</asp:TemplateField>
</asp:GridView>
protected void InsertOccupant_Click(object sender, EventArgs e)
{
GridViewRow row = gridOccupants.FooterRow;
string name = ((TextBox)row.FindControl("txtname2")).Text;
}
受保护的void InsertOccupant\u单击(对象发送方,事件参数e)
{
GridViewRow行=GridViewRow.FooterRow;
字符串名称=((TextBox)row.FindControl(“txtname2”).Text;
}
此代码不会调出用户在文本框-txtname2中输入的值
欢迎任何想法。这可能是因为您正在处理按钮的
onclick
事件,而不是像链接到的示例那样处理row命令
请尝试以下方法:
<asp:GridView ID="gridOccupants" runat="server" AllowPaging="True" AutoGenerateColumns="false"
OnRowCommand="gridOccupants_RowCommand">
<asp:TemplateField HeaderText="Name" ItemStyle-Width="15px" FooterStyle-Width="15px">
<asp:TemplateField HeaderText="Id" ItemStyle-Width="15px" FooterStyle-Width="15px">
<itemtemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Bind("id")%>'></asp:Label>
</itemtemplate>
<footertemplate>
<asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" />
</footertemplate>
</asp:TemplateField>
<asp:TemplateField>
<itemtemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label>
</itemtemplate>
<footertemplate>
<asp:TextBox ID="txtname2" runat="server" Width="50px" > </asp:TextBox>
</footertemplate>
</asp:TemplateField>
</asp:GridView>
protected void gridOccupants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert", StringComparison.OrdinalIgnoreCase))
{
GridViewRow row = ((GridView)sender).FooterRow;
TextBox txtname2 = (TextBox)row.FindControl("txtname2");
if (txtname2 == null)
{
return;
}
string name = txtname2.Text;
}
}
受保护的void grid命令(对象发送方,GridViewCommandEventArgs e)
{
if(e.CommandName.Equals(“插入”,StringComparison.OrdinalIgnoreCase))
{
GridViewRow行=((GridView)发件人).FooterRow;
TextBox txtname2=(TextBox)row.FindControl(“txtname2”);
if(txtname2==null)
{
返回;
}
字符串名称=txtname2.Text;
}
}
在我的脑海中,我不确定使用submitbehavior=“False”
从哪里来。如果我的示例不起作用,那么您可能需要删除它。尝试以下方法:
protected void gridOccupants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
var txtFName1 = (TextBox)((GridView)sender).FooterRow.FindControl("txtname2");
if (txtname2 == null)
{
return;
}
string name = txtname2.Text;
}
}
代码的作用是什么?生成异常?返回名称的空字符串
?其他?这对我现在有效,我唯一的额外代码是:。谢谢。你能再解释一下你的解决方案吗?