如何在数据表中设置会话 [英] how to set session in datatables
问题描述
我创建了一个带有3个文本框产品名称,数量,价格和一个按钮添加的表单。
当我点击添加按钮时我有网格和3个文本框值应该插入网格中它正在插入。
现在我想添加不同的值,我再次插入文本框但现在它显示我在网格中的当前值,以前的值不保留...
$ b $我试过以下代码。
请告诉我在哪里添加会话以便我也可以保留以前的值。
i have created one form with 3 textboxes product name,qty,price and one button add.
when i click on add button i have grid and 3 textboxes values should be inserted in grid and it is getting inserted.
now i want to add different values which i again insert in textboxes but now it shows me current values in grid and previous values are not retaining...
i have tried following code.
pls tell where to add sessions so that i can retain previous values too.
public partial class invoice : System.Web.UI.Page
{
public SqlDataReader reader;
Query.product objprdt = new product(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
Query.customer objcus = new customer(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
Query.cal objcal = new cal(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString());
DataTable dt1 = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = objprdt.getproducts();
ddlprdcts.DataSource = dt;
ddlprdcts.DataValueField = "Product_Id";
ddlprdcts.DataTextField = "Product_Name";
ddlprdcts.DataBind();
ddlprdcts.Items.Add(new ListItem("- Please Select -", "-1"));
ddlprdcts.SelectedValue = "-1";
DataColumn dc1 = new DataColumn("Product Name");
DataColumn dc2 = new DataColumn("Qty");
DataColumn dc3 = new DataColumn("Product Price");
DataColumn dc4= new DataColumn("Amount");
dt1.Columns.Add(dc1);
dt1.Columns.Add(dc2);
dt1.Columns.Add(dc3);
dt1.Columns.Add(dc4);
DataRow dr1 = dt.NewRow();
GridView1.DataSource = dt1;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
DataRow dr1 = dt1.NewRow();
dr1[0] = TextBox2.Text;
dr1[1] = TextBox4.Text;
dr1[2] =TextBox3.Text;
dr1[3] =TextBox5.Text;
dt1.Rows.Add(dr1);
GridView1.DataSource = dt1;
GridView1.DataBind();
}
在哪里使用会话以便我可以添加尽可能多的值网格应保留所有
注意:我不想保存到数据库中
请告诉
问候
where to use session so that i can add as many values grid should retain all
NOTE: I dnt want to save into database
pls tell
regards
推荐答案
尝试以下代码
Try the following code
protected void Button2_Click(object sender, EventArgs e)
{
if(Session["dt1"] != null)
{
dt1 = (DataTable)Session["dt1"];
}
DataRow dr1 = dt1.NewRow();
dr1[0] = TextBox2.Text;
dr1[1] = TextBox4.Text;
dr1[2] =TextBox3.Text;
dr1[3] =TextBox5.Text;
dt1.Rows.Add(dr1);
GridView1.DataSource = dt1;
GridView1.DataBind();
Session["dt1"] = dt1;
}
您好,请尝试以下代码
hi try the following code
Button_Click()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("Name");
dt.Columns.Add("Address");
dt.Columns.Add("Number");
//First fill all the date present in the grid
for (int intCnt = 0; intCnt < grd.Rows.Count; intCnt ++)
{
if (grd.Rows[intCnt].RowType == DataControlRowType.DataRow)
{
dr = dt.NewRow();
dr["Name"] = grd.Rows[intCnt].Cells[0].Value;
dr["Address"] = grd.Rows[intCnt].Cells[1].Value;
dr["Number"] = grd.Rows[intCnt].Cells[2].Value;
dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["Name"] = txt1.Text;
dr["Address"] = txt2.Text;
dr["Number"] = txt3.Text;
dt.Rows.Add(dr);
grd.DataSource = dt;
grd.DataBind();
}
如果我犯了任何语法错误,请更正,因为我直接在记事本中写了这段代码
Please make corrections if I made any syntactic mistake because I wrote this code directly in the notepad
这篇关于如何在数据表中设置会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!