GridView row clicked every time page is rendered - c#

Basically everytime I load or reload the page it treats every row in my gridview as if it has been clicked, thus calling the function in the codebehind related to it once for every row which at the time is set to 13 rows max. Thanks for any help.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Attaching one onclick event for the entire row, so that it will
// fire SelectedIndexChanged, while we click anywhere on the row.
e.Row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(this.GridView2, "Select$" + e.Row.RowIndex);
}
Console.WriteLine("");
}

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!

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.

Disable button in Gridview in multiple pages

I have buttons in one Column called Type. When the user clicks on the button it should be disabled. This works fine in the first page but it doesnt on the second,third,fourth pages.
I have 10 rows in my page and e.CommandArgument gets the row number.
I believe the buttons are populated in the gridview from 0 to 9 and e.CommandArgument is 1-10. Thats why I have (e.CommandArgument) - 1 and it works find in the first page.
The things that in the second page the next buttons are again 0-9 but my e.CommandArgument is 11-20. Any ideas?
protected void GridViewType_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button btnVote = (Button)GridViewType.Rows[Convert.ToInt32(e.CommandArgument) - 1].FindControl("btnVote");
btnV ote.Enabled = false;
}
try the following
protected void GridViewType_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)(e.CommandSource);
Button btnVote = (Button)row.FindControl("btnVote");
btnVote.Enabled = false;
}
kindly check this :
want to Find Control in gridview on RowCommand event

How to change row height on last page of Gridview

On the last page if If i have 1 or 2 items the header and the pager stretch too much.. it all autosizes..
My gridview height is not set, But I set the row property to 30pixels..still doesnt prevent the autosizing..
I was searching for a solution over the net..and the closest solution that i found was that:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(this.GridView1.Rows.Count<this.GridView1.PageSize)//I need here to grab the last page..// do something like e.Row.... and last page.
{
foreach (GridViewRow tt in GridView1.Rows)
{ tt.Height = Unit.Pixel(30); }
}
}
It isnt correct.... what i need to happen, is when the user clicks the last page the height should be modified to to 30 pixels.
Any other ideas on how to prevent autosizing on the last page are welcome!!
To find out if you're on the last page of a paged grid and then change row heights, use the following (tested) code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (GridView1.PageIndex == GridView1.PageCount - 1)
{
e.Row.Height = Unit.Pixel(30);
}
}

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