Passing Control Id from Linkbutton in a gridview - c#

How do I pass the Control ID in a gridview that is derived from a stored procedure into a variable. I will use the variable to pass into the database later on to return some results. Thanks.

var controlId = ((LinkButton)gridView1.Rows[0].FindControl("lbName")).Id;
Are you trying to do something like the above?
Update
You can use the OnSelectedIndex event of the GridView to find the row that was select.
void GridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
var controlId = ((LinkButton)row.FindControl("lbName")).Id;
}

I tend to use the CommandName and CommandArgument fields with LinkButtons in a GridView.
The CommandName represents the name of the command, so that you can have more than one linkbutton that do different things and the CommandArgument is the Database ID or other point of reference for the command.
You need to subscribe to the RowCommand event handler on the GridView

Related

Command argument string is empty in rowcommand event c#

I have a gridview. Inside it I have a link button. I want to get the row index of the button which was clicked inside the rowcommand event handler. This can be done using the command argument property. however it is returning empty string and there by getting an exception.
This is the line present inside event handler
Int indexval = Convert.ToInt32(e.commandArgument.ToString());
I've also tried this to get row index.
Inside aspx page:
commandArgument= <%#container.DataItem%>
But this also returns empty string
Tried like this in rowcommand event
Gridview gr = (gridview)sender;
Int Ind = gr.rows.selectedIndex.
Here Ind value shows as -1
There are some questions on this but all are in VB.net
I want the solution to this to be in c#.
PS I'm new to c# and asp.net
I use CommandField
Add this CommandField to your GridView <Columns>...</Columns>
<asp:CommandField ShowSelectButton="true" SelectText="View/Edit" ItemStyle-Width="60" />
This ties in to GridView_SelectedIndexChanging which gives you the RowIndex:
protected void gvCategories_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int rowIndex = e.NewSelectedIndex;
}

Make GridView AutGenerateColumn a LinkButton that fires RowCommand event

Simple question which my searches seem to be coming up blank on.
I have a GridView that is AutoGeneratingColumns=True
This generates a column "MyColumn". I would like to make this columns value a clickable LinkButton that fires the RowCommand event of the GridView with a specific CommandNmae and the cells value as the CommandArgument.
I'm trying to do this without using a custom ItemTemplate, I know how to do that. I'm hoping i can pragmatically modify a auto generated column in the GridView as described.
Tia
This can be done in the OnRowDataBound event of the GridView.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the current row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
///create a new linkbutton
LinkButton button = new LinkButton();
button.Text = "LinkButton";
button.CommandArgument = "MyArgs";
//assign a command to the button
button.Command += Button_Command;
//add the button to cell x
e.Row.Cells[3].Controls.Add(button);
}
}
Because you are adding control dynamically, the binding of the GridView should NOT be wrapped inside an IsPostBack check. Something you would do normally.

Textbox control from gridview column is returned but gives empty value

I have succeeded in retrieving Textbox from textbox column of Gridview, but it can't retain the value I have put there. It simply returns reference to textbox but miss the encapsulated value; Following is sample code I'm using to retrieve the textbox and its value; Please help me out of handling such problem.
protected void Submit_RowCommand(object sender, GridViewCommandEventArgs e)
{
int RowIndex = int.Parse(e.CommandArgument.ToString());
string userId = ApprovalParentGridView.DataKeys[RowIndex]["UserID"].ToString();
GridViewRow row = ApprovalParentGridView.Rows[RowIndex];
TextBox remarks = (TextBox)row.Cells[6].FindControl("txtRemark");
string remarks = remarks.Text.Trim();
...
}
Thanks in advance!!!
I have had this in the past and found it to be because the Gridview is loaded in Page_Load and the textbox had its value set in that routine, and Submit_RowCommand is fired afterwards.
You need to rebuild the gridview so that the control is in the controls collection but you need to be careful that you do not put the value in the textbox at the same time or else the code behind will overwrite the value that you user has entered from their browser.

Parameterised Drill down with Gridview

I'm trying to get a Gridview "select" link to drill down to a specific page for the selected row in ASP.NET with C#. This gridview is generated dynamically in the page_load, and is databound from a fairly simple SQL select query. Each row has a unique id, and I would like this to be passed as a parameter in the url when the select button is clicked for that row. So, when you click the "select" button on the row with an id value of 9 (note - not the id as defined by the gridview, but the one from the SQL query) you are redirected to an address such as moreDetail.aspx?id=9.
However, when trying to pass the id into the event handler I've hit issues... GridView.SelectedIndexChanging takes the usual (object sender, EventArgs e) as parameters and nothing else, and since the Gridview is created at Page_Load the EventArgs class is useless. I can't seem to find any way to pass the id that I have retrieved earlier into the event handler.
After lots of searching,I tried creating a class that extends EventArgs (obviously with my extra parameter added in), but it seems using any parameters other than (object sender, EventArgs e) just won't work. I could theoretically redo the SQL query within the event handler, but that seems to me a terrible way to achieve what I'm looking for, so I'm hoping someone will be able to see what I've got wrong here, because I'm sure I'm missing something obvious.
Some code - grid.SelectedRow.Cells[0] will contain the parameter I want to pass:
In Page_Load:
GridView grid = new GridView();
grid.DataSource = source;
CommandField selectField = new CommandField();
selectField.ShowSelectButton = true;
selectField.SelectText = "View Jobs";
grid.Columns.Add(selectField);
grid.SelectedIndexChanging += grid_SelectedIndexChanging;
grid.DataBind();
content.Controls.Add(grid);
And the Event Handler:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Obviously this doesn't work because the scope of grid doesn't extend to the handler...but how can I get access to that data?
You need to cast sender to GridView to reference your calling GridView:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
GridView grid = (GridView)sender;
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}

Obtaining the ctl property from a gridview control in C#, ASP.Net

I am trying to obtain the ctl property that is specific to each row in a gridview. In my scenario, there is a list of titles, each title is displayed as a hyperlink using the LinkButton. When this is clicked, I would like to pass the ctl property (or what ever value is specific to that title, to the database and pull the information that is relative to the value).
I guess my first step is to obtain the ctl value. Could someone please help me get on my way. Thanks in advance.
You will need to keep track of the control ID that is written to the page in the row created event. Make a integer to increment for each row written and save it as a command argument on a link in the grid.
linkbutton CommandArgument='<%# Eval("some_id") %>'
protected void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton) sender;
if (linkButton != null)
{
if (linkButton.CommandArgument != null)
{
...some code...
}
}
}

Categories

Resources