473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to ref web form controls in client side event handler?

I have a drop down list with an associated label. I want to take the
selectedValue of the list and populate the text of the label when the value
in the list is changed. I havent figured out how to access the controls
through the script, however.
Here's what I have:

In the ASPX file:
<script>
function OnSelectedIndexChanged()
{window.document.Form1.lblLogIDDate.Text =
window.document.Form1.ddlLogID.SelectedValue;}
</script>
with the following in the Page_Load function:
Me.ddlLogID.Attributes.Add("onchange", "OnSelectedIndexChanged();")

I know the handler fires, but I keep getting errors when it does.
Variations on "xxx" is null or not a valid object. In the above example it
would be ""window.document.Form1.ddlLogIDDate" is null or not a valid
object"

I've tried window.document.Forms(0).ddlLogIDDate,
window.document.Form1("lblLogIDDate"), and other variations but cant find
the right combo.

Can anyone help?
Nov 18 '05 #1
3 1323
Hi Eric,

It looks like you're trying to mix server-side events and client-side code
in a way that is going to get you into trouble. You'd be best advised to use
the server-side event unless causing a postback is really a problem for you.

Here's an example of what I think you might be trying to accomplish:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
Dim intCounter As Integer
For intCounter = 0 To 5
ddlLogID.Items.Add _
(Now.AddDays(intCounter).ToLongDateString)
Next
End If
End Sub

Private Sub ddlLogID_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ddlLogID.SelectedIndexChanged
lblLogIDDate.Text = ddlLogID.SelectedItem.Text
End Sub

<P>
<asp:Label id="lblLogIDDate" runat="server"></asp:Label></P>
<P>
<asp:DropDownList id="ddlLogID" runat="server"
AutoPostBack="True"></asp:DropDownList></P>

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

"Eric" <er**@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have a drop down list with an associated label. I want to take the
selectedValue of the list and populate the text of the label when the
value
in the list is changed. I havent figured out how to access the controls
through the script, however.
Here's what I have:

In the ASPX file:
<script>
function OnSelectedIndexChanged()
{window.document.Form1.lblLogIDDate.Text =
window.document.Form1.ddlLogID.SelectedValue;}
</script>
with the following in the Page_Load function:
Me.ddlLogID.Attributes.Add("onchange", "OnSelectedIndexChanged();")

I know the handler fires, but I keep getting errors when it does.
Variations on "xxx" is null or not a valid object. In the above example
it
would be ""window.document.Form1.ddlLogIDDate" is null or not a valid
object"

I've tried window.document.Forms(0).ddlLogIDDate,
window.document.Form1("lblLogIDDate"), and other variations but cant find
the right combo.

Can anyone help?


Nov 18 '05 #2
on the client there are no .Text or .SelectedValue properties. aslo if
lblLogIDDate is label, then its rendered as a span.

<script>
function OnSelectedIndexChanged(e)
{
var text = e.options[e.selectedIndex].value;

// if lblLogIDDate is a textbox

document.getElementById('lblLogIDDate').value = text;

// if lblLogIDDate is a label

document.getElementById('lblLogIDDate').innerText = text;
}

</script>

use the following in the Page_Load function:
Me.ddlLogID.Attributes.Add("onchange", "OnSelectedIndexChanged(this);")


"Eric" <er**@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| I have a drop down list with an associated label. I want to take the
| selectedValue of the list and populate the text of the label when the
value
| in the list is changed. I havent figured out how to access the controls
| through the script, however.
| Here's what I have:
|
| In the ASPX file:
| <script>
| function OnSelectedIndexChanged()
| {window.document.Form1.lblLogIDDate.Text =
| window.document.Form1.ddlLogID.SelectedValue;}
| </script>
| with the following in the Page_Load function:
| Me.ddlLogID.Attributes.Add("onchange", "OnSelectedIndexChanged();")
|
| I know the handler fires, but I keep getting errors when it does.
| Variations on "xxx" is null or not a valid object. In the above example
it
| would be ""window.document.Form1.ddlLogIDDate" is null or not a valid
| object"
|
| I've tried window.document.Forms(0).ddlLogIDDate,
| window.document.Form1("lblLogIDDate"), and other variations but cant find
| the right combo.
|
|
|
| Can anyone help?
|
|
Nov 18 '05 #3
Try the following code in the ASPX page
// Since label is rendered as Span element, to display the selected value in
label,
// set the innerText property.

<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 167px; POSITION: absolute;
TOP: 346px" runat="server" Width="252px"></asp:Label>
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 102; LEFT: 54px;
POSITION: absolute; TOP: 300px" runat="server" onchange="callChange();">
<asp:ListItem Value="Item1">Item1</asp:ListItem>
<asp:ListItem Value="Item2">Item2</asp:ListItem>
<asp:ListItem Value="Item3">Item3</asp:ListItem>
</asp:DropDownList>

<script language="javascript">
function callChange()
{
var index = document.getElementById("DropDownList1").selectedI ndex;
var str = document.getElementById("DropDownList1")[index].value;
document.getElementById("Label2").innerText = str;
}
</script>
"Eric" wrote:
I have a drop down list with an associated label. I want to take the
selectedValue of the list and populate the text of the label when the value
in the list is changed. I havent figured out how to access the controls
through the script, however.
Here's what I have:

In the ASPX file:
<script>
function OnSelectedIndexChanged()
{window.document.Form1.lblLogIDDate.Text =
window.document.Form1.ddlLogID.SelectedValue;}
</script>
with the following in the Page_Load function:
Me.ddlLogID.Attributes.Add("onchange", "OnSelectedIndexChanged();")

I know the handler fires, but I keep getting errors when it does.
Variations on "xxx" is null or not a valid object. In the above example it
would be ""window.document.Form1.ddlLogIDDate" is null or not a valid
object"

I've tried window.document.Forms(0).ddlLogIDDate,
window.document.Form1("lblLogIDDate"), and other variations but cant find
the right combo.

Can anyone help?

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Pete | last post by:
I have form/select which executes a function using onchange. No problem. However, when I validate the page with a strict HTML 4.01 doctype at http://validator.w3.org, it demands either an action or...
1
by: TB | last post by:
Howdy... I have an asp.net web form with an ImageButton control on the form. When clicked, the ImageButton's click event handler is supposed to pass X and Y coordinates of the mouse click's...
1
by: Zoe Hart | last post by:
I know that I can use a control's Attributes collection to specify client-side event handlers. I want to add a simple javascript function to pop up a confirmation message for a command button. The...
4
by: Jiho Han | last post by:
What is the best way to check whether the page is simply a postback or the form has been submit with the intention of doing something? In the olden days, I used to check for a form field name...
3
by: Casper Skovgaard | last post by:
I have a form with several fields (textbox, radio, checkbox), when the user submits the form I want to check if the user have changed anything. The check should be performed in the code-behind...
3
by: jeff29_b | last post by:
I am having a strange problem on a web form. I have an image button with an OnClick event handler. When I click the image the event isn't being called in the code behind when browsing in firefox....
3
by: UJ | last post by:
Can somebody help me please? I have two 'sets' of controls on a page. There are two radio buttons that enable the two sets (when radiobutton A is checked, set A should be enabled, B, Set B should...
4
by: Lee Chapman | last post by:
Hi, Can anyone tell me why in the code below, the call to ClearChildViewState() has no effect? To paraphrase the code: I'm using view state. I have a textbox and a submit button (and a label...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.