473,405 Members | 2,344 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,405 software developers and data experts.

How to have a value from DropDownList.selectedItem on the Client Side?

Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";

At the script on Windows Form, I added this function to have the value of
DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID
%>").innerText)
}
</script>

Instead of having only one result selected-value from this DropDownList, the
pop-up messages showing entire data that contained on this DropDownList, how
to have the selected value/item only on onChange event with this client-side
script?

Thx,
Julius F


Nov 19 '05 #1
6 5496
Hi,

The problem is that innerText retrieves the text between the start and end
tags of the <select> element. I believe you need to get only the selected
item's value or text. See inline:

"Julius Fenata" <id****@gmx.net> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";
DropDownList1.Attributes["onchange"] =
"javascript:GenerateArticleID(this.options(this.se lectedIndex));";

At the script on Windows Form, I added this function to have the value of
DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID
%>").innerText)
}
function GenerateArticleID(SelectedItem)
{
alert("Value: " + SelectedItem.value + "\nText: " + SelectedItem.text);
}
</script>

Instead of having only one result selected-value from this DropDownList, the pop-up messages showing entire data that contained on this DropDownList, how to have the selected value/item only on onChange event with this client-side script?

Thx,
Julius F

Hope this helps
Martin Dechev
ASP.NET MVP
Nov 19 '05 #2
Julius,

A ddl translates to an html <select>. You should follows the DHTML object
model for <select>:

DropDownList1.Attributes["onChange"] = "GenerateArticleID(this)";

<script language="JavaScript">
function GenerateArticleID(ddl){
javascript:alert(ddl.options(ddl.selectedIndex).va lue)
}
</script>

Eliyahu

"Julius Fenata" <id****@gmx.net> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";

At the script on Windows Form, I added this function to have the value of
DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID
%>").innerText)
}
</script>

Instead of having only one result selected-value from this DropDownList, the pop-up messages showing entire data that contained on this DropDownList, how to have the selected value/item only on onChange event with this client-side script?

Thx,
Julius F

Nov 19 '05 #3
Problem solved,

Thx a lot Martin, for your prompt response & it is a wonderfully help for
me...

Regards,
Julius Fenata
"Martin Dechev" <de*******@hotmail.com> wrote in message
news:eS**************@TK2MSFTNGP12.phx.gbl...
Hi,

The problem is that innerText retrieves the text between the start and end
tags of the <select> element. I believe you need to get only the selected
item's value or text. See inline:

"Julius Fenata" <id****@gmx.net> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";


DropDownList1.Attributes["onchange"] =
"javascript:GenerateArticleID(this.options(this.se lectedIndex));";

At the script on Windows Form, I added this function to have the value of DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID %>").innerText)
}


function GenerateArticleID(SelectedItem)
{
alert("Value: " + SelectedItem.value + "\nText: " + SelectedItem.text);
}
</script>

Instead of having only one result selected-value from this DropDownList,

the
pop-up messages showing entire data that contained on this DropDownList,

how
to have the selected value/item only on onChange event with this

client-side
script?

Thx,
Julius F

Hope this helps
Martin Dechev
ASP.NET MVP

Nov 19 '05 #4
Dear Eliyahu & Martin,

Just recently notice that ASP .Net need some understanding about other
programming languange: DHTML Object Model.., but how do I can get
resources/tutorials about this scripting ability?

I have question from your code, what is ddl for? Is that a container
(variable) to accept passed parameter from (this)? If ddl is a variable why
there isn't available a data type whose define ddl variable?

The problem is solved temporarily, now I have value from DropDownList which
I selected. But requirements is now extend, because actually I have to
generate ID from many DropDownList.

The scenario is here, I have 3 DropDownList, whichever onChange event
triggered on any DropDownList, the GenerateArticleID() function on
client-side collect the values from every DropDownList and concate it into
one ID.

e.g:

DropDownList1 -> If selected, then take value selected from this
DropDownList (DropDownList1), collect value from DropDownList2&3, concate it
into one ID, and assign it into Label.text

This event is applied too to other DropDownList...

DropDownList2 -> If selected, then take value selected from this
DropDownList (DropDownList2), collect value from DropDownList1&3, concate it
into one ID, and assign it into Label.text

DropDownList3 -> If selected, same activity.

Now.., how can I collect the other DropDownList value (passive one), after
I've got value from selected DropDownList? Or, how ASP .Net recognize value
from each server control?

Thx a lot,
Julius F

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
Julius,

A ddl translates to an html <select>. You should follows the DHTML object
model for <select>:

DropDownList1.Attributes["onChange"] = "GenerateArticleID(this)";

<script language="JavaScript">
function GenerateArticleID(ddl){
javascript:alert(ddl.options(ddl.selectedIndex).va lue)
}
</script>

Eliyahu

"Julius Fenata" <id****@gmx.net> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";

At the script on Windows Form, I added this function to have the value of DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID %>").innerText)
}
</script>

Instead of having only one result selected-value from this DropDownList,

the
pop-up messages showing entire data that contained on this DropDownList,

how
to have the selected value/item only on onChange event with this

client-side
script?

Thx,
Julius F


Nov 19 '05 #5
> Just recently notice that ASP .Net need some understanding about other
programming languange: DHTML Object Model.., but how do I can get
resources/tutorials about this scripting ability? MSDN Library was good enough for me. You should better ask for
recommendations in a separate thread.
I have question from your code, what is ddl for? Is that a container
(variable) to accept passed parameter from (this)? Yes, it is.
If ddl is a variable why
there isn't available a data type whose define ddl variable? Javascript is a language with no types.

For the rest of your message you can simply use element's id property. If on
server side you declare the ddls as
<asp:DropDownList runat="server" id ="ddl1">
<asp:DropDownList runat="server" id ="ddl2">
<asp:DropDownList runat="server" id ="ddl3">
then on client side you can find corresponding html elements with
getElementById("ddl1");
getElementById("ddl2");
getElementById("ddl3");

You don't really need to pass "this" as parameter since you are going to
access all 3 ddls anyway.

Eliyahu
The problem is solved temporarily, now I have value from DropDownList which I selected. But requirements is now extend, because actually I have to
generate ID from many DropDownList.

The scenario is here, I have 3 DropDownList, whichever onChange event
triggered on any DropDownList, the GenerateArticleID() function on
client-side collect the values from every DropDownList and concate it into
one ID.

e.g:

DropDownList1 -> If selected, then take value selected from this
DropDownList (DropDownList1), collect value from DropDownList2&3, concate it into one ID, and assign it into Label.text

This event is applied too to other DropDownList...

DropDownList2 -> If selected, then take value selected from this
DropDownList (DropDownList2), collect value from DropDownList1&3, concate it into one ID, and assign it into Label.text

DropDownList3 -> If selected, same activity.

Now.., how can I collect the other DropDownList value (passive one), after
I've got value from selected DropDownList? Or, how ASP .Net recognize value from each server control?

Thx a lot,
Julius F

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
Julius,

A ddl translates to an html <select>. You should follows the DHTML object
model for <select>:

DropDownList1.Attributes["onChange"] = "GenerateArticleID(this)";

<script language="JavaScript">
function GenerateArticleID(ddl){
javascript:alert(ddl.options(ddl.selectedIndex).va lue)
}
</script>

Eliyahu

"Julius Fenata" <id****@gmx.net> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";

At the script on Windows Form, I added this function to have the value of DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID %>").innerText)
}
</script>

Instead of having only one result selected-value from this

DropDownList, the
pop-up messages showing entire data that contained on this
DropDownList, how
to have the selected value/item only on onChange event with this

client-side
script?

Thx,
Julius F



Nov 19 '05 #6
One of the best books I have ever bought is the O'Reilly JavaScript book.
It will save your life.
"Julius Fenata" <id****@gmx.net> wrote in message
news:eg**************@TK2MSFTNGP11.phx.gbl...
Dear Eliyahu & Martin,

Just recently notice that ASP .Net need some understanding about other
programming languange: DHTML Object Model.., but how do I can get
resources/tutorials about this scripting ability?

I have question from your code, what is ddl for? Is that a container
(variable) to accept passed parameter from (this)? If ddl is a variable why there isn't available a data type whose define ddl variable?

The problem is solved temporarily, now I have value from DropDownList which I selected. But requirements is now extend, because actually I have to
generate ID from many DropDownList.

The scenario is here, I have 3 DropDownList, whichever onChange event
triggered on any DropDownList, the GenerateArticleID() function on
client-side collect the values from every DropDownList and concate it into
one ID.

e.g:

DropDownList1 -> If selected, then take value selected from this
DropDownList (DropDownList1), collect value from DropDownList2&3, concate it into one ID, and assign it into Label.text

This event is applied too to other DropDownList...

DropDownList2 -> If selected, then take value selected from this
DropDownList (DropDownList2), collect value from DropDownList1&3, concate it into one ID, and assign it into Label.text

DropDownList3 -> If selected, same activity.

Now.., how can I collect the other DropDownList value (passive one), after
I've got value from selected DropDownList? Or, how ASP .Net recognize value from each server control?

Thx a lot,
Julius F

"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:eW**************@tk2msftngp13.phx.gbl...
Julius,

A ddl translates to an html <select>. You should follows the DHTML object
model for <select>:

DropDownList1.Attributes["onChange"] = "GenerateArticleID(this)";

<script language="JavaScript">
function GenerateArticleID(ddl){
javascript:alert(ddl.options(ddl.selectedIndex).va lue)
}
</script>

Eliyahu

"Julius Fenata" <id****@gmx.net> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Dear all,

I have created client-side scripting to trigger event onChange from
code-behind, like this:

DropDownList1.Attributes["onChange"] = "GenerateArticleID()";

At the script on Windows Form, I added this function to have the value of DropDownList1

<script language="JavaScript">
function GenerateArticleID()
{
javascript:alert(document.getElementById("<%= DropDownList1.ClientID %>").innerText)
}
</script>

Instead of having only one result selected-value from this

DropDownList, the
pop-up messages showing entire data that contained on this
DropDownList, how
to have the selected value/item only on onChange event with this

client-side
script?

Thx,
Julius F



Nov 19 '05 #7

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

Similar topics

1
by: DC Gringo | last post by:
I'm having such a problem with this DropDownList in a user control that is posting back and throwing an error: System.Web.HttpException: A DropDownList cannot have multiple items selected ...
1
by: Ashish Kanoongo | last post by:
I am getting error at the time of compilation, However application runs successfully 'System.Web.UI.WebControls.DropDownList' does not contain a definition for 'SelectedValue'. It is giving...
5
by: DC Gringo | last post by:
I've got a command button to submit a value from a dropdown list that should then filter a SELECT query. I'm simply appending a WHERE colx = <variableSelectedFromDropdownList>. How do I pass this...
2
by: Jeremy | last post by:
In client-side JavaScript, how does one retrieve into a variable the currently displayed text in a DropDownList control. I tried this, but it gets the current ID/value- not the displayed text:...
1
by: Asha | last post by:
greetings, can someone show me how to read a client side value from code behind withouth having to resfresh the page? here is the code i've implemented.... ...
2
by: jason | last post by:
Pardon my ignorance on this. The below code works, except, when I edit a record and update the two drop downs take the first entry in the dropdownlist if not selected. I'd also like the dropdown to...
15
by: mc | last post by:
I'm writing an app for managing Task Lists, I'm trying to add some controls to a form that I can use to link tasks, my original intention was to: - Add two list boxes, one listing "all Tasks"...
2
by: AlecL | last post by:
I have a page that has about 5 or 6 user controls ranging from hearders to footers to ad space but when I add a new user control which is a form it gives me the exception "page can have only one...
1
by: RSH | last post by:
I am testing a few concepts in preparation of a project. One of the concepts revolves around a custom dropdownlist class. Basically I am overriding the RenderContents and writing a custom value...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.