Solved

Need to dynamically add an onChange event to a DropDownList

Posted on 2003-12-09
9
3,378 Views
Last Modified: 2012-08-14
Trying to add an onchange event to a dropdownlist through code behind.  Can create the dropdown using the placeholder like so:

codebehind:
Dropdown1 = New DropDownList
Dropdown1.ID = "cbo" & objDB1.dr.GetValue(0) & "_" & CleanUpName(objDB1.dr.GetValue(1))
Placeholder1.Controls.Add(Dropdown1)

On form:
<asp:placeholder id="Placeholder1" runat="server"></asp:placeholder>

what I want to add is an onChange event like so:
<select name="cbo27_NeckRing" id="cbo27_NeckRing" onChange="GetPrice(cbo27_NeckRing,lbl27_NeckRing)">


Any ideas?
0
Comment
Question by:Knight905416
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 5

Expert Comment

by:DesertWarrior
ID: 9905340
maybe u should ask this in dotNet area
0
 

Author Comment

by:Knight905416
ID: 9905376
Ok, found another related article, can add events using the attribute.add like so:

Dropdown1.Attributes.Add("onChange", "GetPrice(cbo" & strName & ",lbl" & strName & ")")

Produces nice html code like this:

<select name="cbo27_NeckRing" id="cbo27_NeckRing" onChange="GetPrice(cbo27_NeckRing,lbl27_NeckRing)">
      <option value="45">0</option>
      <option value="46">1</option>
      <option value="25">2</option>
      <option value="26">4</option>

</select>

Regards,

Knight
0
 
LVL 6

Expert Comment

by:pillbug22
ID: 9905416
Warrior is right, there is a section for .NEt related questions...

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/


In your page load:

     AddHandler DropDown1.SelectedIndexChanged, AddressOf DropDown1_SelectedIndexChanged

Then make your function:

     Private Sub DropDown1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
          ' Access your drop-down list items just like normal
         
          Dim ddl As DropDownList = sender
          Dim ddlId As String = ddl.ID.ToString()
          Dim ddlItemIndex as integer = ddl.selectedindex
          Dim ddlItemText as string = ddl.selectedItem.text.tostring
          Dim ddlItemValue as string = ddl.selectedItem.Value.tostring

         ' Rest of your function here

     End Sub
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:pillbug22
ID: 9905425
...Or use the Attributes.Add to add client-side functions/events.
0
 
LVL 8

Expert Comment

by:nishikanth
ID: 9910328
add this to the page load event



c = New DropDownList()
c.ID = "id1"

AddHandler c.SelectedIndexChanged, AddressOf Me.GetPrice


Private Sub GetPrice(ByVal sender As System.Object, ByVal e As System.EventArgs)

'This gives the Selected Item's Text. use this for your validations. the same way u can get the value

            CType(sender, DropDownList).SelectedItem.Text)  

End Sub



-------------------------------------------

To use javascript


Person.Attributes.Add("onkeypress", "javascript: getprice();")

have a function in javascript where you do the validations.

You can pass in the dropdown name to the fucntion also
0
 
LVL 8

Expert Comment

by:nishikanth
ID: 9910351
change the name of the dropdown

dropdown1.Attributes.Add("onkeypress", "javascript: getprice();")
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 500 total points
ID: 9910861
Knight905416 ,

>>..Dropdown1.Attributes.Add("onChange", "GetPrice(cbo" & strName & ",lbl" & strName & ")")
-You can use it if you want to validate dropdownlist control in javascript functions.

If you want to validate the dropdownlist using client-side method,
here's the solutions you can use with :

Dropdown1 = New DropDownList
Dropdown1.ID = "cbo" & objDB1.dr.GetValue(0) & "_" & CleanUpName(objDB1.dr.GetValue(1))
Dropdown1.Attributes.Add("onChange", "javascript:checkValue('" + Dropdown1.UniqueID + "')")
Placeholder1.Controls.Add(Dropdown1)

Client-side validations --javascript
------------------------------------------
            <script language="javascript">
                  function checkValue(obj)
                  {
                     //do whatever things you like inside javascript functions
                    alert(document.getElementById(obj).value)
                  }
            </script>

-if you want use server-side, you need SelectedIndexChanged and i believe all the code above work for you and not onChange because this event work for client-side language. Hope that clear for you.

Regards
x_com
0
 

Author Comment

by:Knight905416
ID: 9913302
I should have been clear with regards to whether I needed server side or client side functionality.  I needed client side functionality.  The original solution worked for me, but the 'Dropdown1.UniqueID' trick is a nice touch.  I will replace my code with that more eligant solution.  The onkeypress solution does not work at all - even when only using keyboard, onChange is the correct event.
Points to XCom.
0
 
LVL 29

Expert Comment

by:David H.H.Lee
ID: 9917381
Glad to help, Knight905416 .

Regards
x_com
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question