Want to add an onload JavaScript based on a condition from server-side code

Source: Internet
Author: User
Http://forums.asp.net/983001/ShowPost.aspx

Want to add an onload JavaScript based on a condition from server-side code
I want to test for a condition in page_load event for a page and if that is true then I want add JavaScript For the form's onload event. the javascript is a simple alert message I. e. alert ("You have already submitted this form. you cannot re-submit it. "). how can I do this in server-side code?

sun21170
Report
, AM
Sonukapoor


Montreal/Canada
Posts 1,870

Preferred programming language: Visual Basic. net

Re: Want to add an onload JavaScript based on a condition from server-side code

each webpage usually has a htmlform-by default the ID is form1, so you coshould basically do the following in page_load:

protected sub page_load (byval sender as object, byval e as system. eventargs) handles me. load
If ispostback then
form1.attributes. add ("onLoad", "javascript: Alert ('you have already submitted this form. you cannot re-submit it. ') ")
end if
end sub

did it help? Rate it!
Sonu Kapoor [MCP]
Website: http://KapoorSolutions.com/default.aspx
blog: http://KapoorSolutions.com/blog
Report
, AM
sun21170


Dallas, USA
posts 380

preferred programming language: Visual Basic. net

re: want to add an onload JavaScript based on a condition from server-side code

that doesn't help because there is no 'form1 'object available in page_load event, even though the form name is 'form1 'in my ASPX page.

sun21170
Report
, AM
Sonukapoor


Montreal/Canada
Posts 1,870

Preferred programming language: Visual Basic. net

re: want to add an onload JavaScript based on a condition from server-side code
can you show me please your HTML and the page_load code?

did it help? Rate it!
Sonu Kapoor [MCP]
Website: http://KapoorSolutions.com/default.aspx
blog: http://KapoorSolutions.com/blog
Report
,
Sun21170


Dallas, USA
Posts 380

Preferred programming language: Visual Basic. net

Re: Want to add an onload JavaScript based on a condition from server-side code

HTML is:

<% @ Page Language = "VB" autoeventwireup = "false" codebehind = "webform1.aspx. VB" inherits = "oneclicktester. webform1" %>
<% @ Register tagprefix = "mboc" namespace = "metabuilders. webcontrols" assembly = "metabuilders. webcontrols. oneclick" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> webform1 </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "Visual Basic. Net 7.1" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server" onsubmit = "If (document. forms [0]. submitted. value = '1') {alert ('You cannot resubmit the form'); Return false;} else {document. forms [0]. submitted. value = '1'; return true;} ">
<Asp: button id = "button1" runat = "server" text = "button" enableviewstate = "true"> </ASP: button> <asp: label id = "result" runat = "server" enableviewstate = "false"> </ASP: Label> <mboc: oneclick id = "oneclick2" runat = "server"> </mboc: oneclick> <input id = "submitted" style = "Z-INDEX: 101; left: 40px; position: absolute; top: 56px "type =" hidden"
Value = "0" runat = "server"> </form>
</Body>
</Html>

Code behind is:

Imports system. Data
Imports system. Data. sqlclient
Public class webform1
Inherits system. Web. UI. Page

# Region "Web Form Designer generated code"

'this call is required by the web form designer.
private sub initializecomponent ()
me. sqlconnection1 = new system. data. sqlclient. sqlconnection
'
'sqlconnection1
'
me. sqlconnection1.connectionstring = "workstation id = Sunil; packet size = 4096; Integrated Security = sspi; Data Source =" "Sunil" & _
"\ vsdotnet ""; persist Security info = false; initial catalog = echotangodb "

End sub
Protected withevents button1 as system. Web. UI. webcontrols. Button
Protected withevents result as system. Web. UI. webcontrols. Label
Protected withevents oneclick2 as metabuilders. webcontrols. oneclick
Protected withevents sqlconnection1 as system. Data. sqlclient. sqlconnection
Protected withevents submitted as system. Web. UI. htmlcontrols. htmlinputhidden

'Note: The following placeholder declaration is required by the web form designer.
'Do not delete or move it.
Private designerplaceholderdeclaration as system. Object

Private sub page_init (byval sender as system. Object, byval e as system. eventargs) handles mybase. init
'Codegen: This method call is required by the web Form Designer
'Do not modify it using the code editor.
Initializecomponent ()
End sub

# End Region

Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
'Put user code to initialize the page here

Me. Submitted. value = "0"
If ispostback then
If oneclick2.isvalid then
Session ("timessubmitted") = ctype (Session ("timessubmitted"), integer) + 1
System. Threading. thread. Sleep (timespan. fromseconds (10 ))
Dim cmd as new sqlcommand ("Update Table1 set clicked =" + CSTR (ctype (Session ("timessubmitted"), integer), me. sqlconnection1)
Me. sqlconnection1.open ()
Cmd. executenonquery ()
Me. sqlconnection1.close ()
End if
Else
Session ("timessubmitted") = 0
End if
Result. Text = "Operation completed successfully" + CSTR (Session ("timessubmitted "))
End sub

Private sub button#click (byval sender as object, byval e as system. eventargs) handles button1.click

End sub
End Class

Sun21170
Report
, AM
Sonukapoor


Montreal/Canada
Posts 1,870

Preferred programming language: Visual Basic. net

re: want to add an onload JavaScript based on a condition from server-side code
try:

If page. ispostback then
dim form as htmlform = page. findcontrol ("form1")
form. attributes. add ("onLoad", "alert ('test')")
end if

did it help? Rate it!
Sonu Kapoor [MCP]
Website: http://KapoorSolutions.com/default.aspx
blog: http://KapoorSolutions.com/blog
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.