问题描述
|
就像标题+如何处理按钮一样,单击GridView页脚中的哪个按钮也是?
文件.aspx看起来像这样
<asp:GridView ID=\"GridView1\" runat=\"server\"
AutoGenerateColumns=\"False\"
CellPadding=\"4\" DataKeyNames=\"id\" EnableModelValidation=\"True\"
ForeColor=\"#333333\" GridLines=\"None\"
onrowcancelingedit=\"GridView1_RowCancelingEdit1\"
onrowediting=\"GridView1_RowEditing1\"
onrowupdating=\"GridView1_RowUpdating1\" AllowPaging=\"True\"
onrowdeleting=\"GridView1_RowDeleting\"
onpageindexchanging=\"GridView1_PageIndexChanging\" Height=\"322px\"
ShowFooter=\"True\" onselectedindexchanged=\"GridView1_SelectedindexChanged\" >
<AlternatingRowStyle BackColor=\"White\" ForeColor=\"#284775\" />
<Columns>
<asp:CommandField ShowEditButton=\"True\" />
<asp:BoundField datafield=\"id\" HeaderText=\"ID\" Visible=\"False\" />
<asp:BoundField datafield=\"Name\" HeaderText=\"Name\" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID=\"txtName\" runat=\"server\" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField datafield=\"LastName\" HeaderText=\"LastName\" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID=\"txtLastName\" runat=\"server\" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField datafield=\"DriveLic\" HeaderText=\"DriveLicense\" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID=\"txtDriveLicense\" runat=\"server\" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField datafield=\"country\" HeaderText=\"Country\" />
<asp:TemplateField>
<FooterTemplate>
<asp:TextBox ID=\"txtWoj\" runat=\"server\" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton=\"True\" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID=\"ButtonOK\" Text=\"Confirm\" runat=\"server\" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
解决方法
在
GridView_RowCommand
事件中,您可以通过GridView1.FooterRow.FindControl
方法访问页脚控件。
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if (e.CommandName.Equals(\"Insert\",StringComparison.OrdinalIgnoreCase))
{
TextBox txtSomeNewValue = ((TextBox)GridView1.FooterRow.FindControl(\"txtSomeNewValue\"));
string theTextValue = txtSomeNewValue.Text;
}
}
更新:将代码包装在if块中,以检查commandname
是否符合您的期望。此事件还用于删除,编辑等,因此,如果不将其包装在其中,可能最终会运行该事件的代码。