PDA

View Full Version : call javascript function to the page load event


jmohan
05-17-2008, 01:35 PM
Hi everyone,

I am developing a website using asp.net with C#.

And I developed a toolbar using javascript like google and yahoo toolbar which is placed in the browser separately.

Toolbar has the toolbar id.

I need to capture the toolbar id and save into the mysql database during the page load.

So that, I put the javascript funcation called myFunc() in the Source file of the page to capture the toolbar id.

I need to save the toolbar id into the mysql database and so I applied the coding to insert the toolbarid into the database.

When I execute the coding, first of all, the page is loaded, then only the javascript is executed. Therefore, I am unable to get the toolbar id to the variable assigned in the page load.

Please help me regarding this.

How do I call the javascript function to the page load event.


Thanks in Advance,

J. Mohan.:)

chump2877
05-18-2008, 05:31 AM
How do I call the javascript function to the page load event.

You can use JavaScript to insert the toolbar ID value into a HiddenField control or hidden input HTML server control. Then, when the page posts back to the server, the value of the server control should be available to you in the Page_Load event. And don't forget to sanitize any data before you insert it into your database -- even "hidden" controls can be tampered with.

jmohan
05-19-2008, 11:17 AM
You can use JavaScript to insert the toolbar ID value into a HiddenField control or hidden input HTML server control. Then, when the page posts back to the server, the value of the server control should be available to you in the Page_Load event. And don't forget to sanitize any data before you insert it into your database -- even "hidden" controls can be tampered with.





Could you please give me some examples.


J. Mohan

chump2877
05-23-2008, 02:27 AM
I dunno, something like this (forget about the JavaScript - my bad):

.aspx file
<!-- lets pretend the following DIV is your toolbar -->
<div id="toolbarID"></div>

<asp:hiddenfield id="hfToolbarIdValue"
value="toolbarID"
runat="server"/>

.cs code-behind file
protected void Page_Load(object sender, EventArgs e)
{
// Access toolbar id value via "hfToolbarIdValue.Value"
}

OR, you could just make your toolbar an HTML server control:

.aspx file
<!-- let's pretend the following DIV is your toolbar -->
<div id="ToolbarID" runat="server"></div>

.cs code-behind file
protected void Page_Load(object sender, EventArgs e)
{
// Access toolbar id value via 'ToolbarID.Attributes["id"]'
}