Solved

creating dropdownlist from code behind and javascript

Posted on 2003-11-07
10
1,020 Views
Last Modified: 2007-12-19
if i am creating a dropdownlist dynamically in code behind, how can i add a onChange javascript function to it?

thanks!
0
Comment
Question by:trevoray
[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
  • 6
  • 3
10 Comments
 

Author Comment

by:trevoray
ID: 9704126
this is my current code for the dropdownlist, it is placing it in the placeholder named "rctable"

 ddl = New DropDownList
                ddl.ID = "RCDropDownList" & int
                ddl.Items.Add(New ListItem(dtrMembers("rcmbrtype"), dtrMembers("rcfeetext")))
                rctable.Controls.Add(ddl)
0
 
LVL 1

Expert Comment

by:ib_setiawan
ID: 9705386
Perhaps I can help you..

You can add these two lines at your code:
-----------------------------------------------
        ddl.AutoPostBack = True
        AddHandler ddl.SelectedIndexChanged, AddressOf DropDownChange

And then add this method:
------------------------------
    Private Sub DropDownChange(ByVal sender As Object, ByVal e As EventArgs)
        'this is your code when there is a change event
         txtNote.Text = ddl.SelectedItem.Text
    End Sub

The autopost back is used to auto wire the page when there is a change at the control.
And the AddHandler function is used to bind the SelectedIndexChanged event to DropDownChange method.

The code is like this:
-----------------------
    Protected WithEvents txtNote As System.Web.UI.WebControls.TextBox
    Protected WithEvents rctable As System.Web.UI.WebControls.PlaceHolder
    Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create drop downlist at runtime
        ddl = New DropDownList()
        ddl.ID = "RCDropDownList1"
        ddl.Items.Add(New ListItem("Shampo", "S"))
        ddl.Items.Add(New ListItem("Deodorant", "D"))
        ddl.Items.Add(New ListItem("Pasta", "P"))
        rctable.Controls.Add(ddl)

        ddl.AutoPostBack = True
        AddHandler ddl.SelectedIndexChanged, AddressOf DropDownChange

    End Sub

    Private Sub DropDownChange(ByVal sender As Object, ByVal e As EventArgs)
        txtNote.Text = ddl.SelectedItem.Text
    End Sub


Regards,
Iman Budi Setiawan
0
 
LVL 2

Expert Comment

by:CuSo4
ID: 9706538
For a Javanscript onChange instead of a codebehind onChange, you can use this:

                ddl = New DropDownList
                ddl.ID = "RCDropDownList" & int
                ddl.Items.Add(New ListItem(dtrMembers("rcmbrtype"), dtrMembers("rcfeetext")))
                rctable.Controls.Add(ddl)
                ddl.Attributes.Add("onChange","javascript:return confirm('Are you sure?');")
0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 

Author Comment

by:trevoray
ID: 9707006
CuSo4, thanks for the javascript. ok, question. how can i modify your script to run a javascript function that already exists? i have a javascript function that will change the value of a text box when an item is selected...

thanks!
0
 
LVL 2

Expert Comment

by:CuSo4
ID: 9707121
hmm, if your javascript function is already in your apsx-page, then it's quite easy:
assuming your Javascript Function is "thisFunction()":

                     ddl.Attributes.Add("onChange","javascript:thisFunction();")

If your Javascript Page isn't in your aspx-page yet, and you want to add/create it with code-behind, you can do it like this:
In your Page_Load you can add the following lines:

                     If (Not IsClientScriptBlockRegistered("Script")) Then
                         strScript = "<script language='JavaScript'>"
                         strScript = strScript & vbCrLf
                         strScript = strScript & "Function thisFunction() {"
                         strScript = strScript & "//Your Function script here"
                         strScript = strScript & "}"
                         strScript = strScript & "</script>"
                         RegisterClientScriptBlock("Script", strScript)
                     End If

                      ddl.Attributes.Add("onChange","javascript:thisFunction();")
0
 

Author Comment

by:trevoray
ID: 9709730
ok, can you help me out with this last part. actually writing the javascript?

in html...
<script language=Javascript>
Function thisFunction(myValue)
    document.form.mytextbox.value = myValue
End Function
</script>

and in code behind...

    ddl = New DropDownList
               ddl.ID = "RCDropDownList" & int
               ddl.Items.Add(New ListItem(dtrMembers("rcmbrtype"), dtrMembers("rcfeetext")))
               rctable.Controls.Add(ddl)
               ddl.Attributes.Add("onChange","javascript:thisFunction(dtrMembers("rcfeevalue"));")


would this work?

thanks!
0
 
LVL 2

Accepted Solution

by:
CuSo4 earned 500 total points
ID: 9709747
hmm, I think excepting a quote problem in your last line this should be working... The last line has to be:

                           ddl.Attributes.Add("onChange","javascript:thisFunction(" & dtrMembers("rcfeevalue") & ");")

(because dtrMembers("rcfeevalue") is a variable in your code-behind, and not in your Javascript)
0
 

Author Comment

by:trevoray
ID: 9709787
ok, can we reformat it if i used a string instead of dtrMembers("rcfeevalue")  ?

strRCFeevalue?

how would i do it then? i tried and it is giving me a string not defined error in the javascript when the page is run.

thanks!
0
 

Author Comment

by:trevoray
ID: 9709916
this is what i tried and i am getting a  javascript null error. i think this is because it is not passing the values ( i have two) to the javascript function because of the formating of quotation marks.

  ddl.Attributes.Add("onChange", "javascript:RC_onselect(" & strRCFeeValue & "," & strRCTextbox & ");")
0
 

Author Comment

by:trevoray
ID: 9709976
ok, the javascript worked. thanks for your help! i'm still having issues, but the javascript is not giving errors, i might open up new topic with my new problems.

this is what worked:

ddl.Attributes.Add("onChange", "javascript:RC_onselect('" & strRCFeeValue & "','" & strRCTextbox & "');")
0

Featured Post

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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