如何在后端代码中获取GridView列的值

4

如果我点击第二行的编辑图像按钮,如何从代码后台中的GridView获取AppId。

应用程序列表

Aspx代码:

<asp:BoundField HeaderText="AppId" DataField="AppID" />


<asp:TemplateField HeaderText="Actions" ControlStyle-Width="20px" ItemStyle-Width="130px">
                    <ItemTemplate>
                      <asp:ImageButton ID="imgMailCamp" runat="server" ImageUrl="~/Images/AppSetup/Mail.png"
                            Height="18px" ToolTip="Send Mail Campaign" CssClass="grdImageAlign"  CommandName="SendMail" OnClick="btnMailCamp_Click"    />
                        <asp:ImageButton ID="imgViewApp" runat="server" ImageUrl="~/Images/AppSetup/application-view-list-icon.png"
                            Height="18px" ToolTip="View Appplication" CssClass="grdImageAlign" CommandName="View" OnClick="btnView_Click" />
                        <asp:ImageButton ID="imgEditApp" runat="server" ImageUrl="~/Images/AppSetup/Action-edit-icon.png"
                            Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" CommandName="Edit" OnClick="btnEdit_Click"/>
                        <asp:ImageButton ID="imgDeleteApp" runat="server" ImageUrl="~/Images/AppSetup/Trash-can-icon.png"
                            Height="18px" ToolTip="Delete Application" CssClass="grdImageAlign" CommandName="Delete" OnClick="btnDelete_Click" />
                   </ItemTemplate>
                </asp:TemplateField>

C# 代码:

protected void btnEdit_Click(object sender, EventArgs e)
{
   // I need to get the current row appId, I use this appId in next page for sql query
  Response.Redirect("/Secured/EditApplication.aspx?AppID="+AppID);
}

你可以使用数据键或隐藏字段方法,只需查看此处http://weblogs.asp.net/aghausman/archive/2009/01/08/get-primary-key-on-row-command-gridview.aspx。 - user2031802
4个回答

2

试试这样做...不要定义按钮的点击事件...像这样定义按钮...

     <asp:ImageButton ID="imgEditApp" runat="server"
 ImageUrl="~/Images/AppSetup/Action-edit-icon.png" 
    Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" 
CommandName="Edit"/>

定义GridView的RowEditing事件,代码如下...
 protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
          Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[e.NewEditIndex].Cells[1].Text);
        }

编辑: 我认为您在定义RowEditingEvent方面遇到了问题......好的,您可以这样做......不需要更改任何内容,只需在单击事件中编写以下代码...

protected void btnEdit_Click(object sender, EventArgs e)
{
      ImageButton ib = sender as ImageButton;
        GridViewRow row = ib.NamingContainer as GridViewRow;
  Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[row.RowIndex].Cells[1].Text);
}

Edit 2

<asp:ImageButton ID="imgEditApp" runat="server"
 ImageUrl="~/Images/AppSetup/Action-edit-icon.png" 
    Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" 
CommandName="Edit" CommandArgument='<%#Eval("AppID") %>'/>

    protected void btnEdit_Click(object sender, EventArgs e)
    {
string appid= (sender as ImageButton).CommandArgument;
      Response.Redirect("/Secured/EditApplication.aspx?AppID="+appid
    }

如果我使用上面的代码,就会出现错误:“System.EventArgs”不包含“NewEditIndex”的定义,也没有接受类型为“System.EventArgs”的第一个参数的扩展方法“NewEditIndex”(您是否缺少使用指令或程序集引用?)。 - Vignesh
如果我将EventArgs更改为GridViewEditEventArgs,那么我就会收到这样的错误信息:“CS0123:没有重载“btnEdit_Click”与委托“System.Web.UI.ImageClickEventHandler”相匹配”。 - Vignesh
你不能这样做...你需要移除图像按钮的onclick事件...定义gridview的rowediting事件,展示你的gridview apsx代码..我所说的那一行是指你定义gridview id和其他属性的地方。 - Amit Singh
@Vignesh,请仔细查看我的答案,现在替换您的按钮代码,就像我在这里定义的一样...并定义行编辑...Gridview事件并编写代码,就像上面写的那样/...在那里编写代码... - Amit Singh
另一件事是,如果将AppId列移动到表中的第二个位置,那么在这种情况下,上述代码将获取错误的列值,对吗?为此,是否有任何方法可以使用列名称而不是数字来指定索引? - Vignesh
@Vignesh 看一下另一个编辑...像我定义和编写代码一样,定义按钮的命令参数... - Amit Singh

2

这是最好、最快的方式!+1 - JAN

1
应该是这样的 commandargument
aspx
<asp:ImageButton ID="imgEditApp" CommandArgument='<%# Eval("AppID") %>' runat="server" ... OnClick="btnEdit_Click"/>

代码

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
          int categoryId = Convert.ToInt32(e.CommandArgument);
          Response.Redirect("/Secured/EditApplication.aspx?AppID="+categoryId);
        }

或者您可以使用ImageButton的PostBackUrl属性,代码如下:
<asp:ImageButton ID="imgEditApp" PostBackUrl='<%# string.Format("/Secured/EditApplication.aspx?AppID={0}", Eval("AppID")) %>' runat="server" />

0
请检查以下代码片段。
这是aspx文件中的代码,其中包含两列DataBound“AppId”和TemplateColumn“Action”,包含图像按钮。请注意图像按钮的CommandName和CommandArgument属性。还要为gridview定义OnRowCommand事件监听器。
<asp:GridView ID="grdDisplayData" runat="server" AutoGenerateColumns="false" 
            EnableViewState="false" onrowcommand="grdDisplayData_RowCommand">
            <Columns>
                <asp:BoundField HeaderText="AppId" DataField="AppId" />
                <asp:TemplateField HeaderText="Action" >
                    <ItemTemplate>
                        <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="MyEdit" 
                        CommandArgument="<%# ((GridViewRow) Container).RowIndex%>"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ImageAction">
                    <ItemTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" Width="15px" Height="15px" 
                            CommandName="ImgEdit" CommandArgument="<%# ((GridViewRow) Container).RowIndex%>"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

这里是代码背后的代码。e.CommandArgument返回点击图像按钮的行的索引。

protected void grdDisplayData_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName == "ImgEdit")
            {
                int RowIndex = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("/Secured/EditApplication.aspx?AppID=" + grdDisplayData.Rows[RowIndex].Cells[1].Text.Trim());
            }
        }

请告诉我这是否解决了你的问题。

祝好!!! 皮尤什·德斯帕恩德


你遇到了什么错误?如果网格中的行数超过了Int16所支持的范围,将"Convert.ToInt16"更改为"Convert.ToInt32"或"Convert.ToInt64",然后检查错误是否消失。如果没有消失,请发布错误信息。祝好! - Piyush Deshpande

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接