Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to maintain drop down selected value during post back in asp.net with c#. I am using a javascript function with which I am assigning values from a pop up window. I am trying to trigger a server side event using __dopostback function in javascript. Hence I could not bind the dropdownlist in the page load (not postback) event either.

What I have tried:

Calling server side event from javascript is as below:
var dialogWindow = window.showModalDialog('ViewOIC.aspx?Bank=' + Bank + '&InstCode=' + InstCode, 'mywindow', 'dialogWidth:875px; dialogHeight:400px; center:yes; status = no; toolbar = no; menubar = no');
        if (dialogWindow != null) {
            
            document.getElementById('ctl00_cphDDS_txtOrgIdCode').value = dialogWindow.OriginatorIdentificationCode;
            document.getElementById('trOriginatorName').style.display = '';
            document.getElementById('trOriginatorName').style.visibility = 'visible';
            document.getElementById('ctl00_cphDDS_lblSelectedOriginator').innerHTML = dialogWindow.CompanyName;
            document.getElementById('ctl00_cphDDS_hidOrgName').value = dialogWindow.CompanyName;
            var OrgIdCode = document.getElementById('ctl00_cphDDS_txtOrgIdCode').value;
            
            //Added by Alex on 19.3.2020 for triggering Server Side Event Start
            __doPostBack('','');
            //Added by Alex on 19.3.2020 for triggering Server Side Event End
        }

Code to bind dropdownlist:
#region FetchPurposeCode_OIC
    private void FetchPurposeCode_OIC(string strOIC)
    {
        try
        {

            if (!string.IsNullOrEmpty(strOIC))
            {
                string OICType = strOIC.Substring(1, 2);
                DataSet ds = BLL_OUT_300.GetDDAPurposeCodeForOIC(OICType);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ddlIssuedFor.DataSource = null;
                    ddlIssuedFor.DataBind();
                    ddlIssuedFor.DataSource = ds;
                    ddlIssuedFor.DataTextField = "DDAPurposeCodeDescription";
                    ddlIssuedFor.DataValueField = "DDAPurposeCode";
                    ddlIssuedFor.DataBind();
                }
                else
                {
                    DataSet dsPurposeCodes = Common.GetDDAPurposeCode(ddlBankType.SelectedValue);
                    if (dsPurposeCodes.Tables[0].Rows.Count > 0)
                    {
                        ddlIssuedFor.DataSource = null;
                        ddlIssuedFor.DataBind();
                        ddlIssuedFor.DataSource = dsPurposeCodes.Tables[0];
                        ddlIssuedFor.DataTextField = "DDAPurposeCodeDescription";
                        ddlIssuedFor.DataValueField = "DDAPurposeCode";
                        ddlIssuedFor.DataBind();
                    }

                }
                ddlIssuedFor.Items.Insert(0, new ListItem("---Select---", "0"));
                ddlIssuedFor.SelectedIndex = 0;
                //ViewState["DDAIssuedFor"] = "binded"; //Added to avoid rebinding cannot move to !ispostback as we are calling dopostback from javascript to invoke as reqd here
            }
            

        }
        catch (Exception ex)
        {
            throw ex;
        }


    }
    #endregion
Posted
Updated 30-Apr-20 20:27pm
v2

1 solution

Quote:
You can use SESSION value for this purpose, using this value you need to find Item from dropdown

protected void Page_Load(object sender, EventArgs e)
{
        
        DataTable subjects = new DataTable();
        if (Page.IsPostBack == false)
        {
            using (SqlConnection con = new SqlConnection(constring))
            {

                SqlDataAdapter adapter = new SqlDataAdapter("SELECT name FROM Table", con);
                adapter.Fill(subjects);

                con.Open();


                DropDownList1.DataSource = subjects;
                DropDownList1.DataTextField = "name";
                DropDownList1.DataValueField = "name";
                DropDownList1.DataBind();


                if(!String.IsNullOrEmpty(Session["selectedval"] as string)) 
                {
                 DropDownList1.SelectedIndex =
                 DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(Session["selectedval"].ToString()));
                }


                con.Close();

            }

  }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string selectedval = DropDownList1.SelectedItem.Value;
        Session["selectedval"] = selectedval;
        Response.Redirect("default.aspx?val="+selectedval);
     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900