开发者

Javascript update before page loads in ASP.NET

开发者 https://www.devze.com 2023-01-01 06:34 出处:网络
I\'m trying to update values from a xml file into textboxes. I have this javascript being called in the Page_Load event

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 co开发者_StackOverflow中文版ntinue 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消