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

http://forums.asp.net/983001/ShowPost.aspx


Want to add an onload javascript based on a condition from server-side code
Reply Quote
I want to test for a condition  in Page_load event for a page and if that is true then I want to 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 
   07-10-2005, 12:26 AM
SonuKapoor is not online. Last active: 7/29/2005 9:01:54 PM SonuKapoor

Top 50 Posts
Montreal/Canada
Posts 1,870

Preferred Programming Language: Visual Basic .NET

Moderator
Re: Want to add an onload javascript based on a condition from server-side code
Reply Quote

Each webpage usually has a HtmlForm - by default the ID is form1, so you could 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 
   07-10-2005, 12:42 AM
sun21170 is not online. Last active: 7/10/2005 1:56:39 PM sun21170

Top 500 Posts
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
Reply Quote

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 
   07-11-2005, 12:10 AM
SonuKapoor is not online. Last active: 7/29/2005 9:01:54 PM SonuKapoor

Top 50 Posts
Montreal/Canada
Posts 1,870

Preferred Programming Language: Visual Basic .NET

Moderator
Re: Want to add an onload javascript based on a condition from server-side code
Reply Quote
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 
   07-11-2005, 8:50 PM
sun21170 is not online. Last active: 7/10/2005 1:56:39 PM sun21170

Top 500 Posts
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
Reply Quote

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.
    <System.Diagnostics.DebuggerStepThrough()> 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 Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
End Class


sun21170
   Report 
   07-12-2005, 9:16 AM
SonuKapoor is not online. Last active: 7/29/2005 9:01:54 PM SonuKapoor

Top 50 Posts
Montreal/Canada
Posts 1,870

Preferred Programming Language: Visual Basic .NET

Moderator
Re: Want to add an onload javascript based on a condition from server-side code
Reply Quote
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

你可能感兴趣的:(JavaScript)