trevoray
asked on
creating dropdownlist from code behind and javascript
if i am creating a dropdownlist dynamically in code behind, how can i add a onChange javascript function to it?
thanks!
thanks!
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. PlaceHolde r
Protected WithEvents ddl As System.Web.UI.WebControls. DropDownLi st
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
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.
Protected WithEvents rctable As System.Web.UI.WebControls.
Protected WithEvents ddl As System.Web.UI.WebControls.
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
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("rcmbr type"), dtrMembers("rcfeetext")))
rctable.Controls.Add(ddl)
ddl.Attributes.Add("onChan ge","javas cript:retu rn confirm('Are you sure?');")
ddl = New DropDownList
ddl.ID = "RCDropDownList" & int
ddl.Items.Add(New ListItem(dtrMembers("rcmbr
rctable.Controls.Add(ddl)
ddl.Attributes.Add("onChan
ASKER
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!
thanks!
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("onChan ge","javas cript:this Function() ;")
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 IsClientScriptBlockRegiste red("Scrip t")) 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("onChan ge","javas cript:this Function() ;")
assuming your Javascript Function is "thisFunction()":
ddl.Attributes.Add("onChan
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 IsClientScriptBlockRegiste
strScript = "<script language='JavaScript'>"
strScript = strScript & vbCrLf
strScript = strScript & "Function thisFunction() {"
strScript = strScript & "//Your Function script here"
strScript = strScript & "}"
strScript = strScript & "</script>"
RegisterClientScriptBlock(
End If
ddl.Attributes.Add("onChan
ASKER
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.va lue = myValue
End Function
</script>
and in code behind...
ddl = New DropDownList
ddl.ID = "RCDropDownList" & int
ddl.Items.Add(New ListItem(dtrMembers("rcmbr type"), dtrMembers("rcfeetext")))
rctable.Controls.Add(ddl)
ddl.Attributes.Add("onChan ge","javas cript:this Function(d trMembers( "rcfeevalu e"));")
would this work?
thanks!
in html...
<script language=Javascript>
Function thisFunction(myValue)
document.form.mytextbox.va
End Function
</script>
and in code behind...
ddl = New DropDownList
ddl.ID = "RCDropDownList" & int
ddl.Items.Add(New ListItem(dtrMembers("rcmbr
rctable.Controls.Add(ddl)
ddl.Attributes.Add("onChan
would this work?
thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!
ASKER
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("onChan ge", "javascript:RC_onselect(" & strRCFeeValue & "," & strRCTextbox & ");")
ddl.Attributes.Add("onChan
ASKER
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("onChan ge", "javascript:RC_onselect('" & strRCFeeValue & "','" & strRCTextbox & "');")
this is what worked:
ddl.Attributes.Add("onChan
ASKER
ddl = New DropDownList
ddl.ID = "RCDropDownList" & int
ddl.Items.Add(New ListItem(dtrMembers("rcmbr
rctable.Controls.Add(ddl)