如何在更新面板中获取javascript中的下拉列表的值

How to get value of dropdown in javascript when it is in update panel

本文关键字:获取 javascript 下拉列表 更新      更新时间:2023-09-26

我只在ddlCountry的事件上绑定ddlstate selectedindechangedddlState javascript 中的下拉值检查,如果它是"-1",然后生成警报。当我在javascript中检查值时,它向我显示一个空白值"。当我使用 Postbacktrigger 表示 ddlState 时,我可以用 javascript 获取值,但使用 async 的页面平滑度比 Postbacktrigger 更好。这就是我使用异步触发器的原因。我的主要问题是ddlState当我使用异步触发器时,它的值没有进入 javascript,而我可以使用回发触发器获取它。

JavaScript 验证:

function validateForm()
{
    var ddlCountry = document.getElementById('<%=ddlCountry.ClientID%>');
    var ddlState = document.getElementById('<%=ddlState.ClientID%>');
    if (ddlCountry .value == "-1")
    {
        alert("Country  should not be blank.");
        ddlCountry .focus();
        return false;
    }
    if (ddlState .value == "-1")
    {
        alert("State should not be blank.");
        ddlState .focus();
        return false;
    }
    return true;
}

ASPX 代码:

<asp:DropDownList ID="ddlAcqModalityList" runat="server" CssClass="csstextbox" Width="207px" AutoPostBack="true" OnSelectedIndexChanged="ddlAcqModalityList_SelectedIndexChanged">
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanelState" runat="server">
    <ContentTemplate>
        <asp:DropDownList ID="ddlState " runat="server" CssClass="csstextbox" Width="177px">
        </asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="btnSave" runat="server" Width="80px" OnClientClick="return validateForm();" Text="Save" CssClass="cssbutton" OnClick="btnSave_Click" />

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        BindCountry()
        {
            strSQL = @"SELECT Country_ID,Country_Desc
                       FROM Country_Master";
            DataTable dataTableState = null;
            dataTableState = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];
            var dictioneryCountry = new Dictionary<int, string>();
            foreach(DataRow dr in dataTableStudy.Rows)
            {
                dictioneryCountry .Add(Convert.ToInt32(dr["Country_ID"]), dr["Country_Desc"].ToString());
            }
            ddlCountry.DataTextField = "Value";
            ddlCountry.DataValueField = "Key";
            ddlCountry.DataSource = dictioneryCountry;
            ddlCountry.DataBind();
            ddlCountry.Items.Insert(0, new ListItem("[Select]", "-1"));
            ddlCountry.Items[0].Selected = true;
        }
    }
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    int countryID = Convert.ToInt32(ddlCountry.SelectedItem.Value);
    ifcountryID == -1)
    {
        return;
    }
    strSQL = @"SELECT State_ID,State_Desc
               FROM State_Master
               WHERE countryID = '" + countryID + @"';
    DataTable dataTableState = null;
    dataTableState = objSqlDbComm.ExecuteDatasetQuery(strSQL).Tables[0];
    var dictioneryState = new Dictionary<int, string>();
    foreach(DataRow dr in dataTableStudy.Rows)
    {
        dictioneryState .Add(Convert.ToInt32(dr["State_ID"]), dr["State_Desc"].ToString());
    }
    ddlState.DataTextField = "Value";
    ddlState.DataValueField = "Key";
    ddlState.DataSource = dictioneryState;
    ddlState.DataBind();
    ddlState.Items.Insert(0, new ListItem("[Select]", "-1"));
    ddlState.Items[0].Selected = true;
}

首先,我认为您不能直接调用"ddlCountry.value",该ID用于服务器控件!您需要使用,以获取客户端 ID。

document.getElementById('ddlCountry').value;

另一件事,如果你想在更改下拉列表时在javascript上触发事件,你可以像这样添加onchange客户端事件,这将调用javascript函数而不发布页面(刷新(:

<asp:DropDownList ID="ddlState " runat="server" 
CssClass="csstextbox" Width="177px" onchange="validateForm()">

然后,如果需要,您可以删除事件触发器和更新面板。

希望对您有所帮助。

        //javascript code
var hidStateValue=document.getElementByID('<%=hidStateValue.ClientID%>');
        if (hidStateValue.value == "-1")
                    {
                        alert("State should not be blank.");                
                        return false;
                    }
        //aspx code
        <asp:DropDownList ID="ddlState" AutoPostBack="true"  runat="server" CssClass="csstextbox" Width="177px" onselectedindexchanged="ddlState_SelectedIndexChanged">                                                                </asp:DropDownList>
            <asp:HiddenField ID="hidStateValue" runat="server" />
        //Code behind
          protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
         {
            //code
            hidStateValue.Value = ddlState.SelectedItem.Value;
         }
        protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
            {       
                hidStateValue.Value="";
                hidStateValue.Value = ddlState.SelectedItem.Value;      
            }