jQuery更改数据绑定ASP.NET DropDownList中的SelectedValue

韦尔德金

我有一个ASP.NET表单(c#MySQL后端),其中显示了三个DropDownList小部件

<asp:FormView
    ID="_fvNoleggio"
    runat="server"
    DataKeyNames="id"
    DataSourceID="_sdsNoleggio"
    DefaultMode="Edit" >
        <EditItemTemplate>
            <asp:Table runat="server">
                <asp:TableRow>
                    <asp:TableCell>
                        <asp:Label 
                            AssociatedControlID="_ddlAssicurazioneCedente"
                            ID="_lblAssicurazioneCedente" 
                            runat="server" 
                            Text="Assicurazione cedente" />
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:DropDownList 
                            ID="_ddlAssicurazioneCedente"
                            ClientIDMode="Static"
                            runat="server" 
                            DataSourceID="_sdsAssicurazione"
                            onchange="javascript: CheckAssicurazione(this);"
                            DataTextField="nome" 
                            DataValueField="id"
                            AppendDataBoundItems="true"
                       SelectedValue='<%#Eval("id_assicurazione_cedente")%>' >
                            <asp:ListItem Text="Nessuna" Value="" />
                        </asp:DropDownList>
                    </asp:TableCell>
                    <asp:TableCell>&nbsp;</asp:TableCell>
                </asp:TableRow>

                <asp:TableRow>
                    <asp:TableCell>
                        <asp:Label
                          AssociatedControlID="_ddlAssicurazioneControparte"
                            ID="_lblAssicurazioneControparte" 
                            runat="server" 
                            Text="Assicurazione cedente" />
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:DropDownList 
                             ID="_ddlAssicurazioneControparte"
                             ClientIDMode="Static"
                             runat="server" 
                             DataSourceID="_sdsAssicurazione"
                             onchange="javascript:CheckAssicurazione(this);"
                             DataTextField="nome" 
                             DataValueField="id"
                             AppendDataBoundItems="true"
                   SelectedValue='<%#Eval("id_assicurazione_controparte")%>' >
                             <asp:ListItem Text="Nessuna" Value="" />
                        </asp:DropDownList>
                    </asp:TableCell>
                    <asp:TableCell>&nbsp;</asp:TableCell>
                </asp:TableRow>

                <asp:TableRow>
                    <asp:TableCell>
                        <asp:Label 
                            AssociatedControlID="_ddlAssicurazionePagante"
                            ID="_lblAssicurazionePagante" 
                            runat="server" 
                            Text="Assicurazione cedente" />
                    </asp:TableCell>
                    <asp:TableCell>
                        <asp:DropDownList 
                            ID="_ddlAssicurazionePagante"
                            ClientIDMode="Static"
                            runat="server" 
                            DataSourceID="_sdsAssicurazione"
                            onchange="javascript: CheckAssicurazione(this);"
                            DataTextField="nome" 
                            DataValueField="id"
                            AppendDataBoundItems="true"
                       SelectedValue='<%#Eval("id_assicurazione_pagante")%>' >
                                <asp:ListItem Text="Nessuna." Value="" />
                        </asp:DropDownList>
                    </asp:TableCell>
                    <asp:TableCell>&nbsp;</asp:TableCell>
                </asp:TableRow>
            </asp:Table>
        </EditItemTemplate>
</asp:FormView>

它们都通过单个SQLDataSource元素输入,而SelectedValue属性绑定到主字段集的匹配字段(输入包括FormView):

<asp:SqlDataSource 
    ID="_sdsAssicurazione" 
    runat="server"
    ConnectionString="<%$ ConnectionStrings:mydb %>"
    ProviderName="<%$ ConnectionStrings:mydb.ProviderName %>"
    SelectCommand=" SELECT 
                       * 
                    FROM 
                       assicurazioni;">
</asp:SqlDataSource>

<asp:SqlDataSource 
    ID="_sdsNoleggio" 
    runat="server"
    ConnectionString="<%$ ConnectionStrings:mydb %>"
    ProviderName="<%$ ConnectionStrings:mydb.ProviderName %>"
    SelectCommand=" SELECT 
                       * 
                    FROM 
                       noleggio_veicoli;">
</asp:SqlDataSource>

我需要检查前两个中的选定值DDL(加上另一个外部值)以设置第三个DDL选定值。这是我正在使用的.js代码:

function CheckAssicurazione(widget) {
    if (widget == null)
        return;

    CheckAssicurazionePagante();

    $.ajax({
        type: "POST",
        url: "/Service/WSDataService.asmx/CheckAssicurazione",
        data: "{'id':'" + widget.value + "'}",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            if (data == null)
                return;

            if (data.d == null)
                return;

            if (data.d == "True")
                alert("Warning!")
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

function CheckAssicurazionePagante()
{
    // _tipo_indennizzio is a global value that is set in another section of code and is not null (I debugged it)

    if (_tipo_indennizzo == null)
        return;

    var _assicurazione_controparte = $('#_ddlAssicurazioneControparte option:selected').val();
    var _assicurazione_cedente = $('#_ddlAssicurazioneCedente option:selected').val();
    var _assicurazione_pagante = null;

    if (_tipo_indennizzo == "DIRETTO")
        _assicurazione_pagante = _assicurazione_cedente;
    else if (_tipo_indennizzo == "TRADIZIONALE")
        _assicurazione_pagante = _assicurazione_controparte;
    else
        return;

    var _assicurazione_pagante_selezionata = $('select#_ddlAssicurazionePagante option:selected').val();

    if (isBlank(_assicurazione_pagante_selezionata) || (_assicurazione_pagante_selezionata != _assicurazione_pagante))
    {
        var _result = confirm("warning ?");

        // If the user answer 'yes' to the confirm dialog the third DDL should be set to the 'correct' value

        if (_result == true) {
            $('#_ddlAssicurazionePagante option:selected').val(_assicurazione_pagante);
        }
    }
}

不幸的是,它不起作用...值似乎设置正确(不会引发异常),但事实上,网页中呈现的DDL并未更新。

我哪里错了?顺便说一句,我正在与jquery 2.1.3jquery-ui 1.11.2

<script type="text/javascript" src="../Scripts/jquery-2.1.3.js"></script>
<script type="text/javascript" src="../Scripts/jquery-ui-1.11.2.js"></script>

n

康纳斯·范

在当前代码中,您将获取并设置每个DropDownList所选项目的值。您应该获取并设置DropDownList本身的值:

var _assicurazione_controparte = $('#_ddlAssicurazioneControparte').val();
var _assicurazione_cedente = $('#_ddlAssicurazioneCedente').val();

...

$('#_ddlAssicurazionePagante').val(_assicurazione_pagante);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

DropDownList中的SelectedValue无法选择任何值(VB.Net,Asp.Net)

来自分类Dev

使用asp.net mvc将DropDownList中的SelectedValue从View Razor传递到控制器

来自分类Dev

如何使用javascript更改asp.net DropDownList SelectedValue?

来自分类Dev

在ASP.NET中绑定SelectedValue

来自分类Dev

ASP.NET MVC DropDownList SelectedValue转换为ViewBag?

来自分类Dev

如果使用jquery添加数据,ASP.Net dropdownlist会丢失selectedValue

来自分类Dev

如何使用/不使用ASP.net C#中的CodeBehind将DropDownList中的SelectedValue作为查询字符串传递为LinkButton中的查询字符串

来自分类Dev

数据绑定到asp.net中的dropdownlist?

来自分类Dev

ASP.NET-使用RowCommand时获取当前行的DropDownList SelectedValue

来自分类Dev

ASP.NET C#如何在GridView中更改数据并写入XML

来自分类Dev

ASP .Net jQuery DropDownList更改事件

来自分类Dev

在ASP.NET中本地化动态绑定的DropDownList

来自分类Dev

在ASP.NET中本地化动态绑定的DropDownList

来自分类Dev

更改数据模板中的绑定对象

来自分类Dev

DropDownList中的ASP.NET 2 DataValuefield

来自分类Dev

在ASP.NET MVC中填充DropDownList

来自分类Dev

Asp.net中的DropDownList问题

来自分类Dev

FormView中的ASP.NET DropDownList

来自分类Dev

在 ASP.NET MVC 中填充 DropDownList

来自分类Dev

如何更改数据网格标题以跟随 C# ASP.NET 中的其他单元格值?

来自分类Dev

ADO.Net数据绑定错误到ASP DropDownList

来自分类Dev

ADO.Net数据绑定到ASP DropDownList的错误

来自分类Dev

ASP.NET MVC 中的数据绑定

来自分类Dev

来自 Web 服务的 DropDownList 绑定在 asp.net 中编辑时不传递数据源

来自分类Dev

从asp.net中的回发中的DropDownList检索值

来自分类Dev

在Asp.Net Core中的Kendo Grid中的DropDownList

来自分类Dev

将checkboxlist selectedvalue放入asp.net中的字符串中

来自分类Dev

在Gridview中获取Dropdownlist的SelectedValue

来自分类Dev

在 .net 中填充 DropDownList

Related 相关文章

  1. 1

    DropDownList中的SelectedValue无法选择任何值(VB.Net,Asp.Net)

  2. 2

    使用asp.net mvc将DropDownList中的SelectedValue从View Razor传递到控制器

  3. 3

    如何使用javascript更改asp.net DropDownList SelectedValue?

  4. 4

    在ASP.NET中绑定SelectedValue

  5. 5

    ASP.NET MVC DropDownList SelectedValue转换为ViewBag?

  6. 6

    如果使用jquery添加数据,ASP.Net dropdownlist会丢失selectedValue

  7. 7

    如何使用/不使用ASP.net C#中的CodeBehind将DropDownList中的SelectedValue作为查询字符串传递为LinkButton中的查询字符串

  8. 8

    数据绑定到asp.net中的dropdownlist?

  9. 9

    ASP.NET-使用RowCommand时获取当前行的DropDownList SelectedValue

  10. 10

    ASP.NET C#如何在GridView中更改数据并写入XML

  11. 11

    ASP .Net jQuery DropDownList更改事件

  12. 12

    在ASP.NET中本地化动态绑定的DropDownList

  13. 13

    在ASP.NET中本地化动态绑定的DropDownList

  14. 14

    更改数据模板中的绑定对象

  15. 15

    DropDownList中的ASP.NET 2 DataValuefield

  16. 16

    在ASP.NET MVC中填充DropDownList

  17. 17

    Asp.net中的DropDownList问题

  18. 18

    FormView中的ASP.NET DropDownList

  19. 19

    在 ASP.NET MVC 中填充 DropDownList

  20. 20

    如何更改数据网格标题以跟随 C# ASP.NET 中的其他单元格值?

  21. 21

    ADO.Net数据绑定错误到ASP DropDownList

  22. 22

    ADO.Net数据绑定到ASP DropDownList的错误

  23. 23

    ASP.NET MVC 中的数据绑定

  24. 24

    来自 Web 服务的 DropDownList 绑定在 asp.net 中编辑时不传递数据源

  25. 25

    从asp.net中的回发中的DropDownList检索值

  26. 26

    在Asp.Net Core中的Kendo Grid中的DropDownList

  27. 27

    将checkboxlist selectedvalue放入asp.net中的字符串中

  28. 28

    在Gridview中获取Dropdownlist的SelectedValue

  29. 29

    在 .net 中填充 DropDownList

热门标签

归档