使用Jquery更改asp:DropDownList的默认选择值

Change asp:DropDownList default selected value with Jquery

本文关键字:默认 选择 DropDownList Jquery 更改 asp 使用      更新时间:2023-09-26

我有一个asp:DropDownList控件在一个隐藏的弹出窗口,这个弹出窗口是激活当用户点击一个图标(图像)在Gridview控件的一行

然后我使用一些jquery来选择被点击的行,然后我提取gridview行上的标签控件的值,然后要填充弹出框字段(文本框控件和下拉列表控件默认值),这个想法是使用它们来更新数据库中行中的记录。

我遇到的问题是在弹出框上的下拉控件中填充默认选择。我可以在文本框中填充文本框,而不是下拉框。

下面是其中一个文本框的标记和来自gridview的ddl,我的值来源于此:

        <asp:TemplateField HeaderText="Current Stage"> 
    <ItemTemplate> 
        <asp:Label ID="lblCurrentStage" CssClass="clCurrentStage" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CurrentStage")%>' ToolTip ='<%# DataBinder.Eval(Container.DataItem, "CurrentStage")%>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Review Date"> 
    <ItemTemplate> 
        <asp:Label ID="lblReviewDate" CssClass="clReviewDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ReviewDate")%>' ToolTip ='<%# DataBinder.Eval(Container.DataItem, "ReviewDate")%>'></asp:Label>
    </ItemTemplate> 
</asp:TemplateField> 

,下面的代码在文本框上工作得很好,但在ddl上不行:

        <div id="PopUpTrackerEditFieldCurrentStage">
    <div class="clEditFieldCurrentStageContainer">
    <asp:DropDownList ID="ddlPopUpEditCurrentStage" runat="server"> </asp:DropDownList>
    </div>
</div>
<div id="PopUpTrackerEditFieldReviewDate">
    <div class="clEditFieldReviewDateContainer">
    <asp:TextBox ID="tbPopUpEditReviewDate"  CssClass="clPopUpDateFieldsInEdit" runat="server" Text=""  ToolTip =""></asp:TextBox>
    </div>
</div>

下面是用来填充文本框和下拉列表的jquery:

//Store the current row being edited
var row = $(this).closest("tr");
//Get the existing Comments into a string
var strCurrentStage = $(".clCurrentStage", row).text();
//Add the any existing Comments
$("#<%=ddlPopUpEditCurrentStage.ClientID%>").val(strCurrentStage);
//Dynamically add the text to the tooltip 
$("#<%=ddlPopUpEditCurrentStage.ClientID%>").attr('title', 'Click to select the current stage here for ' + strPSTNNum);
//Get the existing Review Date into a string
var strReviewDate = $(".clReviewDate", row).text();
//Add the any existing Review Date
$("#<%=tbPopUpEditReviewDate.ClientID%>").val(strReviewDate);
//Dynamically add the text to the tooltip 
$("#<%=tbPopUpEditReviewDate.ClientID%>").attr('title', 'Edit the review date here for ' + strPSTNNum);

我知道strCurrentStage是ok的,因为我暂时使用它来填充文本框,看看它是否包含gridview中当前阶段标签的当前阶段文本,它确实包含了。所以我认为问题是,我不能选择下拉列表控件的正确部分来填充默认值。

尝试不使用选择器。例如,您试图将选定值设置为selected:

//Store the current row being edited
var row = $(this).closest("tr");            
//Get the existing Comments into a string from the label in the gridview
var strCurrentStage = $(".clCurrentStage", row).text();
//Add the current stage selected value to the ddl control
$("#<%=ddlPopUpCurrentStage.ClientID%>").val(strCurrentStage);

为了其他人的利益,我最终设法对其进行排序的唯一方法是创建一个函数,该函数关闭并从数据库中获得选择,然后在填充部分的部分下方,然后在完成之前将代码添加到该函数的底部。我怀疑的原因是,当OP代码试图添加下拉菜单并放入默认值时,DOM已经看到了变化,我通过使用jquery"Live('mousover',blaa)"来确认这一点。,然后它就工作了。无论如何,下面是代码:

function getListOfStages() {
    var ddlCurrentStages = $("#<%=ddlPopUpEditCurrentStage.ClientID%>");
    $.ajax({
        type: "POST",
        url: "PSTN_OrderManagementTracker.aspx/PopCurrentStagesddl",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var CurrentStagesReturned = xml.find("Table");
            //for each current stage from the database, add it to the dropdown list on the modal
            $.each(CurrentStagesReturned, function (index, CurrentStagesReturned) {
                dbCurrentStageName = $(this).find("CurrentStage").text()
                ddlCurrentStages.append('<option>' + dbCurrentStageName + '</option>');
                //Get the this records existing current stage into a string
                var strCurrentStage = $(".clCurrentStage", row).text();
                //Add the existing current stage for that record as the default one showing
                $("select#<%=ddlPopUpEditCurrentStage.ClientID%>").val(strCurrentStage);
            });
        },
        failure: function (msg) {
            alert(msg);
        }
    });
}

$ (' # & lt; % = ddlPopUpCurrentStage。ClientID %> option:selected').val(strCurrentStage)