Solved

Need to dynamically add an onChange event to a DropDownList

Posted on 2003-12-09
9
3,226 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
  • 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
 
LVL 6

Expert Comment

by:pillbug22
ID: 9905425
...Or use the Attributes.Add to add client-side functions/events.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now