当前位置: 首页 > 编程语言 > C#

ASPGridView在单击按钮时获取行值share

时间:2023-04-10 18:27:34 C#

ASPGridView在单击按钮时获取行值到目前为止完成-添加了GridViewCommandEventHandler-并且它正确触发。使用MSDN中的代码。我的e.CommandArgument得到一个空字符串(""),这会在运行时引发错误(无法将""解析为int)。我可以在调试器中看到在e的其他地方存储了一个“rowIndex”属性(对于我的点击是正确的),我可以访问它吗?我认为来自MSDN的代码工作正常-是否有其他方法可以解决此错误或有其他方法可以修复它?谢谢。voidresetpassword(Objectsender,GridViewCommandEventArgse){//如果使用了多个ButtonField列,则使用//CommandName属性来确定单击了哪个按钮。if(e.CommandName=="resetpass"){//将存储在CommandArgument//属性中的行索引转换为整数。intindex=Convert.ToInt32(e.CommandArgument);//从Rows集合中检索包含用户单击的按钮的行。使用//CommandSource属性访问GridView控件。GridViewGridView1=(GridView)e.CommandSource;GridViewRowrow=GridView1.Rows[index];Stringusrname=row.FindControl("用户名").ToString();aspx页面代码:活动添加代码:protectedvoidGridView1_RowCreated(objectsender,GridViewRowEventArgse){GridView1.RowCommand+=newGridViewCommandEventHandler(this.resetpassword);}传递CommandArgument(假设您要传递名为PK的主键字段):'或者通过ImageButton的NamingContainer获取对GridViewRow的引用:WebControlwc=e。CommandSource作为WebControl;GridViewRowrow=wc.NamingContainerasGridViewRow;Stringusrname=((TextBox)row.FindControl("username")).Text;您还可以将RowIndex作为CommandArgument传递:CommandArgument=''ButtonField类使用适当的索引值自动填充CommandArgument属性对于其他命令按钮,您必须手动设置命令按钮的CommandArgument属性。我认为您的代码缺少CommandArgument=''。'CommandName="resetpass"ImageUrl="~/Images/glyphicons_044_keys.png"Text="Button"/>这是关于SOASP.NETGridViewRowIndexAsCommandArgument的问题,供进一步阅读。ButtonField类自动使用适当的索引值填充CommandArgument属性。这是MSDN的来源。以上就是C#学习教程:ASPGridView在点击按钮时获取行值共享的全部内容,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: