Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 457
  • Last Modified:

Syntax in RegisterStartupScript

The first if works, but the else if does not on a click event. Where is the syntax wrong in:

        Dim Checkbox1_click As String = "<script language='javascript'>" & _
            "var elemCheckbox1 = document.getElementById('Checkbox1'); " & _
            "var elemTexbox1 = document.getElementById('TextBox1'); " & _
            "if(elemCheckbox1='checked'); {elemTexbox1.value='Yes';}" & _
            "else if(elemCheckbox1=''); {elemTexbox1.value='No';}" & _
            "</script>"

        ClientScript.RegisterStartupScript(Page.GetType(), "Startup", Checkbox1_click)


Thanks.
0
barnesco
Asked:
barnesco
  • 2
  • 2
1 Solution
 
Sammy AgeilCommented:
Use this

Dim StrScript As New StringBuilder("")
        With StrScript
            .Append("<script type=""text/javascript"">")
            .Append(vbNewLine)
            .Append("var elemCheckbox1 = document.getElementById('Checkbox1'); ")
            .Append(vbNewLine)
            .Append("var elemTexbox1 = document.getElementById('TextBox1'); ")
            .Append(vbNewLine)
            .Append("if(elemCheckbox1='checked'); ")
            .Append(vbNewLine)
            .Append("{elemTexbox1.value='Yes';}")
            .Append(vbNewLine)
            .Append("else if(elemCheckbox1='');")
            .Append(vbNewLine)
            .Append("{elemTexbox1.value='No';}")
            .Append(vbNewLine)
            .Append("</script>")

        End With
        ClientScript.RegisterStartupScript(Page.GetType(), "Startup", StrScript.ToString)


the HTML Output will be

<script type="text/javascript">
var elemCheckbox1 = document.getElementById('Checkbox1');
var elemTexbox1 = document.getElementById('TextBox1');
if(elemCheckbox1='checked');
{elemTexbox1.value='Yes';}
else if(elemCheckbox1='');
{elemTexbox1.value='No';}
</script>

Good luck
0
 
Edwin_CCommented:
       Dim Checkbox1_click As String = "<script language='javascript'>" & _
            "var elemCheckbox1 = document.getElementById('Checkbox1'); " & _
            "var elemTexbox1 = document.getElementById('TextBox1'); " & _
            "if(elemCheckbox1='checked'); {elemTexbox1.value='Yes';}" & _            <<< modify these 2 lines
            "else if(elemCheckbox1=''); {elemTexbox1.value='No';}" & _                  <<<
            "</script>"

to
            "if(elemCheckbox1=='checked') {elemTexbox1.value='Yes';} " & _            
            "else {if(elemCheckbox1=='') {elemTexbox1.value='No';}}" & _                  
0
 
barnescoAuthor Commented:
Neither one works, but just so people don't think I'm crazy, here's the complete code:

HTML:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test6.aspx.vb" Inherits="Test6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
   
</head>
<body>
    <form id="form1" runat="server">

        <asp:CheckBox ID="CheckBox1" runat="server" /><br />
        <br />
       
        <asp:TextBox ID="TextBox1" runat="server" Width="18px"></asp:TextBox><br />
        <br />
       
    </form>
</body>
</html>


AND CODE BEHIND:

Partial Class Test6
    Inherits System.Web.UI.Page

    Protected Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

        Dim Checkbox1_click As String = "<script language='javascript'>" & _
     "var elemCheckbox1 = document.getElementById('Checkbox1'); " & _
     "var elemTexbox1 = document.getElementById('TextBox1'); " & _
     "if(elemCheckbox1=='checked') {elemTexbox1.value='Yes';} " & _
     "else {if(elemCheckbox1=='') {elemTexbox1.value='No';}}" & _
     "</script>"

        ClientScript.RegisterStartupScript(Page.GetType(), "Startup", Checkbox1_click.ToString)

    End Sub
End Class

Thanks.
0
 
Edwin_CCommented:
Hi

I changed the last line to

        Page.RegisterStartupScript("Startup", Checkbox1_click)

and then the webform ran successfully.

However your code cannot do any visible change. If I modify the script to

        Dim Checkbox1_click As String = "<script language='javascript'>" & _
      "var elemCheckbox1 = document.getElementById('Checkbox1'); " & _
      "var elemTexbox1 = document.getElementById('TextBox1'); " & _
      "if (elemCheckbox1.checked==true) {elemTexbox1.value='Yes';} " & _
      "else {elemTexbox1.value='No';}" & _
      "</script>"

and then have AutoPostBack="True" in the checkbox.  The effect can be seen upon each postback after the checkbox is checked or unchecked
Note: the javascript is executed only when the checkbox is changed AND the page is loaded/postback.

If you still cannot figure out, please tell me what you want to archive.

Edwin

0
 
Sammy AgeilCommented:
Edwin,
ClientScript.RegisterStartupScript(Page.GetType(), "Startup", Checkbox1_click)
will only work in Asp.net 2.0, its a recommended replacement to Page.RegisterStartupScript("Startup", Checkbox1_click)

I am still at loss at what is the goal for this script and just like you said without autopostback it will not do nothing other than sit in the HTML


0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now