Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to dynamically add an onChange event to a DropDownList

Posted on 2003-12-09
9
Medium Priority
?
3,428 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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