C# ASP GridView在单击按钮时获取行值
我正在做什么-在imagebutton上重置用户密码单击 到目前为止已经完成-添加了GridViewCommandEventHandler-它正确启动。使用来自的代码。我的e.CommandArgument得到一个空字符串(“”),它在运行时抛出一个错误(无法将“”解析为int) 我可以在调试器中看到在e中的其他位置存储了一个“rowIndex”属性(对于我的单击是正确的),我可以访问它吗?我认为MSDN的代码会起作用——我是否做了其他事情使这个错误发生,或者用其他方法来修复它?谢谢C# ASP GridView在单击按钮时获取行值,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在做什么-在imagebutton上重置用户密码单击 到目前为止已经完成-添加了GridViewCommandEventHandler-它正确启动。使用来自的代码。我的e.CommandArgument得到一个空字符串(“”),它在运行时抛出一个错误(无法将“”解析为int) 我可以在调试器中看到在e中的其他位置存储了一个“rowIndex”属性(对于我的单击是正确的),我可以访问它吗?我认为MSDN的代码会起作用——我是否做了其他事情使这个错误发生,或者用其他方法来修复它?谢谢 void
void resetpassword(Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField columns are used, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "resetpass")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection. Use the
// CommandSource property to access the GridView control.
GridView GridView1 = (GridView)e.CommandSource;
GridViewRow row = GridView1.Rows[index];
String usrname = row.FindControl("username").ToString();
aspx页面代码:
<asp:TemplateField HeaderText="Reset Password">
<ItemTemplate>
<asp:ImageButton ID="ibtnReset" runat="server" CausesValidation="false"
CommandName="resetpass" ImageUrl="~/Images/glyphicons_044_keys.png" Text="Button" />
</ItemTemplate>
<HeaderStyle Width="70px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
我认为您缺少
CommandArgument=''
为您的代码
<asp:ImageButton ID="ibtnReset" runat="server" CausesValidation="false"
CommandArgument='<%# Container.DataItemIndex %>'
CommandName="resetpass" ImageUrl="~/Images/glyphicons_044_keys.png"
Text="Button" />
供进一步阅读
ButtonField类自动填充CommandArgument
属性具有适当的索引值
传递CommandArgument
(假设要传递名为PK
的主键字段):
您还可以将RowIndex作为CommandArgument传递:
CommandArgument='<%# Container.DataItemIndex %>'
CommandArgument=''
ButtonField
类会自动使用适当的索引值填充CommandArgument
属性。对于其他命令按钮,必须手动设置命令按钮的CommandArgument
属性 感谢ButtonField/ImageButton上的注释-现在我知道为什么该部分不起作用了!使用了第一个选项,它工作得很好。@Volvox:ButtonField
的进一步阅读,我在更新中提供了MSDN链接。感谢您的帮助!我去看看。
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibtnReset"
Text="reset password"
CommandName="resetpass"
CommandArgument='<%# Eval("Pk") %>'
</ItemTemplate>
</asp:TemplateField>
WebControl wc = e.CommandSource as WebControl;
GridViewRow row = wc.NamingContainer as GridViewRow;
String usrname = ((TextBox)row.FindControl("username")).Text;
CommandArgument='<%# Container.DataItemIndex %>'