Having trouble with script to reload parent page?

J

Josh Nikle

I have a webform containing a GridView control "GridView1" and
usercontrol which is also a GridView "GridView2." I have a button
column in the usercontrol. The code behind that button adds an entry
to my database, which it's doing fine, but I can't get GridView1 to
show the updated data unless I browse to another page and then come
back.

I've been reading about using java script to accomplish this, but I
must be doing something wrong. I don't get any errors, GridView2 just
gets updated and GridView1 doesn't change. I've also noticed that
clicking refresh doesn't update GridView1 either, so maybe the java
script isn't the answer?

I'm very new to C#/ASP.NET, so could someone provide a sample of where
to put the script? The usercontrol (which is where I believe the
script needs to go?) uses a PreRender event rather than a PageLoad
event, if that makes any difference. Perhaps that's the issue? I've
read the numerous posts on the subject here and have been to pretty
much every blog that comes up in my browser, but no luck. I'm sure
it's something simple.

Any help is greatly appreciated.

-Josh Nikle
 
G

Guest

There are a couple ways I generally connect user controls to objects in the
parent page. Other responders may have other options.

1. You can pass the System.Web.UI.Page object from the page to the
controller as a property.
2. You can pass the GridView object from the page to the controller as
a property.

Either of these solutions allow you to update the GridView in the parent
page from within the user control, or:

3. You can create an event in the user control and subscribe to the
event in the parent page. This is a more robust solution, making your user
control more loosely coupled to the page and more usable elsewhere.

The trade off of #3 is that it takes more coding and testing up front. If
you know your user control will only ever be used in this page and in the way
you are using it now, you may want to choose option 1 or 2. If you're not
sure, and we're usually not as sure as we think we are - that's why good OOP
practices exist in the first place - then you may want to choose option 3.

Dale
 
G

Guest

Whoops. I guess I'm brain dead today. In options 1 and 2, replace
"controller" with "control".

Dale
 
J

Josh Nikle

Thanks for the reply. I use the GridView2 control on the pages, so
looks like option 3 is for me. Can you elaborate on that one for me?
Like I said, I'm new to ASP.NET/C#, and I'm not entirely sure what I
should have in the event in the user control or how or when I'd call it
from the parent page.

Josh Nikle
 
J

Josh Nikle

can anyone help me out with this?

josh nikle



Josh said:
Thanks for the reply. I use the GridView2 control on the pages, so
looks like option 3 is for me. Can you elaborate on that one for me?
Like I said, I'm new to ASP.NET/C#, and I'm not entirely sure what I
should have in the event in the user control or how or when I'd call it
from the parent page.

Josh Nikle
 
G

Guest

Josh,

I apologize for not responding earlier - I missed the notification on this
one. I don't know if you solved it yet, but here's my reply, just in case.

Here is a simple example of adding events to a user control. This is from a
user control that consists of a Save button and a Reset button. The page can
subscribe to the events on the control. This code goes in the user control
class:


#region Button Clicked Events
public event System.EventHandler SaveClicked;
public event System.EventHandler ResetClicked;

void btnSave_Click(object sender, System.EventArgs e)
{
if (SaveClicked != null)
{
this.SaveClicked(sender, e);
}
}

private void btnReset_Click(object sender, System.EventArgs e)
{
if (ResetClicked != null)
{
this.ResetClicked(sender, e);
}
}
#endregion

You can modify this by creating your events as type
System.Web.UI.WebControls.GridViewCommandEventHandler instead of
System.EventHandler. You could then pass back the gridview as the sender
argument and the item as the e argument.

HTH

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top