senerio
-
Hi, my page postbacks many times and this button click occurs in between postbacks(one postback occurs due to button click). when a particular button is clicked for the first time, a snippet will be executed and for other clicks of same button 'this' snippet should not be executed but some other code should be executed and for all clicks(including first click) it should remember(append the values to gridview) the previous values selected. how can i achieve this senerio. IsPostBack is not applicable. Is there anyway to check clicks and save values in gridview (gridview is not in stored in DB).. hope i am clear.. any doubts on my question are welcome.... Thanks in advance
-
Hi, my page postbacks many times and this button click occurs in between postbacks(one postback occurs due to button click). when a particular button is clicked for the first time, a snippet will be executed and for other clicks of same button 'this' snippet should not be executed but some other code should be executed and for all clicks(including first click) it should remember(append the values to gridview) the previous values selected. how can i achieve this senerio. IsPostBack is not applicable. Is there anyway to check clicks and save values in gridview (gridview is not in stored in DB).. hope i am clear.. any doubts on my question are welcome.... Thanks in advance
If I understand your question correctly then I believe that a Session variable can help you. You page should have at least two event handlers, one for Page_Load and one for your button OnClick event. Page_Load always executes first. When the page first loads it goes through Page_Load with IsPostBack == false and When you click the button it first calls Page_Load with IsPostBack == true, then it executes your Button_Click event where you should have the code snippet that you only want to execute one time. Let's say here's your button click event handler: public void MyButton_OnClick(object sender, EventArgs args) { if (Session("MyButton_Handled") == null) { // Your one-time code snippet goes here // Set the session varaible Session("MyButton_Handled") = true; } } First time through the session variable will be undefined, which should return null. After you execute the code then the variable is defined and the one-time code gets skipped. Ray Wampler www.RayWampler.com
-
If I understand your question correctly then I believe that a Session variable can help you. You page should have at least two event handlers, one for Page_Load and one for your button OnClick event. Page_Load always executes first. When the page first loads it goes through Page_Load with IsPostBack == false and When you click the button it first calls Page_Load with IsPostBack == true, then it executes your Button_Click event where you should have the code snippet that you only want to execute one time. Let's say here's your button click event handler: public void MyButton_OnClick(object sender, EventArgs args) { if (Session("MyButton_Handled") == null) { // Your one-time code snippet goes here // Set the session varaible Session("MyButton_Handled") = true; } } First time through the session variable will be undefined, which should return null. After you execute the code then the variable is defined and the one-time code gets skipped. Ray Wampler www.RayWampler.com
hi, thanks a lot for your reply, this is what i wanted, but i need the 'dt' value outside the if(session...) statement.As this is giving me "the name 'dt' doesnot exist in the current context" error. And if i create 'dt' outside this session statement, dt is initializing for every postback... foreach (GridViewRow row in GridView1.Rows) { CheckBox box1 = (CheckBox)row.FindControl("CheckBox1"); if (box1.Checked) { if (Session["MyButton_Handled"] == null) { DataTable dt = new DataTable(); dt.Columns.Add("Items"); dt.Columns.Add("Price"); Session["MyButton_Handled"] = true; } DataTable dt = (DataTable)Session["tablevalues"]; DataRow dr = dt.NewRow(); string name1 = row.Cells[2].Text; double cost1 = Convert.ToDouble(row.Cells[3].Text); dr["Items"] = name1; dr["Price"] = cost1; dt.Rows.Add(dr); GridView2.DataSource = dt; GridView2.DataBind(); Session["tablevalues"] = dt; cost = cost + cost1; Label12.Visible = true; Label12.Text = "Total Price " + cost ; } thanks,
-
hi, thanks a lot for your reply, this is what i wanted, but i need the 'dt' value outside the if(session...) statement.As this is giving me "the name 'dt' doesnot exist in the current context" error. And if i create 'dt' outside this session statement, dt is initializing for every postback... foreach (GridViewRow row in GridView1.Rows) { CheckBox box1 = (CheckBox)row.FindControl("CheckBox1"); if (box1.Checked) { if (Session["MyButton_Handled"] == null) { DataTable dt = new DataTable(); dt.Columns.Add("Items"); dt.Columns.Add("Price"); Session["MyButton_Handled"] = true; } DataTable dt = (DataTable)Session["tablevalues"]; DataRow dr = dt.NewRow(); string name1 = row.Cells[2].Text; double cost1 = Convert.ToDouble(row.Cells[3].Text); dr["Items"] = name1; dr["Price"] = cost1; dt.Rows.Add(dr); GridView2.DataSource = dt; GridView2.DataBind(); Session["tablevalues"] = dt; cost = cost + cost1; Label12.Visible = true; Label12.Text = "Total Price " + cost ; } thanks,
Now that I see your code I think that I can suggest something that will work. You're already storing the data table in session, so you don't need an additional session variable your code can check for the existence of the data table in session. If I understand the issue correctly then what you want is the first time through to create the table and store it in session, and each subsequent time you want to add a new row to the existing table. First you create a helper function that returns a data table. This is where you check the session:
private DataTable ItemTable()
{
if (Session["tablevalues"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Items");
dt.Columns.Add("Price");
Session["tablevalues"] = dt;
}
return dt;
}The above code will create the data table in session if it does not exist and then return it. It it already exists then it returns what's there. You now change your button handler as follows:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox box1 = (CheckBox)row.FindControl("CheckBox1");
if (box1.Checked)
{
DataTable dt = ItemTable();DataRow dr = dt.NewRow(); string name1 = row.Cells\[2\].Text; double cost1 = Convert.ToDouble(row.Cells\[3\].Text); dr\["Items"\] = name1; dr\["Price"\] = cost1; dt.Rows.Add(dr); GridView2.DataSource = dt; GridView2.DataBind(); cost = cost + cost1; Label12.Visible = true; Label12.Text = "Total Price " + cost ; Session\["tablevalues"\] = dt;
}
The line of code, DataTable dt = ItemTable(), is going to get either a new table or table with the rows you added. You then add your new row, update the grid, and store the table in session. I hope that's what you were looking for... Ray Wampler http://wwww.RayWampler.com
-
Now that I see your code I think that I can suggest something that will work. You're already storing the data table in session, so you don't need an additional session variable your code can check for the existence of the data table in session. If I understand the issue correctly then what you want is the first time through to create the table and store it in session, and each subsequent time you want to add a new row to the existing table. First you create a helper function that returns a data table. This is where you check the session:
private DataTable ItemTable()
{
if (Session["tablevalues"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Items");
dt.Columns.Add("Price");
Session["tablevalues"] = dt;
}
return dt;
}The above code will create the data table in session if it does not exist and then return it. It it already exists then it returns what's there. You now change your button handler as follows:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox box1 = (CheckBox)row.FindControl("CheckBox1");
if (box1.Checked)
{
DataTable dt = ItemTable();DataRow dr = dt.NewRow(); string name1 = row.Cells\[2\].Text; double cost1 = Convert.ToDouble(row.Cells\[3\].Text); dr\["Items"\] = name1; dr\["Price"\] = cost1; dt.Rows.Add(dr); GridView2.DataSource = dt; GridView2.DataBind(); cost = cost + cost1; Label12.Visible = true; Label12.Text = "Total Price " + cost ; Session\["tablevalues"\] = dt;
}
The line of code, DataTable dt = ItemTable(), is going to get either a new table or table with the rows you added. You then add your new row, update the grid, and store the table in session. I hope that's what you were looking for... Ray Wampler http://wwww.RayWampler.com
oops, I just realized the first function has one line that's incorrect. It should be:
private DataTable ItemTable()
{
if (Session["tablevalues"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Items");
dt.Columns.Add("Price");
Session["tablevalues"] = dt;
}
return Session["tablevalues"];} After your if statement then Session["tablevalues"] will exist. The code will either create it for the first time or will return what's currently stored there.
-
oops, I just realized the first function has one line that's incorrect. It should be:
private DataTable ItemTable()
{
if (Session["tablevalues"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Items");
dt.Columns.Add("Price");
Session["tablevalues"] = dt;
}
return Session["tablevalues"];} After your if statement then Session["tablevalues"] will exist. The code will either create it for the first time or will return what's currently stored there.
thanks a lot.. it worked... thanks once again