使用jQuery设置DropDownList服务器控件不会更改SelectedValue [英] Setting DropDownList Server Control Using jQuery Doesn't Change SelectedValue

查看:122
本文介绍了使用jQuery设置DropDownList服务器控件不会更改SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,其中我使用jQuery设置了dropdownlist(asp.net服务器控件)的值,但在回发时它不会更改selectedvalue.该控件不是数据绑定的,所以我不认为该问题与更改被覆盖有关.该控件位于WizardControl内部,我也认为与该问题无关.在客户端,一切正常.

I am running into an issue where I set the value of a dropdownlist (asp.net server control) using jQuery, but it doesn't change the selectedvalue upon postback. The control is not databound, so I don't believe the issue has anything to do with the change being overwritten. The control is inside a WizardControl, which I also don't think has anything to do with the issue. On the client side everything works as expected.

这是我的代码:

<asp:DropDownList runat="server" ID="MilitaryBackgroundSelect" ClientIDMode="Static">
    <asp:ListItem Text="" Value="" Selected="True" />                        
    <asp:ListItem Text="No" Value="0" />
    <asp:ListItem Text="YES, IN THE ARMED FORCES" Value="1" />
    <asp:ListItem Text="YES, IN THE RESERVES" Value="2" />
    <asp:ListItem Text="YES, IN THE NATIONAL GUARD" Value="3" />
    <asp:ListItem Text="REFUSED" Value="-7" />
    <asp:ListItem Text="DON'T KNOW" Value="-8" />
    <asp:ListItem Text="MISSING DATA" Value="-9" />
</asp:DropDownList>

我正在使用以下代码在客户端设置下拉列表:

I am setting the dropdownlist on the client side using this code:

 $(document).ready(function(){ 
      $('#MilitaryBackgroundSelect').val("1");
 });

注意:我在下拉列表中将clientidmode设置为static,因此使用jQuery与之交互时没有问题.下面是呈现的HTML,请注意id字段:

NOTE: I set the clientidmode to static on the dropdownlist, so I have no issue interacting with it using jQuery. Below is the HTML rendered, notice the id field:

<select name="ctl00$ctl00$MainContent$MainContentNested$AHHQ_DataEntryWizard$MilitaryBackgroundSelect" 
id="MilitaryBackgroundSelect">
    <option value=""></option>
    <option selected="selected" value="0">No</option>
    <option value="1">YES, IN THE ARMED FORCES</option>
    <option value="2">YES, IN THE RESERVES</option>
    <option value="3">YES, IN THE NATIONAL GUARD</option>
    <option value="-7">REFUSED</option>
    <option value="-8">DON&#39;T KNOW</option>
    <option value="-9">MISSING DATA</option>
</select>

推荐答案

显然,使用javascript更改下拉列表不会触发更新后的控件回发到服务器(已使用fiddler2进行了验证).我不确定为什么会这样,但是有一些解决方法.

Apparently, changing a dropdownlist using javascript doesn't trigger the updated control being posted back to the server (verified using fiddler2). I am not sure why that would be, but there are some workarounds.

  1. 您可以使用隐藏字段,并且在更改dropdownlists值时,还将更新该隐藏值.这是我以前使用jQuery完成的代码片段:

  1. You can use a hidden field and when you change the dropdownlists value, you will also update the hidden value. Here is a snippet I used to do that using jQuery:

 $(document).ready(function(){ 
      $('#dropdownelelement').change(function() {

       var elementID = $(this).attr("id");

       $("#" + elementID + "hidden").val($(this).val());

       });   

});

我研究了 ASP.Net AJAX工具包组合框控件.它的工作方式与上述方法类似,因为当将其呈现到客户端浏览器时,它使用一个隐藏字段来存储其值.

I looked into the ASP.Net AJAX Toolkit combobox control. It works similar to the approach above, since when it is rendered to the client browser, it uses a hidden field to store its values.

最后,由于基于用户输入启用或禁用该字段的某些逻辑非常复杂,因此我不得不拒绝这两种方法.我将代码放入更新面板中,在驱动已启用逻辑的控件上启用了自动回发,并使用验证程序控件执行了我的逻辑.它会导致讨厌的屏幕闪烁,但是代码要简单得多.

In the end I had to reject both approaches due to how complex some of the logic was for which field to enable or disable based on the user input. I put the code into an update panel, enabled autopostback on the controls which drove the enabled logic, and used validator controls to enforce my logic. It causes the nasty screen flicker, but the code is much simplier.

感谢所有尝试提供帮助的人.

Thanks to all who tried to help.

这篇关于使用jQuery设置DropDownList服务器控件不会更改SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆