asp dropdownlist - 服务器端 SelectedIndexChanged 仅在使用鼠标时触发,而不是在通过代码设置选择时触发...任何人都可以帮忙吗?
刚在这里发帖,但我读过很多其他主题和内容。过去找到了很好的答案!我希望有人知道为什么会发生这种情况...在我的 DDL 上,服务器端 SelectedIndexChanged 事件仅在用户实际单击控件并更改选择时触发...但当我通过 javascript 代码设置选择时则不会触发。任何帮助表示赞赏!
这是一些代码...
<%@ Page Language="VB" AutoEventWireup="false" Inherits="FileUpload._Default"
EnableEventValidation="false" EnableViewState="true"
Codebehind="Default.aspx.vb" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="cc1" %>
无论是通过 gui 还是我自己在代码中更改选择,此客户端 OnChange 代码都会触发:
function ddDBF_OnChange(dropdown)
{
UpdateSelectedMappings();
}
当我像这样在 js 中设置所选值时,服务器端 SelectedIndexChanged 事件不会触发:
dropdowns.item(i).options[k].selected = "selected";
这是def 控件(在 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>
...并且不确定它是否重要,但这是服务器端事件:
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
虽然我认为缺少服务器事件触发导致了我所看到的问题,但我猜我从来没有真正解释过这个问题是什么! :P 基本上,我有一个由这些级联下拉列表组成的网格 - 当用户与它们交互时,视图状态被维护,并且值在回发中被保留。然而,有时我需要在客户端 js 代码中设置值 - 在这些情况下,框中的值在视觉上确实会发生变化,但在下一个回发操作中它们会被重置:(
非常感谢!!!!
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" %>
<%@ 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!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 doPostBack:
http://www.codedigest.com/Articles/ASPNET/320_Doing_or_Raising_Postback_using_doPostBack()_function_from_Javascript_in_AspNet.aspx
Use doPostBack:
http://www.codedigest.com/Articles/ASPNET/320_Doing_or_Raising_Postback_using_doPostBack()_function_from_Javascript_in_AspNet.aspx
尝试了 doPostBack 但没有成功,但我确实找到了解决方案,所以我想我会分享。让我感到困惑的是,为什么实际上单击列表框会保留这些值,但以编程方式设置它会导致级联下拉菜单刷新后该值丢失。
事实证明,使用 cdd ajax 控件,您不能只设置 ddl 选择的值 - 您必须将其设置到 cdd 加载的 webmethod 中。当执行 .add 到 List 结构时,您附加第三个参数 - true 表示已选择, false 表示未选择。做出改变后一切都很好。
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.