1

我有 ddlCountry 下拉列表和 ddlState 下拉列表。ddlState 在更新面板中。在选择国家时,我使用 ddlCountry 的 selectedindexchanged 事件填充了它的状态。绑定后,我在两个下拉列表中都添加了起始值“-1”,如果下拉列表内容为 [Select] 值“-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;          

                }
4

1 回答 1

0

为两个下拉列表启用 viewstate 为 true,如下所示:

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

它可能会解决您的问题。

谢谢

于 2012-05-17T07:15:20.937 回答