views:

32

answers:

2

Hello,

I'm trying to update values from a xml file into textboxes. I have this javascript being called in the Page_Load event

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", sb.ToString(), true);

I click the continue button which does a post back but the values are not updated until I refresh the page again which makes me think the js isn't being run until after the page is returned. I'm wondering how to have the values updated when the page is refreshed after the button postback.

thanks

A: 

The js will be rendered out to the browser after every postback so long as your RegisterStartupScript is not in a If(!IsPostback) block. Please post your javascript.

Ben Robinson
I know it's being rendered but at which point is it run. It seems to run after the page is displayed because when I refresh, the updated values are displayed.
That entirely depends on the javascript. If sb.ToString() outputs a function definition then it runs when something calls the function. If it was straight forward commands e.g. alert('Hello World') it would run after every page load. Post your javascript.
Ben Robinson
+1  A: 

I would re-evaluate why you're doing this with JavaScript. I think this would be a very trivial thing to do in the code behind of the asp.net page. something like:

var xmlSource = contacts.Load(@"myxmldoc.xml");
mytextbox.Text = (from c in xmlSource.contact
        where c.contactId < 4
        select c.firstName + " " + c.lastName).FirstOrDefault();

If you're committed to using JavaScript.. I'm not sure why you dont just write the javascript directly in the aspx code. I only use registerstartupscript if my javascript is dynamic in some way. Usually its easy enough to pass dynamic values to your page using public properties as well for the javascript to use.

itchi