How to display cascading drop down from selected one page into another page

Posted by Raghuldrag under C# on 5/2/2014 | Points: 10 | Views : 867 | Status : [Member] | Replies : 1
Hi Friends,


in my application i m using cascading dropdown with stored values from databases.

my application shows on page1.aspx:


state: drop1

district: drop2


show marker: button


here the code for cascading dropdown is....


protected void BindContrydropdown()
{
conn.Open();

SqlCommand cmd = new SqlCommand("select * from ind_state", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
ddlstate.DataSource = ds;
ddlstate.DataTextField = "ind_state_name";
ddlstate.DataValueField = "ind_stat_id";
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));




}




protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{



conn.Open();
SqlCommand cmd = new SqlCommand(" Select * From ind_state_dist where ind_stat_id =" + ddlstate.SelectedValue, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
ddldist.DataSource = ds;
ddldist.DataTextField = "ind_dist_name";
ddldist.DataValueField = "ind_dist_id";
ddldist.DataBind();
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));
if (ddlstate.SelectedValue == "0")
{
ddldist.Items.Clear();
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));
}

}


when i choose the state depends on district ll display here .....


for example i choose "TAMILNADU" and district "CHENNAI"


then press show Marker button


Session["DlistIndex"] = ddlstate.SelectedValue;

Session["DlistInd"] = ddldist.SelectedValue;
Response.Redirect(ddlstate.SelectedValue + ".aspx");


in my page2.aspx:


pageload:

if (!IsPostBack)
{

if (Session["DlistIndex"].ToString() != null )
{

stateid = Convert.ToInt32(Session["DlistIndex"]);

ddlstate.SelectedValue = stateid.ToString();

}


if (Session["DlistInd"].ToString() != null)
{
di = Convert.ToInt32(Session["DlistInd"]);
ddldist.SelectedValue = di.ToString();

}




BindRepeaterData();
BindContrydropdown();

}


here both stateid & di ll get the selected value of page1.aspx
i copied same code for drop down above in page2.aspx also......


here the problem is state dropdown ll dispaly the selected value from page1.aspx
but the district ll display "--select--" only..

why it was not fetching distric from database not shown selected dropdown?




Responses

Posted by: Allemahesh on: 5/2/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
I got the problem.

Just change you code as below:-

In page one:-

protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
BindSatedropdown(ddlstate.SelectedValue);
}

protected void BindSatedropdown(int _SateID)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * From ind_state_dist where ind_stat_id =" + _SateID, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
ddldist.Items.clear();
ddldist.DataSource = ds;
ddldist.DataTextField = "ind_dist_name";
ddldist.DataValueField = "ind_dist_id";
ddldist.DataBind();
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));
if (ddlstate.SelectedValue == "0")
{
ddldist.Items.Clear();
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));
}
}


In Page two:-

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindContrydropdown();

if (Session["DlistIndex"].ToString() != null)
{
stateid = Convert.ToInt32(Session["DlistIndex"]);
ddlstate.SelectedValue = stateid.ToString();
}

if (Session["DlistInd"].ToString() != null)
{
di = Convert.ToInt32(Session["DlistInd"]);
BindSatedropdown(di);
ddldist.SelectedValue = di.ToString();
}
BindRepeaterData();
}
}

protected void ddlstate_SelectedIndexChanged(object sender, EventArgs e)
{
BindSatedropdown(ddlstate.SelectedValue);
}

protected void BindSatedropdown(int _SateID)
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select * From ind_state_dist where ind_stat_id =" + _SateID, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
ddldist.Items.clear();
ddldist.DataSource = ds;
ddldist.DataTextField = "ind_dist_name";
ddldist.DataValueField = "ind_dist_id";
ddldist.DataBind();
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));
if (ddlstate.SelectedValue == "0")
{
ddldist.Items.Clear();
ddldist.Items.Insert(0, new ListItem("--Select--", "0"));
}
}



Happy Coding.

Raghuldrag, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response