How to get gridview column value in codebehind

How to get AppId from gridView in codebehind if I clicked the image editing button in the second line.

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); } 
+4
source share
4 answers

Try it like this .... Don't Define Click Event Button .... Define Button Like It ...

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

A Define the 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 a problem in a specific RowEditingEvent ..... ok, you can do it ... 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 } 
+2
source

From this, you can get the value of the grid cell.

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

Here "RowIndex" is the line number from which you want to receive data, and "CellIndex" is the number of the cell from which you want to receive data.

I think the "OnRowCommand" gridview event is best for your problem. use the strike link for more details

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

+2
source

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 you can use the PostBackUrl imagebutton property, and it will be like this:

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

Check out this piece of code.

This is the code in the aspx file containing two DataBound columns "AppId" and TemplateColumn "Action" containing the image button. Observe the CommandName and CommandArgument properties of the Image button. Also define an 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 by code. E.CommandArument returns the index of the row where 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 fixes your problem.

Hooray!!! Piyush Deshpande

0
source

Source: https://habr.com/ru/post/1481503/


All Articles