本文介绍了如何使用会话绑定下拉框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用会话绑定下拉框?
第一页
How to bind dropdown box using session ?
1st page
protected void Page_Load(object sender, EventArgs e)
{
LoadRetrieve();
Session["ddcity"] = ddCity.Items;
}
public void LoadRetrieve()
{
SqlConnection conn = new SqlConnection(ConnString);
string selectSql = "Select * from quest_categories ";
SqlCommand cmd = new SqlCommand(selectSql, conn);
conn.Open();
ddCity.DataSource = cmd.ExecuteReader();
ddCity.DataTextField = "City"; // Column Name
ddCity.DataValueField = "Sr_No"; // Primary Key
this.DataBind();
conn.Close();
}
2n页
2n page
protected void Page_Load(object sender, EventArgs e)
{
// this is not the correct code... I want correct one
foreach (ListItem item in Session["ddcity"].ToString())
{
DropDownList1.Items.Add(item);
}
}
谢谢...
Thanks...
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
LoadRetrieve();
}
public void LoadRetrieve()
{
SqlConnection conn = new SqlConnection(ConnString);
string selectSql = "Select * from quest_categories ";
SqlCommand cmd = new SqlCommand(selectSql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
conn.Close();
Session["ddcity"] = dt;
ddCity.DataSource = dt;
ddCity.DataTextField = "City"; // Column Name
ddCity.DataValueField = "Sr_No"; // Primary Key
ddCity.DataBind();
}
第二页
Second Page
protected void Page_Load(object sender, EventArgs e)
{
if(Session["ddcity"]!=null)
{
DataTable dt = (DataTable) Session["ddcity"];
ddCity.DataSource = dt;
ddCity.DataTextField = "City"; // Column Name
ddCity.DataValueField = "Sr_No"; // Primary Key
ddCity.DataBind();
}
}
如果您觉得有用,那就别忘了接受解决方案并投票:)
if you feel it useful then don''t forget to accept the solution and vote it up :)
这篇关于如何使用会话绑定下拉框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!