Make GridView AutGenerateColumn a LinkButton that fires RowCommand event - c#

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.

Related

One GridView and many LinkButtons (asp .net WebForms)

I have a GridView and dynamically added LinkButton in GridView cell:
protected void TestGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (var l in links)
{
e.Row.Cells[6].Controls.Add(l.button);
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = l.button.ID;
UpPanel.Triggers.Add(trigger);
}
}
}
Links are added, its ok, but clicking on link, GridView refreshes and links disappears. When I delete this condition from page_load function, I have a problem with page index changing event - GridView does not refresh:
if (!IsPostBack)
{
TestGrid.DataBind();
}
What can I do to stop links disappearing and keep page index functional? And is there a better way to add many LinkButtons in one GridView cell? Thank you!

onRowdatabound not giving label value in asp.net

I am creating the dynamic grid and adding textbox and label in row dynamically on some button click and bind some data from the database to textbox and label.I want to change the label text color and text box read only for condition depend on database.i have used onRowDataBound event of gridview but not getting any value in the textbox and label for a row. Can anyone helps me to solve this issue? Thanks
protected void grdMasterData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label status = e.Row.FindControl("lblProduct") as Label;
if (status.Text == "LY Actuals")
{
e.Row.Cells[0].CssClass = "lblProductColor";
// lbtAction.Visible = false;
}
}
}
In RowDataBound three row types provided by grid, you are using only DataRow, try to get your values from empty row and footer row because first record will not come in DataRow.

Gridview Label Appear in a Selected Row

I has a Gridview called Gridview1 which is binded to a datasource.
and I have a ItemTemplate at Column 5 within it contain a
- Linkbutton called search
- Label called lbl_password , visible press to true
- Label called txt_password , visible set to false
code in done in asp.net webForm , .aspx
when link button pressed this function will get the ID at cell 1 in the row tat button clicked
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string fieldID = grdrow.Cells[1].Text;
Now i want to know how can I make the txt and lbl become visible when the button pressed
I am assuming you mean when link button pressed you want to make the text and label from the same row to be visible.For that purpose you can just use the findcontrol method on gridview row to find the control you need.
If that's what you want then in link button click event you can do as follow,
protected void lnkButton_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
((TextBox)grdrow.FindControl("yourtextboxID")).Visible = true;
((Label)grdrow.FindControl("yourLabelID")).Visible = true;
}
Here is more info on findcontrol.

ModalPopup_Extender.Hide() Issue

I have a button, when clicked, a modalpopup with gridview inside it. as below;
protected void grdDetails_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdDetails.SelectedRow;
//Note: Value of that row's cell must display in A text box (txtBox) on main page
txtBox.Text = row.Cells[2].Text;
ChargeFilterModalDialogExtender.Hide();
}
After the extender is hide, the value of the cell not displaying in the text box. Am I doing something wrong?
Where you have the textbox?
Whether you are using UpdatePanel?, if so define the needed asynchronous postback triggers.

c# GridView RowDataBound Error

I have a gridview that should be editable when a row is clicked. This gridview is clickable in a row so when I clicked it, the row will be displayed in other pages for editing reason. I got an error like this
Specified argument was out of the range of valid values.
Parameter name: index.
This is happening for this line:
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
How can I fix this?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get reference to button field in the gridview.
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
e.Row.Attributes["onclick"] = _jsSingle;
}
}
}
You should be using something like e.Row.FindControl("linkbuttonid"). This will get you the required link button from the current row, then you can attach your handlers to the same and perform your logic

Categories

Resources