按钮字段在转换为模板字段时不执行
本文关键字:字段 执行 转换 按钮 | 更新日期: 2023-09-27 18:15:28
你好,我有这个按钮字段在一个网格视图执行以下功能。
但是如果我将这个按钮字段转换为模板字段,函数不会执行。
错误是得到{"Input string was not in a correct format."}.
<asp:ButtonField CommandName="Selectagreement" HeaderText="Select" ControlStyle-CssClass="btn btn-primary" Text="select">
<ControlStyle CssClass="btn btn-primary" />
</asp:ButtonField>
protected void gvAgreement_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = gvAgreement.Rows[Convert.ToInt32(e.CommandArgument)];
switch (commandName)
{
case "Selectagreement":
Label1.Text = row.Cells[2].Text;
break;
default: break;
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
<asp:TemplateField HeaderText="Select" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="Selectagreement" Text="select"></asp:LinkButton>
</ItemTemplate>
<ControlStyle CssClass="btn btn-primary" />
</asp:TemplateField>
@Hakunamatata在评论部分提到,<ItemTemplate>
下的链接按钮没有设置CommandArgument
然而,根据您的代码,如果您只是在触发链接按钮单击的相应行的行索引之后,那么您可以这样做。
protected void gvAgreement_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
switch (commandName)
{
case "Selectagreement":
Label1.Text = row.Cells[2].Text;
break;
default: break;
}
}
catch (Exception ex)
{
Response.Write("Error: " + ex.Message);
}
}
基本上我把
GridViewRow row = gvAgreement.Rows[Convert.ToInt32(e.CommandArgument)];
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);