C# 如何在codebehind中获取gridview列值

C# 如何在codebehind中获取gridview列值,c#,asp.net,.net,C#,Asp.net,.net,如果单击第二行的“编辑图像”按钮,如何从codebehind中的gridView获取AppId Aspx代码: <asp:BoundField HeaderText="AppId" DataField="AppID" /> <asp:TemplateField HeaderText="Actions" ControlStyle-Width="20px" ItemStyle-Width="130px"> <ItemTemp

如果单击第二行的“编辑图像”按钮,如何从codebehind中的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>
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);
}

像这样尝试…不定义按钮的单击事件…像这样定义按钮

     <asp:ImageButton ID="imgEditApp" runat="server"
 ImageUrl="~/Images/AppSetup/Action-edit-icon.png" 
    Height="18px" ToolTip="Edit Application" CssClass="grdImageAlign" 
CommandName="Edit"/>
编辑: 我认为您在定义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);
}
编辑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
    }

受保护的无效btnEdit\u单击(对象发送者,事件参数e)
{
字符串appid=(发送方为ImageButton).CommandArgument;
Response.Redirect(“/Secured/EditApplication.aspx?AppID=“+AppID
}

您可以从中获取栅格视图单元格值

GridView.Rows[RowIndex].Cells[CellIndex].Text
这里的“RowIndex”是要从中获取数据的行号,“CellIndex”是要从中获取数据的单元格号

我认为gridview的事件“OnRowCommand”最适合您的问题。 使用blow链接了解更多详细信息

它应该与
aspx


或者,您可以使用imagebutton的属性,如下所示:

<asp:ImageButton ID="imgEditApp" PostBackUrl='<%# string.Format("/Secured/EditApplication.aspx?AppID={0}", Eval("AppID")) %>' runat="server" />

检查此代码段

这是aspx文件中的代码,包含两列数据绑定的“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>
如果这解决了你的问题,请告诉我

干杯!!!
Piyush Deshpande

您可以使用数据键或隐藏字段方法,只要看看这里,如果我使用上面的代码,我会得到这个错误,错误'System.EventArgs'不包含'NewEditIndex'的定义,并且找不到接受'System.EventArgs'类型的第一个参数的扩展方法'NewEditIndex'(是否缺少using指令或程序集引用?)您不能这样做…您需要删除图像按钮的onlcik事件…定义gridview的行编辑事件显示grid view apsx代码..我这行的意思是您定义gridview id和其他property@Vignesh现在请仔细查看我的答案,像我在这里定义的那样重新发布您的buttonm代码…并定义行编辑…gridview事件和write代码ke write over/…code there…另一件事是,如果AppId列被移动到表中的第二个位置,那么在这种情况下,上面的代码将得到错误的列值?对于这一点,有没有办法使用列名而不是将索引称为数字?您得到的错误是什么?如果网格中的行比我看到的多如果Int16支持,请将“Convert.ToInt16”更改为“Convert.ToInt32”或“Convert.ToInt64”,然后检查错误是否消失,如果不发布错误。干杯!这是最好、最快的方法!+1
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
          int categoryId = Convert.ToInt32(e.CommandArgument);
          Response.Redirect("/Secured/EditApplication.aspx?AppID="+categoryId);
        }
<asp:ImageButton ID="imgEditApp" PostBackUrl='<%# string.Format("/Secured/EditApplication.aspx?AppID={0}", Eval("AppID")) %>' runat="server" />
<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>
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());
            }
        }