在更新面板中的AsyncPostBackTrigger之后在Javascript中访问时,Dropdownlist(sslState)值丢失

时间:2012-05-16 07:50:01

标签: c# asp.net .net asp.net-ajax

我有ddlCountry下拉列表和ddlState下拉列表。 ddlState位于更新面板中。在选择国家时,我使用ddlCountry的selectedindexchanged事件填充了其状态。我在绑定后的两个下拉列表中都添加了起始值“-1”,如果下拉列表内容为[选择]值为“-1”,则检查Javascript然后提醒并返回false。

但验证适用于ddlCountry,对于ddlState,在ajax更新面板触发并验证失败后,其值变为“空白”。

我该如何解决?我不知道为什么在AsyncPostBackTrigger之后ddlState值会丢失。

  //javascript validation

        function validateForm()
                {
                    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
<asp:DropDownList ID="ddlCountry " runat="server" CssClass="csstextbox" Width="207px"
                                AutoPostBack="true"  OnSelectedIndexChanged="ddlCountry _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>
            //Code Behind

    protected void Page_Load(object sender, EventArgs e)
        {
               if(!IsPostBack)
            {
                    BindCountry();
                    }
            }
            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;          

                }

1 个答案:

答案 0 :(得分:0)

对于以下两个下拉列表启用viewstate为true:

  <asp:DropDownList ID="ddlState " runat="server" CssClass="csstextbox" Width="177px" EnableViewState=true>                                                     </asp:DropDownList> 

它可能会解决您的问题。

由于