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

來源:互聯網
上載者: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 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


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 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


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 
   07-11-2005, 12:10 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 
   07-11-2005, 8:50 PM
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.
    <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


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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.