本文介绍了更新面板中AsyncPostBackTrigger之后的Dropdownlist(ddlState)值丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ddlCountry下拉列表和ddlState下拉列表.. ddlState在更新面板中。在选择国家时,我使用ddlCountry的selectedindexchanged事件填充了其状态。我在绑定后的两个下拉列表中都添加了起始值-1...如果下拉列表内容为[Select] value-1则检查javascript然后提醒并返回false ...但是ddlCountry和ddlState的验证是正确的在ajax更新面板触发并验证失败后,它的值变为空白。我该如何解决它们...我不知道为什么在AsyncPostBackTrigger之后ddlState值丢失





i have ddlCountry dropdownlist and ddlState dropdownlist.. ddlState is in update panel. On selection of country i have populated its states using selectedindexchanged event of ddlCountry. i have added start value "-1" to both dropdown after bind...and check in javascript if dropdownlist content is [Select] value"-1" then alert and return false...But Validation is proper for ddlCountry and for ddlState its value becomes "blank" after ajax update panel fires and validation fail..How should i resolve them..I dont know why ddlState value loss after AsyncPostBackTrigger


//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: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()
           {
               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;

            }

推荐答案

<script type="text/javascript">
	var ddlState = document.getElementById('<%= ddlState.ClientID %>');
</script>




这篇关于更新面板中AsyncPostBackTrigger之后的Dropdownlist(ddlState)值丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:38