Answered by:
Naming Container and using the row's data later on.

Question
-
User842841555 posted
Hello, I've a column of buttons next to a populated gridview. I'm trying to save the row that lines up where the button was clicked, I then want to use some cells in that row to populate a URL that will be executed when the button is clicked. The below is obviously incorrect. Any idea how to properly do this?
Thank you,
protected void ebb_Click(Object sender, EventArgs e)
{
LinkButton ebb = (LinkButton)sender;
GridViewRow gvr = (GridViewRow)ebb.NamingContainer;
Page.ClientScript.RegisterStartupScript(
GetType(), "OpenWindow", "window.open('https://website.com/query?='+ gvr.rows[0].Cells[0].Text ,'_newtab');", true);
}
Monday, September 23, 2019 7:29 PM
Answers
-
User288213138 posted
Hi Mike1986,
I'm trying to save the row that lines up where the button was clicked, I then want to use some cells in that row to populate a URL that will be executed when the button is clickedgvr.rows[0].Cells[0]If you want to get the value of a cell in the current GridView, you can try below code:
protected void apibutton_Click(object sender, EventArgs e) { string a=gdvCustomer.Rows[0].Cells[0].Text; }
If you want to get the value of the current row, I recommend you do it in the OnRowCommand event.
Here is a demo about how to Get GridView Cell value on Button Click in ASP.Net using C#. https://www.aspsnippets.com/Articles/Get-GridView-Cell-value-on-Button-Click-in-ASPNet-using-C-and-VBNet.aspx
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 24, 2019 6:32 AM -
User288213138 posted
Hi Mike1989,
Yes, they are template field.If you are using a template field, I recommend that you get the value of the cell in the RowCommand () event.
<asp:GridView ID="gdvCustomer" runat="server" AutoGenerateColumns="False" OnRowCommand="gdvCustomer_RowCommand"> <Columns> <asp:TemplateField HeaderText="Id"> <ItemTemplate> <asp:Label ID="txtId" runat="server" Text='<%# Eval("Id") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="txtName" runat="server" Text='<%# Eval("Name") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Age"> <ItemTemplate> <asp:Label ID="txtAge" runat="server" Text='<%# Eval("Age") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Call API" SortExpression="APIcall"> <ItemTemplate> <asp:LinkButton ID="apibutton" runat="server" CommandName="Select" CommandArgument="<%# Container.DataItemIndex %>" OnClick="apibutton_Click">Find It</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt.Rows.Add(1, "name1", "age1"); dt.Rows.Add(2, "name2", "age2"); dt.Rows.Add(3, "name3", "age3"); gdvCustomer.DataSource = dt; gdvCustomer.DataBind(); } protected void apibutton_Click(object sender, EventArgs e) { } protected void gdvCustomer_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = gdvCustomer.Rows[rowIndex]; string name = (row.FindControl("txtName") as Label).Text; Response.Write(name); } }
The result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 25, 2019 8:29 AM
All replies
-
User288213138 posted
Hi Mike1986,
I'm trying to save the row that lines up where the button was clicked, I then want to use some cells in that row to populate a URL that will be executed when the button is clickedgvr.rows[0].Cells[0]If you want to get the value of a cell in the current GridView, you can try below code:
protected void apibutton_Click(object sender, EventArgs e) { string a=gdvCustomer.Rows[0].Cells[0].Text; }
If you want to get the value of the current row, I recommend you do it in the OnRowCommand event.
Here is a demo about how to Get GridView Cell value on Button Click in ASP.Net using C#. https://www.aspsnippets.com/Articles/Get-GridView-Cell-value-on-Button-Click-in-ASPNet-using-C-and-VBNet.aspx
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 24, 2019 6:32 AM -
User-1716253493 posted
You have already in that row, i guess the correct one is gvr.Cells[0].Text
try this
protected void ebb_Click(Object sender, EventArgs e) { LinkButton ebb = (LinkButton)sender; GridViewRow gvr = (GridViewRow)ebb.NamingContainer; Page.ClientScript.RegisterStartupScript( GetType(), "OpenWindow", "window.open('https://website.com/query?='+ gvr.Cells[0].Text ,'_newtab');", true); }
Tuesday, September 24, 2019 7:02 AM -
User842841555 posted
LinkButton ebb = (LinkButton)sender; GridViewRow gvr = (GridViewRow)ebb.NamingContainer; string accession = gvr.Cells[1].Text;
Page.ClientScript.RegisterStartupScript(
this.GetType(), "OpenWindow", "window.open('https://website.com/query=' "+ accession +" ,'_newtab');", true);Hello,
In the code above the string "accession" appears to be NULL. Any ideas as to why that would happen?
edit: here is an image of the designer. When search is clicked, I'm trying to store the 2nd cell in a variable to call in the API.
Tuesday, September 24, 2019 2:28 PM -
User288213138 posted
Hi Mike1986,
LinkButton ebb = (LinkButton)sender; GridViewRow gvr = (GridViewRow)ebb.NamingContainer; string accession = gvr.Cells[1].Text;
This code is fine, please check if the data in the data source is empty.
Best regards,
Sam
Wednesday, September 25, 2019 1:46 AM -
User-1716253493 posted
What column 1 is?
For boundfield the code should work.
But for templatefield, use find control to gelt control inside it
Wednesday, September 25, 2019 2:46 AM -
User842841555 posted
Yes, they are template field.Wednesday, September 25, 2019 3:26 AM -
User288213138 posted
Hi Mike1989,
Yes, they are template field.If you are using a template field, I recommend that you get the value of the cell in the RowCommand () event.
<asp:GridView ID="gdvCustomer" runat="server" AutoGenerateColumns="False" OnRowCommand="gdvCustomer_RowCommand"> <Columns> <asp:TemplateField HeaderText="Id"> <ItemTemplate> <asp:Label ID="txtId" runat="server" Text='<%# Eval("Id") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="txtName" runat="server" Text='<%# Eval("Name") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Age"> <ItemTemplate> <asp:Label ID="txtAge" runat="server" Text='<%# Eval("Age") %>' /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Call API" SortExpression="APIcall"> <ItemTemplate> <asp:LinkButton ID="apibutton" runat="server" CommandName="Select" CommandArgument="<%# Container.DataItemIndex %>" OnClick="apibutton_Click">Find It</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt.Rows.Add(1, "name1", "age1"); dt.Rows.Add(2, "name2", "age2"); dt.Rows.Add(3, "name3", "age3"); gdvCustomer.DataSource = dt; gdvCustomer.DataBind(); } protected void apibutton_Click(object sender, EventArgs e) { } protected void gdvCustomer_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridViewRow row = gdvCustomer.Rows[rowIndex]; string name = (row.FindControl("txtName") as Label).Text; Response.Write(name); } }
The result:
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, September 25, 2019 8:29 AM -
User-1716253493 posted
Mike1986
Yes, they are template field.Bound the field to label text inside it
Use
Label lbl = (Label)gvr.FindControls("Label1");
then get lbl.Text
Wednesday, September 25, 2019 8:45 AM