简体   繁体   English

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

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

I'm binding ddlstate on selectedindechanged event of ddlCountry only. 我结合ddlstateselectedindechanged的事件ddlCountry只。 ddlState dropdown value checking in javascript if it is "-1" then generate alert. 在javascript中检查ddlState下拉值是否为“ -1”,然后生成警报。 When I check the value in javascript, it shows me a blank value, "". 当我检查javascript中的值时,它显示了一个空白值“”。 When I use Postbacktrigger for ddlState , I can get the value with javascript, but the smoothness of the page using async is better than Postbacktrigger. 当我将Postbacktrigger用于ddlState ,可以使用javascript获取值,但是使用async进行页面的平滑性要比Postbacktrigger更好。 That is why I use async trigger. 这就是为什么我使用异步触发器。 My main problem is ddlState 's value is not getting in javascript when I use async trigger while I can get it using Postback trigger. 我的主要问题是使用异步触发器时ddlState的值未进入javascript,而我可以使用回发触发器来获取它。

JavaScript validation: 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 code: 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" />

Code Behind: 背后的代码:

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;
}

First of all I think you cannot call "ddlCountry.value" directly, that id is for the server control! 首先,我认为您不能直接调用“ ddlCountry.value”,该ID用于服务器控件! You need to use, to get the client id. 您需要使用获取客户端ID。

document.getElementById('ddlCountry').value;

Another thing if you want to fire event on javascript when you change the dropdownlist you can add onchange client event like this, this will call a javascript function without posting the page (Refresh): 另一件事,如果您想在更改下拉列表时在javascript上触发事件,则可以添加onchange客户端事件,如下所示,这将调用javascript函数而不发布页面(刷新):

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

Then you can remove the eventtrigger and the updatepanel if you want. 然后,您可以根据需要删除eventtrigger和updatepanel。

Hope it helps. 希望能帮助到你。

        //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;      
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用asp.net中的自动回发和更新面板的情况下在Label中获取下拉列表中选定的值 - How to get dropdown list selected value in Label without using autopostback and update panel in asp.net 当 FCK 编辑器放置在更新面板中时如何从 FCK 编辑器获取编辑值 - How to get edited value from FCK Editor when FCK Editor placed inside a Update Panel 在更新面板中调用Javascript ConfirmBox时不显示 - Javascript ConfirmBox not showing when called in Update Panel 无法获取更新面板内按钮的值 - unable to get the value of button inside update panel 在更新面板中的AsyncPostBackTrigger之后在Javascript中访问时Dropdownlist(ddlState)值丢失 - Dropdownlist(ddlState) value loss when acess in Javascript after AsyncPostBackTrigger in update panel 如何使用JavaScript触发更新面板 - How to trigger update panel using javascript 无法使用 javascript 获取下拉选择值? - Cant get dropdown selected value with javascript? 使用UPDATE PANEL时,应用程序不显示javascript消息 - Application does not shows javascript message when UPDATE PANEL is used 在使用的GridView更新面板没有得到单元格的值 - Did not get cell value in update panel used Gridview 当按钮执行代码在更新面板之外时,如何使用更新面板内部的更新进度 - How to use update progress inside update panel, when the button executing code is outside update panel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM