asp dropdownlist - server side SelectedIndexChanged only fires when the mouse is used, not when the selection is set via code... Can anyone help?
New to posting here, but I've read a lot of the other threads & found great answers in the past! I'm hoping someone knows why this may be happening... On my DDL the server side SelectedIndexChanged event only fires when the user actually clicks on the control and changes the selection... but not when I set the selection via javascript code. Any help is appreciated!!!
Here's some of the code...
<%@ Page Language="VB" AutoEventWireup="false" Inherits="FileUpload._Default"
EnableEventValidation="false" EnableViewState="true"
Codebehind="Default.aspx.vb" 开发者_Python百科%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="cc1" %>
This client side OnChange code fires whether the selection is changed via the gui or by myself in the code:
function ddDBF_OnChange(dropdown)
{
UpdateSelectedMappings();
}
When I set the selected value in js like this the server side SelectedIndexChanged event does not fire:
dropdowns.item(i).options[k].selected = "selected";
Here's the def for the control (its repeated in a gridview):
<asp:gridview id='gvMap' runat='server' autogeneratecolumns='False'
onrowdatabound='gvMap_rowdatabound'>
<columns>
<asp:boundfield datafield='f1' headertext='h1' />
<asp:templatefield headertext='h2'>
<itemtemplate>
<asp:dropdownlist LastSel = '' ID='ddDBF' CssClass='ddDBF' runat='server' AutoPostBack='True' onchange='ddDBF_OnChange();' OnSelectedIndexChanged='ddDBF_SelectedIndexChanged' OnDataBound = 'ddDBF_OnDataBound'
OnClientPopulated='ddDBF_OnClientPopulated' EnableViewState = 'true' >
</asp:dropdownlist>
<cc1:CascadingDropDown id='cddDBF'
runat='server'
Category='DataBaseField'
TargetControlID='ddDBF'
LoadingText='Loading Database Fields..'
ServicePath='CascadingDropDown.asmx'
ServiceMethod='GetDatabaseFields'
UseContextKey='true'
ContextKey='Set serverside gvMap_RowDataBound'
EnableViewState = 'true'
>
</cc1:CascadingDropDown>
</ItemTemplate>
</asp:templatefield>
</columns>
</asp:gridview>
...and not sure that it matters, but here's the server side event:
Protected Sub ddDBF_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim d As DropDownList = sender
d.Attributes("LastSel") = d.SelectedValue
Dim hdnSelect As HiddenField = Page.FindControl("AlreadyMapped")
Session("MappedFields") = hdnSelect.Value
End Sub
While I think the lack of the server event firing is causing the problem I'm seeing, I guess I never really explained what that problem is! :P Basically, I have a grid of these cascading dropdownlist's - the viewstate is maintained and the values are kept across postbacks when the user is interacting with them. However, at times I need to set the values in client side js code - in those cases, the value does change in the boxes, visually, but on the next postback action they get reset :(
Thanks so much!!!!
Use doPostBack: http://www.codedigest.com/Articles/ASPNET/320_Doing_or_Raising_Postback_using_doPostBack()_function_from_Javascript_in_AspNet.aspx
<script type="text/javascript">
function CallServer() {
__doPostBack('lbDoPostBack', 'JavaScript');
}
</script>
Tried the doPostBack without success, but I did find the solution so I thought I'd share. It was confusing to my why actually clicking on the listbox kept the values but setting it programatically caused the value to be lost after the cascading drop downs refreshed.
Turns out that with the cdd ajax controls, you can't just set the ddl selected value - you have to set it int he webmethod that the cdd loads from. When doing an .add to the List struct, you append a third parm - true for selected, false for not selected. After making that change all is well.
dbFieldNames.Add(new CascadingDropDownNameValue(row["FieldName"].ToString(), row["FieldName"].ToString(), True));
精彩评论