RegisterClientScript

C

collie

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

Jim Cheshire [MSFT]

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
(e-mail address removed)

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

--------------------
From: (e-mail address removed) (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: <[email protected]>
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: (e-mail address removed)
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top