【问题标题】:ASP GridView get row values on button clickASP GridView 在按钮单击时获取行值
【发布时间】:2012-05-14 06:43:54
【问题描述】:

我在做什么 - 在单击图像按钮时重置用户密码。

到目前为止已完成 - 添加了 GridViewCommandEventHandler - 它正在正确触发。使用来自MSDN 的代码。我的 e.CommandArgument 得到一个空字符串 (""),它在运行时抛出错误(无法将 "" 解析为 int)。

我可以在调试器中看到在 e 的其他地方存储了一个“rowIndex”属性(对于我的点击正确),我可以访问它吗?我认为 MSDN 的代码会起作用 - 我是否已经做了其他事情来使这个错误发生或用其他方法来解决它?谢谢。

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>

事件添加代码:

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        GridView1.RowCommand += new GridViewCommandEventHandler(this.resetpassword);
    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    要么传递CommandArgument(假设你想传递名为PK的主键字段):

     <asp:TemplateField>
        <ItemTemplate>                
          <asp:ImageButton runat="server" ID="ibtnReset"
            Text="reset password"
            CommandName="resetpass"
            CommandArgument='<%# Eval("Pk") %>'
        </ItemTemplate>
      </asp:TemplateField>
    

    或通过您的ImageButtonNamingContainer 获取GridViewRow 的引用:

    WebControl wc = e.CommandSource as WebControl;
    GridViewRow row = wc.NamingContainer as GridViewRow;
    String usrname = ((TextBox)row.FindControl("username")).Text;
    

    您也可以将 RowIndex 作为 CommandArgument 传递:

    CommandArgument='<%# Container.DataItemIndex %>'
    

    ButtonField 类自动使用适当的索引值填充CommandArgument 属性。对于其他命令按钮,您必须手动设置命令按钮的CommandArgument 属性。

    【讨论】:

    • 感谢关于 ButtonField/ImageButton 的注释 - 现在我知道为什么该部分不起作用了!选择第一个选项,效果很好。
    【解决方案2】:

    我认为你错过了CommandArgument='&lt;%# Container.DataItemIndex %&gt;'

    为您的代码。

    <asp:ImageButton ID="ibtnReset" runat="server" CausesValidation="false" 
            CommandArgument='<%# Container.DataItemIndex %>'
            CommandName="resetpass" ImageUrl="~/Images/glyphicons_044_keys.png" 
    Text="Button" />
    

    这是关于 SO ASP.NET GridView RowIndex As CommandArgument 的问题,供进一步阅读。

    ButtonField 类自动填充 CommandArgument 具有适当索引值的属性。

    Here is the MSDN source

    【讨论】:

    • @Volvox:为了进一步阅读ButtonField,我在更新中提供了 MSDN 链接。
    • 感谢您的帮助!我去看看。
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多