Solved

Need to dynamically add an onChange event to a DropDownList

Posted on 2003-12-09
9
3,301 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error viewing ASP page 12 165
Help with query 3 26
Downside of adding characters set in ASP pages 6 21
Connection to multiple databases 13 19
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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