Vignesh
Vignesh

Reputation: 1518

How to get gridview column value in codebehind

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

Application List

Aspx Code:

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

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

Upvotes: 4

Views: 22826

Answers (4)

Piyush Deshpande
Piyush Deshpande

Reputation: 195

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.

<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.

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

Upvotes: 0

Amit Singh
Amit Singh

Reputation: 8109

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....

 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...

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
    }

Upvotes: 2

Roar
Roar

Reputation: 2167

it should be with 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 PostBackUrl property of imagebutton and it would be like this:

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

Upvotes: 1

Sain Pradeep
Sain Pradeep

Reputation: 3125

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.

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

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

Upvotes: 2

Related Questions