按钮单击动态创建会话

时间:2014-12-24 04:03:16

标签: c# asp.net session

实际上我创建了一个会话对象,我正在存储多个文本框,收音机和放大器。该会话中的复选框值。

这是我的代码(我的插入页面,其中我将插入值并存储在会话中)。

protected void btnRegister_Click(object sender, EventArgs e)
{
    //Creating DataTable dt
    DataTable dt = new DataTable();

    //Creating DataTable Columns
    dt.Columns.Add("FName", typeof(string));
    dt.Columns.Add("MName", typeof(string));
    dt.Columns.Add("LName", typeof(string));
    dt.Columns.Add("Phone", typeof(string));
    dt.Columns.Add("EId", typeof(string));
    dt.Columns.Add("State", typeof(string));
    dt.Columns.Add("City", typeof(string));
    dt.Columns.Add("Country", typeof(string));
    dt.Columns.Add("PCode", typeof(string));
    dt.Columns.Add("Gender", typeof(string));
    dt.Columns.Add("AOI", typeof(string));

    dt.Rows.Add(txtFN.Text, txtMN.Text, txtLN.Text, txtPhone.Text, txtEId.Text, ddlState.Text, ddlCity.Text, txtCountry.Text, txtPCode.Text, rdGender.SelectedValue, cbAOI.SelectedValue);

    //Creating Session object to store DataTable dt
    Session.Add("Insert", dt);

    lblMandatory.Text = "Successfully Inserted into Database";
    System.Threading.Thread.Sleep(1000);
    Response.Redirect("Home.aspx");
} 

我只是想知道,有没有办法在我点击注册按钮时创建新会话,即我不想覆盖现有会话,但是在每次点击按钮时,我想自动创建新会话。

我的主页,我将使用GridView

显示此dataTable
protected void Page_Load(object sender, EventArgs e)   
{  
    if ((!IsPostBack) && (Session["Insert"] != null)) 
    {
        //At another place you retrieve datatable:
        DataTable dt = new DataTable();            
        dt = (DataTable)Session["Insert"];

        //Binding GridView with DataTable dt
        gdvView.DataSource = dt;
        gdvView.DataBind();
   }
}

我的问题是,每次GridView列都被新的GridView列替换,因为我的会话被新值替换。 那么有什么办法,我可以将新的会话值存储到gridview中而不会丢失gridview中的先前值。

3 个答案:

答案 0 :(得分:0)

好的,我现在得到了你的问题。

您不需要每次都创建新会话

这是一个片段;

 DataTable dt;
 if (Session["Insert"]_== null) //or nothing
  {
     dt = new DataTable();
  }
  else
  {
    dt = (DataTable)Session["Insert"];

  }

  //Add to dt.Rows here
  //Save it again to Session["Insert"]
  //Then bind dt to GridView again 

This way you won't need new sessions and your dt data is retained,

答案 1 :(得分:0)

 //Creating DataTable dt
 DataTable dt = new DataTable(); //Declare dt only once. If this wont work put it in a get set property.

protected void Page_Load(object sender, EventArgs e)   
 {  
     if ((!IsPostBack) 
     {
        CreateDataTable();
        LoadDataTable();
     }

    gdvView = Nothing; //or Null;
    gdvView.DataSource = dt;
    gdvView.DataBind();

 }

protected void CreateDataTable()
{

//Creating DataTable Columns
    dt.Columns.Add("FName", typeof(string));
    dt.Columns.Add("MName", typeof(string));
    dt.Columns.Add("LName", typeof(string));
    dt.Columns.Add("Phone", typeof(string));
    dt.Columns.Add("EId", typeof(string));
    dt.Columns.Add("State", typeof(string));
    dt.Columns.Add("City", typeof(string));
    dt.Columns.Add("Country", typeof(string));
    dt.Columns.Add("PCode", typeof(string));
    dt.Columns.Add("Gender", typeof(string));
    dt.Columns.Add("AOI", typeof(string));

 }

protected void LoadDataTable()
{
     if ((Session["Insert"] == null) //or (Session["Insert"] == nothing)
     {
         Session.Add("Insert", dt);
     }
     else {
        dt = (DataTable)Session["Insert"];
     {
}

protected void btnRegister_Click(object sender, EventArgs e)
{

//Do whatever you need to do as is
dt.Rows.Add(txtFN.Text, txtMN.Text, txtLN.Text, txtPhone.Text, txtEId.Text,     ddlState.Text, ddlCity.Text, txtCountry.Text, txtPCode.Text, rdGender.SelectedValue,    cbAOI.SelectedValue);

LoadDataTable();

 /* Remove this block if you don't need it here */
    gdvView = Nothing; //or Null;
    gdvView.DataSource = dt;
    gdvView.DataBind();
 /* Remove this block if you don't need it here */

lblMandatory.Text = "Successfully Inserted into Database";
System.Threading.Thread.Sleep(1000);
Response.Redirect("Home.aspx");
} 

答案 2 :(得分:0)

您可以尝试以下代码。在这里,我正在测试此会话中是否已存在值“插入”。如果是,则将新行添加到其中,否则将创建新的DataTable。

protected void btnRegister_Click(object sender, EventArgs e)
{
    //Creating DataTable dt

 DataTable dt;
 if (Session["Insert"] == null) //or nothing
 {
     dt = new DataTable();

     //Creating DataTable Columns
     dt.Columns.Add("FName", typeof(string));
     dt.Columns.Add("MName", typeof(string));
     dt.Columns.Add("LName", typeof(string));
     dt.Columns.Add("Phone", typeof(string));
     dt.Columns.Add("EId", typeof(string));
     dt.Columns.Add("State", typeof(string));
     dt.Columns.Add("City", typeof(string));
     dt.Columns.Add("Country", typeof(string));
     dt.Columns.Add("PCode", typeof(string));
     dt.Columns.Add("Gender", typeof(string));
     dt.Columns.Add("AOI", typeof(string));
 }
 else
 {
    dt = Session["Insert"] as DataTable;
 }

    dt.Rows.Add(txtFN.Text, txtMN.Text, txtLN.Text, txtPhone.Text, txtEId.Text, ddlState.Text, ddlCity.Text, txtCountry.Text, txtPCode.Text, rdGender.SelectedValue, cbAOI.SelectedValue);

    //Creating Session object to store DataTable dt
    Session["Insert"] = dt;

    lblMandatory.Text = "Successfully Inserted into Database";
    System.Threading.Thread.Sleep(1000);
    Response.Redirect("Home.aspx");
}