更新面板中AsyncPostBackTrigger之后的Dropdownlist(ddlState)值丢失 [英] Dropdownlist(ddlState) value loss after AsyncPostBackTrigger in update panel

查看:68
本文介绍了更新面板中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;

            }

推荐答案

我想这与UpdatePanel的工作方式有关。在asyncpostback之后,updatepanel中的所有内容都将从DOM中删除并从AJAX响应中重新创建。

这意味着将删除原始国家/地区下拉列表,并创建新的下拉列表。因此,在您的javaScript中,您将对ddlCountry和ddlState进行无效引用。解决方案是在asyncpostback之后更新这些引用。



将此脚本放在ddlStateDropDownList下面:



I guess this has to do with how UpdatePanel works. After asyncpostback all content inside updatepanel is deleted from DOM and re-created from AJAX response.
Thes means that original country and state dropdowns are deleted, and new ones created. So in your javaScript you'll have invalid references to ddlCountry and ddlState. The solution is to update these references after asyncpostback.

Put this script just below your ddlStateDropDownList:

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


我已经在代码中使用了hiddenfield值,分配给ddlstate.selectedItem.value和onselectedindexchanged ddlState的事件再次分配隐藏值$ / b $ b并检查javaascript中的hiddenfield.value
I have taken hiddenfield value in code behind assign to ddlstate.selectedItem.value and onselectedindexchanged Event of ddlState again assign hidden value
and check hiddenfield.value in javaascript


你需要在数组中实际保存客户端的值并将其放在隐藏字段中,然后使用c锐利并再次绑定下拉,它对我来说很简单
You need to actually hold the values from client side in a array and put it in hidden field ,split then using c sharp and again bind the drop down ,it worked for me simple


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆