【问题标题】:Get the value of Buttonfield in ASP.NET获取 ASP.NET 中 Buttonfield 的值
【发布时间】:2014-09-26 19:35:44
【问题描述】:

假设我的 GridView (ID & Name) 中只有两列,我需要在单击时获取 ID 的值。例如,如果我点击 123,我应该得到 123。但是在这里...情况并非如此...当我单击任何 ID 时,它会在该 ID 右侧返回名称(例如;如果是 123,我会得到 Tony Stark),但是当我点击时获取 ID 的值,它总是返回一个空字符串。

换句话说,我在 id 中得到了正确的名称(当 X=1 时),但是当 X=0 时,id 变成一个空字符串... (int X and string id : see C#代码如下)

我的 GridView 中的 XAML 代码:

                        <asp:ButtonField
                            DataTextField="ID"
                            HeaderText="ID">
                        </asp:ButtonField>

                        <asp:BoundField
                            DataField="Name"
                            HeaderText="NAME">
                        </asp:BoundField>

C#:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[X].Text.ToString();
        Response.Write(id);
    }

怎么了?!!以及如何解决这个问题?谢谢!

【问题讨论】:

  • X 应该是什么?
  • X 是单元格的索引(参见 C# 代码)。 Name 列为 1,ID 列应为 0,但事实并非如此。 0 总是给我一个空字符串。
  • 一般来说,对于 .net 网格,Cell 内部会有另一个控件。如果您在调试时检查 Cells[0].Controls 或 .Children,是否有这种情况?另外,你确定你有正确的行索引吗?
  • 是的,行是正确的,单元格也是。当我尝试 string id = EmployeeList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Controls.ToString(); 时得到这个输出"System.Web.UI.ControlCollection"
  • EmployeeList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Controls[0]的类型是什么

标签: c# asp.net gridview buttonfield


【解决方案1】:

更复杂但更好的解决方案是:

在 GridView 中,使用 DataKeyNames 标签和 RowCommand 事件,并将绑定值放在 DataKeyName 中,例如:

DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" 

在您的网格视图中使用模板字段(您不需要像下面的示例那样只使用图像按钮):

<asp:TemplateField HeaderText="Action">
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle HorizontalAlign="Left" Wrap="false" />
                                <ItemTemplate>
                                    <div class="GridViewOpc">
                                        <asp:ImageButton ID="buttonDelete" runat="server" Width="17" Height="17" AlternateText="Update"
                                            CausesValidation="False" Visible="false" CommandName="Delete" Enabled="true"
                                            ImageUrl="~/yourDeletepicture.gif"  />

                                        <asp:ImageButton ID="buttonUpdate" runat="server" AlternateText="Update" Width="17"
                                            Height="17" CausesValidation="False" Visible="false" CommandName="Update" ImageUrl="~/yourUpdatePicture.gif" />
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>

然后在 CodeBehind 上获取它:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

            Control ctl = e.CommandSource as Control;
            GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;

                if (e.CommandName == "Update")
                {

                    int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);
                }

               if (e.CommandName == "Delete")
                {

                    int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);

                }
    }

【讨论】:

    【解决方案2】:

    不使用templateField 也可以从ButtonField 获取值。 在您的ButtonField 中,您需要给它一个ButtonType

    XML:

    <asp:ButtonField ButtonType="Link" CommandName="PanelName" DataTextField="PANEL_NAME" HeaderText="Panel Name" runat="server" SortExpression="PANEL_NAME" text="PanleName"/>
    

    代码:

    if (e.CommandName == "PanelName")
    {
        string PanleName;
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = dgSearchResult.Rows[index];
        PanleName = ((LinkButton)selectedRow.Cells[1].Controls[0]).Text;
    }
    

    注意:无论您的单元格索引是什么,Cells[1] 表示索引为 1。但如果 GridView 中只有一个控件,则控件始终从 0 开始。

    【讨论】:

      【解决方案3】:

      在您的 RowCommand 处理程序中,我认为这就是您想要的:

      protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              int id = (int)(DataBinder.Eval(e.Row.DataItem, "ID"));
              Response.Write(id);
          }
      }
      

      我是从 vb 转换过来的,所以我可能遗漏了一些语法,但你明白了......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 1970-01-01
        相关资源
        最近更新 更多