Find what button was clicked inside a Gridview ASP.Net c# - c#

I have a gridview named gvappts.
This grid has seven columns one for each day of the week.
The grid also has multiple rows, each cell has a button inside.
If I am using an onRowCommand event is there a way to find the name of the button that was clicked?
I started off with this:
if (e.CommandName == "GetData")
{
//Get rowindex
int rowindex = Convert.ToInt32(e.CommandArgument);
//Get Row
GridViewRow gvr = gvappts.Rows[rowindex];
}
not sure if I am going about this quite right.
Here is a snippet for the gridview on the web page, all the columns are using TemplateFields.
<Columns>
<asp:TemplateField HeaderText="Day 1" HeaderStyle-CssClass= "hdr" ItemStyle-CssClass="Grid">
<ItemTemplate>
<asp:LinkButton id="lbd1" runat="server" Text='<%#(Eval("Day1"))%>' CommandName="GetData" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>

You can use the CommandSource and cast it back to a LinkButton.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lb = e.CommandSource as LinkButton;
string value = lb.CommandArgument;
}

Related

How to refer to which row was selected when i click textbox in gridview?

I have a gridview, inside the gridview is a textbox on templatefield, how do i get the row index of the clicked textbox when i change its value?
<asp:GridView ID="productView" runat="server" BorderWidth="3px" CellPadding="4" CellSpacing="2" AutoGenerateColumns="False" Width="1000px" OnSelectedIndexChanged="productView_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Sacks">
<ItemTemplate>
<asp:TextBox ID="txtSacks" runat="server" CssClass="form-control" Text ="0" Width="100px" OnTextChanged="txtSacks_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs:
protected void txtSacks_TextChanged(object sender, EventArgs e)
{
//Rowindex of the texbox
}
Get it using RowIndex
GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
int index = gvRow.RowIndex;
You can get the GridViewRow via the NamingContainer property of the TextBox:
protected void txtSacks_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
GridViewRow row = (GridViewRow) txt.NamingContainer;
int rowIndex = row.RowIndex;
}
This is better than using txt.Parent.Parent because it still works even with nested controls (f.e. if you want to add the TextBox to a Table or Panel).

how will i transfer a value from a gridview of a previous page to another web form?

how can i pass the value from the gridview to the next webform? here is my gridview code below:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" PostBackUrl='<%# "Details.aspx?RowIndex=" + Container.DataItemIndex %>'></asp:LinkButton>
</ItemTemplate
</asp:TemplateField>
but i can't seem to make it work. it goes to the next form but does not get the value. my id has a format "1000001" and it does read it.
here is my code for the next page. i created a new gridview just to store the value from.
int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
GridView gv1 = (GridView)this.Page.PreviousPage.FindControl("GridView1");
GridViewRow row = GridView1.Rows[rowIndex];
lblID.Text = row.Cells[0].Text;
Add OnRowCommand="GridView_RowCommand" like
<asp:GridView ID="GridView" OnRowCommand="GridView_RowCommand"
Update your Columns as
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID ="lnkEdit" runat="server" Text="View" CommandName= "Redirect" CommandArgument='<%#Eval("ID")%>' ></asp:LinkButton>
</ItemTemplate
In Code-Behind file
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Redirect") {
//Get Command Argument
Response.Redirect("Details.aspx?RowIndex="+e.CommandArgument.ToString(),true);
}
}

Get column value from GridView

I'm new to ASP.NET and I am using GridView to display data on a web page. I also added a button in Gridview called process orders.
<asp:GridView ID="GridView1" runat="server" autogenerateselectbutton="True" GridLines="None" AllowPaging ="true"
OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Process" runat="server"
CommandName="processorders"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Process orders" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I used the row command method to get the selected index of the row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "processorders")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
}
Also I've implemented selectedIndexChanged method for the default select button:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
txtbox.Text = "You selected " + row.Cells[3].Text + ".";
}
If I try to use the GridViewRow in GridView1_RowCommand I get a null pointer exception. The row is always null but it works fine in GridView1_SelectedIndexChanged.
What am I trying to achieve is, get the column value from the selected row using my Process button and then use that value to update a database. I don't want to do it using Select.
And is there any way I can use row.Cells[] without specifying the index. I want to get the value of a column name.
Help? Please ??

How to get cell data from gridview?

I am trying to fetch data from certain GridView row when I click link in that row...
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="AccessDataSource1" AllowPaging="True">
<Columns>
<asp:BoundField DataField="nazivTeme" HeaderText="nazivTeme"
SortExpression="nazivTeme" />
<asp:BoundField DataField="datum" HeaderText="datum" SortExpression="datum" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkView" commandname="view"
OnClick="lnkView_Click">Komentiraj</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I would like to get data from the row where I click the LinkButton (only first cell).
I hope you understand what I want :)
Do not subscribe to Click event of the button. Instead, subscribe to the RowCommand event of the grid view. Then, in the event handler, you can evaluate which command was received and which row was affected. Once you get the row, you can get its cell value by cell index or ID:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "view")
{
// Retrieve the row index
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row by its index
GridViewRow row = this.GridView1.Rows[index];
// Get the 1st cell value from the row
string cellValue = r.Cells[0].Text;
}
}

How to trigger a function when LinkButton in each row of the Grid is clicked

I have a GridView in which I have put an extra column Details, and in each row of the GridView, I have a LinkButton called Details. So when I click this I want an event to trigger.
The asp.net code is:
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Kindly help. Thank you.
Then use the LinkButton.Click-event:
<asp:LinkButton ID="Details" OnClick="LinkClicked" runat="server" Text="Details">
in codebehind:
protected void LinkClicked(Object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
GridViewRow row = (GridViewRow)link.NamingContainer;
// assuming there's a label with ID=Label1 in another TemplateField:
Label label1 = (Label)row.FindControl("Label1");
label1.Text="You clicked the link button";
}
If you need the GridViewRow of the GridView where the user clicked the link, for example to find controls in other templateFields, you can use the NamingContainerproperty.
Handle GridView.RowCommand Event
Occurs when a button is clicked in a GridView control.
<asp:LinkButton ID="Details" commandname="Details" runat="server" Text="Details"></asp:LinkButton>
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Details")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
//Your Code
}
}
You can have a command argument to determine which row was clicked (assuming you have more than one button (standard buttons/link buttons) in a row
<asp:GridView OnRowCommand="GridViewOnItemCommand" runat="server">
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="btnDetails" CommandName="Details" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Details" runat="server"/>
<asp:LinkButton ID="btnDelete" CommandName="Delete" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Delete" runat="server"/>
<ItemTemplate>
</asp:TemplateField>
</asp:GridView>
in the code behind file
protected void GridViewOnItemCommand(object sender, GridViewCommandEventArgs e)
{
//you can determine which button was clicked (detail or delete)
var command = e.CommandName;
//you can determine which row was clicked
var arg = e.CommandArgument;
if(command == "Details")
ShowDetails(arg);
if(command == "Delete")
Delete(arg);
}
hope this helps

Categories

Resources