Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView.RowCommand not firing on first run of UserControl

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I have a set of WebUserControls on a web page (these are defined at design time and loaded with data at run time).
The WebUserControl has a GridView in it which has a RowCommand event set (again at design time).

When I first run my web page (which has the data loaded into the UserControl's GridViews according to the user selection) and press a button in the GridView - the RowCommand does not fire.

However when I click the button for the second time, this event (RowCommand) then fires correctly.

Why does the event not work on the first run on the page?

Any advice would be appreciated.
Thanks.
Steve
 
You have to bind the datasource to the gridview in the event handler again. Also, make sure you load the datasource in the page_loadcomplete method instead of page_load, or you will lose any edits as well.
 
Which event handler do I need to be setting the GridView.DataBind() command again?

Currently I'm working as follows:

1. I have 10 of the same WebUserControl (each with it's GridView on it) - named WebUserControl1, ... WebUserControl10 - on the default.aspx page

2. Within the default.aspx page, depending on user selections I set the data in the GridViews within the WebUserControls:

WebUserControl wc = (WebUserControl)panel1.FindControl("WebUserControl" + (i + 1).ToString());
wc.Visible = true;
wc.LoadData();

3. Within the WebUserControl I have the (public) LoadData code which does:

GridView1.DataSource = null;
GridView1.DataSource = dt;
GridView1.DataBind();


When I run the page, the data loads ok on the first run through - but the clicking of the button doesn't work until I click it for the second time.

I'm assuming its the GridView1.RowCommand which needs to be re-hooked up - but don't know where to achieve this to ensure the buttons will operate on first run through of the page.

Thanks again.
Steve
 
In the WebUserControl go to the gridview and check the rowcommand event. In there do gridview1.databind();
 
OK. In the 'protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)' I've now introduced the code 'GridView1.DataBind();'.

However this RowCommand is simply not being fired on the first usage of the WebUserControl so still does not get run.

Is this where you were suggesting for me to add the code?

Steve
 
Can you show the full code in GridView1_RowCommand. Also what are you trying to do exactly?
 
Part of the code is as follows:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowindex = -1;
...

switch (e.CommandName)
{
case "MoveUp":
rowindex = Convert.ToInt32(e.CommandArgument);
...
...
break;
case "MoveDown":
rowindex = Convert.ToInt32(e.CommandArgument);
...
...
break;
}
}

However, when debugging if I put a debug point on the first line (int rowindex = -1;) this does not get hit until second usage of the button in the grid.

Thanks again.
Steve
 
WebUserControl wc = (WebUserControl)panel1.FindControl("WebUserControl" + (i + 1).ToString());
wc.Visible = true;
wc.LoadData();


Where do you do that? is that in Page_Load or Page_LoadComplete?

What is happening is that when the user clicks a button that does a postback. Causing Load/PreRender to happen. So if you have that LoadData() in Page_Load you will not see it on the first click. Add the LoadData() to

protected void Page_LoadComplete(object sender, EventArgs e)
 
The block of code:

WebUserControl wc = (WebUserControl)panel1.FindControl("WebUserControl" + (i + 1).ToString());
wc.Visible = true;
wc.LoadData();

... is run from a button being clicked on the main page (default.aspx say) where the 10 WebUserControls are originally set (and made not visible).

The order of events I get when I click the button in the grid the first time is as follows:
(1). Default.aspx Page_Load fires...
(2). WebUserControl Page_Load fires...

WebUserControl GridView1_RowCommand does not fire...

... and on the second time is:
(1). Default.aspx Page_Load fires...
(2). WebUserControl Page_Load fires...
(3). WebUserControl GridView1_RowCommand fires...

Does this suggest what I'm doing wrong?
Thanks for your time on this matter
Steve
 
No problem.

Events are handled after PageLoad before PageLoadComplete. So, the WebUserControl's Event is handled before the gridview performs the RowCommand event. So it gets delayed.

My solution isn't very clean and probably isn't the best one but try something like this.

Wherever you do the wc.LoadData(); instead do something like
x1 = true;

In Page_LoadComplete(object sender, EventArgs e) do
if (x1) panel1.FindControl(...).LoadData();
 
Or another solution would be in
WebUserControl Page_LoadComplete do
if (this.Visible) { LoadData(); }
 
Thanks for the pointers again.

I've tried putting the Page_LoadComplete into the WebUserControl but this never gets run.

So I've used the code to iterate through the WebUserControls (within default.aspx) and called the 'LoadData()' there.

Now, when I click the button in the grid for the first time I get the following:
(1). Default.aspx Page_Load fires...
(2). Default.aspx Page_LoadComplete fires...

... and on the second time:
(1). Default.aspx Page_Load fires...
(2). WebUserControl GridView1_RowCommand fires...
(3). Default.aspx Page_LoadComplete fires...

So - again it's only the second time when the clicking of the button actually operates successfully...
 
Does the RowCommand ever get fired when you click the button for the first time?
 
Looks like the webuser control doesn't have a LoadComplete EventHandler.

Did you try the first solution with the boolean that you set to true in the event handler. Then check the bool in LoadComplete?
 
Am I able to hook up the WebUserControl to use a PageLoad_Complete code block?

And yes, the idea of using a boolean to run the WebUserControl's 'LoadData' was implemented - but did not work (as per my details above - when this was put in the default.aspx Page_LoadComplete code).
 
A webusercontrol has a slightly different cycle then a page.

Maybe you can add the LoadData() to the prerender.

Try adding it to Page_PreRenderComplete(object sender, EventArgs e).

I cannot think of anything else that is preventing the event from firing unfortunately.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top