Solved

Need to dynamically add an onChange event to a DropDownList

Posted on 2003-12-09
9
3,352 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jquery to restrict certain words from input in form 11 52
Writing comments on <p></P> or paragraph 2 23
SP result not being displayed 5 69
Save data in two Database, Asp 2 69
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 was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…

734 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