C# 如何在代码隐藏中获取gridview列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16621304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:29:54  来源:igfitidea点击:

How to get gridview column value in codebehind

c#asp.net.net

提问by Vignesh

How to get the AppId from gridView in codebehind, if I clicked the edit image button in second row.

如果我单击第二行中的编辑图像按钮,如何从代码隐藏中的 gridView 获取 AppId。

Application List

申请清单

Aspx Code:

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# Code:

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);
}

采纳答案by Amit Singh

Try Like this....Don't Define Click Event of Button....Define Button Like this...

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

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

And Define Your GridView RowEditing event Like this....

并像这样定义您的 GridView RowEditing 事件....

 protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
          Response.Redirect("/Secured/EditApplication.aspx?AppID="+YourGridViewId.Rows[e.NewEditIndex].Cells[1].Text);
        }

Edit:I think you have problem in definig RowEditingEvent.....ok you can do this...nothing to change just write this code in you Click event...

编辑:我认为您在定义 RowEditingEvent 时遇到问题.....好的,您可以这样做...没有什么可更改的,只需在您的 Click 事件中编写此代码...

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

编辑 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
    }

回答by Sain Pradeep

You can get grid view cell value from this.

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

GridView.Rows[RowIndex].Cells[CellIndex].Text

Here "RowIndex" is row number from which you want to get data and "CellIndex" is cell number from which you want to get data.

这里“RowIndex”是您要从中获取数据的行号,“CellIndex”是您要从中获取数据的单元格号。

I think event "OnRowCommand" of gridview is best suited for your problem. use blow link for more details

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

http://www.codeproject.com/Tips/564619/Example-of-gridview-rowcommand-on-Button-Click

http://www.codeproject.com/Tips/564619/Example-of-gridview-rowcommand-on-Button-Click

回答by Roar

it should be with commandargument
aspx

它应该与commandargument
aspx

<asp:ImageButton ID="imgEditApp" CommandArgument='<%# Eval("AppID") %>' runat="server" ... OnClick="btnEdit_Click"/>

code

代码

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


or u can use PostBackUrlproperty of imagebutton and it would be like this:


或者你可以使用imagebutton 的PostBackUrl属性,它会是这样的:

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

回答by Piyush Deshpande

Check this code snippet.

检查此代码片段。

This is the code in aspx file having two columns DataBound "AppId" and TemplateColumn "Action" containing Image Button. Observe CommandName and CommandArgument properties of Image Button. Also Define OnRowCommand Event listener for gridview.

这是 aspx 文件中的代码,有两列 DataBound "AppId" 和 TemplateColumn "Action" 包含图像按钮。观察 Image Button 的 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>

And here is the code behind code. The e.CommandArument returns the index of the row in which the image button was clicked.

这是代码背后的代码。e.CommandArument 返回单击图像按钮所在行的索引。

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());
            }
        }

Let me know if this fixed your problem.

让我知道这是否解决了您的问题。

Cheers!!! Piyush Deshpande

干杯!!!皮尤什·德什潘德