RegisterClientScript

Discussion in 'ASP .Net Building Controls' started by collie, Dec 1, 2003.

  1. collie

    collie Guest

    Hi,

    I have a dropdownlist in vb.net (server controls). Based on the user's
    selection from the list of items certain textboxes have to be
    disabled. I wrote a code but it only seems to work once the page is
    loaded (which is also what i need). I need it to work also after the
    page is loaded and a user selects an item.
    I tried putting the code also in Private Sub
    List1_SelectedIndexChanged but it doesn't work

    This is my code in codebehind (aspx.vb):
    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
    If Not Page.IsPostBack Then
    Dim li As New ListItem()
    Dim cities(3) As String
    cities(0) = "Pretoria"
    cities(1) = "JHB"
    cities(2) = "Benoni"
    Dim i As Integer
    For i = 0 To UBound(cities) - 1
    List1.Items.Add(cities(i))

    Next


    End If

    RegisterStartupScript("start", _
    "<script>disableTextBox ();</script>")

    End Sub

    _______________________________
    The code in aspx:

    <script language="javascript">
    function disableTextBox()

    {

    strSelectedItem =
    document.Form1.List1.options[document.Form1.List1.selectedIndex].text

    if(strSelectedItem == "Pretoria")

    {

    document.Form1.Text1.disabled=true;
    document.Form1.txt2.disabled =false;

    }

    else if(strSelectedItem == "Benoni")

    {

    document.Form1.txt2.disabled = true;
    document.Form1.Text1.disabled=false;
    }

    }
    </script>
    </HEAD>
     
    collie, Dec 1, 2003
    #1
    1. Advertising

  2. Hi,

    The situation you describe does not require RegisterStartupScript. The
    only time you would need to generate your client-side script on the server
    is if you do not know at design-time what needs to be in your script. In
    your case, you already know all of your script when you are designing the
    page. Therefore, what you should be doing is putting all of your script in
    the ASPX page itself.

    To execute the script when the page loads, place the call to the script in
    the "onload" of the <body> tag.

    To execute the script when you select an item in your ListBox, you just
    need to hook the OnSelectedIndexChanged client-side event like so:

    <asp:ListBox id="ListBox1" OnSelectedIndexChanged="disableTextBox();" ....

    Jim Cheshire, MCSE, MCSD [MSFT]
    Developer Support
    ASP.NET


    This post is provided as-is with no warranties and confers no rights.

    --------------------
    >From: (collie)
    >Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    >Subject: RegisterClientScript
    >Date: 1 Dec 2003 08:35:28 -0800
    >Organization: http://groups.google.com
    >Lines: 65
    >Message-ID: <>
    >NNTP-Posting-Host: 81.218.123.210
    >Content-Type: text/plain; charset=ISO-8859-1
    >Content-Transfer-Encoding: 8bit
    >X-Trace: posting.google.com 1070296528 11513 127.0.0.1 (1 Dec 2003

    16:35:28 GMT)
    >X-Complaints-To:
    >NNTP-Posting-Date: Mon, 1 Dec 2003 16:35:28 +0000 (UTC)
    >Path:

    cpmsftngxa06.phx.gbl!TK2MSFTNGXA06.phx.gbl!TK2MSFTNGXA05.phx.gbl!TK2MSFTNGP0
    8.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!news-spur1.maxwell.syr.edu!
    news.maxwell.syr.edu!postnews1.google.com!not-for-mail
    >Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.buildingcontrols:8311
    >X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
    >
    >Hi,
    >
    >I have a dropdownlist in vb.net (server controls). Based on the user's
    >selection from the list of items certain textboxes have to be
    >disabled. I wrote a code but it only seems to work once the page is
    >loaded (which is also what i need). I need it to work also after the
    >page is loaded and a user selects an item.
    >I tried putting the code also in Private Sub
    >List1_SelectedIndexChanged but it doesn't work
    >
    >This is my code in codebehind (aspx.vb):
    >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
    > If Not Page.IsPostBack Then
    > Dim li As New ListItem()
    > Dim cities(3) As String
    > cities(0) = "Pretoria"
    > cities(1) = "JHB"
    > cities(2) = "Benoni"
    > Dim i As Integer
    > For i = 0 To UBound(cities) - 1
    > List1.Items.Add(cities(i))
    >
    > Next
    >
    >
    > End If
    >
    > RegisterStartupScript("start", _
    > "<script>disableTextBox ();</script>")
    >
    > End Sub
    >
    > _______________________________
    >The code in aspx:
    >
    ><script language="javascript">
    > function disableTextBox()
    >
    >{
    >
    > strSelectedItem =
    >document.Form1.List1.options[document.Form1.List1.selectedIndex].text
    >
    > if(strSelectedItem == "Pretoria")
    >
    > {
    >
    > document.Form1.Text1.disabled=true;
    > document.Form1.txt2.disabled =false;
    >
    > }
    >
    > else if(strSelectedItem == "Benoni")
    >
    > {
    >
    > document.Form1.txt2.disabled = true;
    > document.Form1.Text1.disabled=false;
    > }
    >
    >}
    > </script>
    > </HEAD>
    >
     
    Jim Cheshire [MSFT], Dec 1, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Boris Twila

    registerclientscript

    Boris Twila, Nov 1, 2006, in forum: ASP .Net
    Replies:
    11
    Views:
    612
    Laurent Bugnion
    Nov 3, 2006
  2. Fritz the Cat

    registerclientscript

    Fritz the Cat, Mar 29, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    311
    Mark Rae
    Mar 29, 2007
  3. DNB
    Replies:
    2
    Views:
    1,824
    Michael Nemtsev
    Dec 13, 2007
  4. Rain
    Replies:
    0
    Views:
    375
  5. Jawahar

    RegisterClientScript from a User COntrol

    Jawahar, Aug 19, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    136
    Marcel
    Aug 22, 2005
Loading...

Share This Page