|
Please go through this article.I hope it will help you.
http://codebetter.com/blogs/brendan.tompkins/archive/2005/01/25/48325.aspx
|
|
|
|
|
I read the article.It is very useful but there are some problems that still exist for example using File menu>close to close the Browser window or transfer to another website in the same window.
"I am too late but i will never give up"
|
|
|
|
|
I have 2 textbox and 2 listbox.What I want is when I click on add button all the values of textbox and listbox should get displayed in DataGrid.Then I add a new Record and Datagrid should get refresh with the new record.It should not earse the pervious record.
Finally when I clicked on Save button I need to save all the rows of Datagrid into the Database.
Please help me.
Thanks and Regards.
|
|
|
|
|
Hi,
What you can do is to create a datatable and add the records to that table when you are click add new button. Bind the datagrid with that datatable and refresh it on clicking of add new. Finally on click of save save all the content of the datagrid to the database.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi varshavmane,
I had similar kind of job sometimes earlier. If you dont want a post back on clicking of the ADD button then problem arises. For that you have to add some 'hidden textboxes' to your form.When you click on the ADD button add a new row to the datagrid through javascript ,which is quite easy,and put newly added values to the new datagrid row as well as add your records to the hidden textboxes by comma(,) separator.The newly added row and its values will not be traced by the server, its only for show purpose.You can add as many records as you want.Now finally when you click on the SAVE button take the old records from the datagrid and new records from the hidden textboxes and save them to the database.
In this process you will have only one postback. Otherwise on every click on the ADD button(when creating new records) will cause a postback.
Tirthadip
|
|
|
|
|
No actually I have to use viewstate as I dont have to display the data into datagrid but may need to modify the data.Then Tracing the record into the hidden textbox will be very difficult.
So please help me with Viewstate.
Thanks and Regards.
|
|
|
|
|
Ok then Apurba's idea is good.But keep the datatable to a session varible.Why are u using viewstate?Hope it will solve your problem.
Tirthadip
|
|
|
|
|
Hi,
I respect your idea and Apurba's but actually problem with the session is that I will have to distore session when I exit that form so I dont want to use.
I have never used Viewstate so I want some help/code which stores all the texbox and listbox values in viewstate and how can I retrival the data from viewstate.
Thanks and regards.
varshavmane
|
|
|
|
|
Hi,
Thats not a problem you can use viewstate to store the datatable.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi,
Thanks for the reply.But can u please tell me the syntax of storing the datatable in viewstate.As when I dont use viewstate my datatable gets refresh and because of which I lose the pervious record.
Also how to retrive the values from viewstate for datatable.
I have not used viewstate so I need to try this.
Thanks and regards.
|
|
|
|
|
Hi,
It is quite simple. To put into viewstate you can use:
ViewState["daa"]=ds;
and to retrieve you can use:
DataSet s = new DataSet();<br />
s = (DataSet)ViewState["daa"];<br />
Response.Write(s.Tables[0].Rows[0][0].ToString());
Although here i have used dataset, same way you can use for datatable.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi,
I can do this for textbox and listbox values also???And if I use datatable then will how do I bind it with datagrid???
Thanks and Regards.
|
|
|
|
|
what i told is that first you create a datatable with all the columns which you require and then when you click add new button add a row to that datatable. And you can bind the datagrid with the datatable directly i.e dg.datasourse = datatable.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Fllowing is the code what I have used:
private void ibtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Classes.CChecklist cchecklist = new Classes.CChecklist();
dgChecklist.AutoGenerateColumns = true;
DataTable dt = new DataTable("CheckList");
int cnt;
if(lbISOClauseNo.Items.Count > 0 && lbISODesc.Items.Count > 0 && txtExpected.Text != "" && txtTimeAllocated.Text != "")
{
for(cnt = 0 ; cnt < lbISOClauseNo.Items.Count ; cnt++)
{
if(cnt == lbISOClauseNo.Items.Count -1)
{
htxtFISOClauseNo.Value += lbISOClauseNo.Items[cnt].Value;
htxtFISOClauseDesc.Value += lbISODesc.Items[cnt].Value;
}
else
{
htxtFISOClauseNo.Value += lbISOClauseNo.Items[cnt].Value + "," ;
htxtFISOClauseDesc.Value += lbISODesc.Items[cnt].Value + "," ;
}
}
htxtFExpected.Value = txtExpected.Text;
htxtFTimePlanned.Value = txtTimeAllocated.Text;
cnt = Int32.Parse(htxtFChecklistNo.Value);
cnt++;
htxtFChecklistNo.Value = cnt.ToString();
}
//Checklist No
DataColumn dc = new DataColumn();
dc = dt.Columns.Add("Checklist No",System.Type.GetType("System.Int32"));
dc.Caption = "checklist_no";
//ISO Clause No
dc = dt.Columns.Add("ISO Clause No",System.Type.GetType("System.String"));
dc.Caption = "checklist_isoclause";
//Activities
dc = dt.Columns.Add("Activities",System.Type.GetType("System.String"));
dc.Caption = "checklist_activities";
//Expected Evidence
dc = dt.Columns.Add("Expected",System.Type.GetType("System.String"));
dc.Caption = "checklist_expected";
//Time Planned
dc = dt.Columns.Add("Time Allocated",System.Type.GetType("System.String"));
dc.Caption = "checklist_timeplanned";
DataRow dr = dt.NewRow();
dr["Checklist No"] = htxtFChecklistNo.Value;
dr["ISO Clause No"] = htxtFISOClauseNo.Value;
dr["Activities"] = htxtFISOClauseDesc.Value;
dr["Expected"] = htxtFExpected.Value;
dr["Time Allocated"] = htxtFTimePlanned.Value;
dt.Rows.Add(dr);
//Bind Datatable with DataGrid
dgChecklist.DataSource = dt;
dgChecklist.DataBind();
//Clear all the textbox and listbox.
lbISOClauseNo.Items.Clear();
lbISODesc.Items.Clear();
txtExpected.Text = "";
txtTimeAllocated.Text = "";
htxtFExpected.Value = "";
htxtFISOClauseDesc.Value = "";
htxtFISOClauseNo.Value = "";
htxtFTimePlanned.Value = "";
}
Whenever I click on add button datatable gets created again and I lose my last record.So I am in problem.
Please tell me how do I handle this
|
|
|
|
|
what you do is when the add button is clicked then you check whether you are having the datatable in the viewstate. If yes then use that(datatable in viewstate) to add the new record and if the datatable is not present in the viewstate(it will happen when the add button is clicked for the first time) then create a new datatable and put it into the viewstate. Finally attach that datatable which is there in viewstate to the datagrid.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi,
But how do I check whether datatable is created or not???
Thanks and Regards.
|
|
|
|
|
you can check like this
if(ViewState["daa"]!=null)
{
//Datatable is available in viewstate.
}
else
{
//Not available.
}
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi,
As u said I made the changes in the code but its giving compile time error saying "The name 'dt' does not exist in the class or namespace".I am copied the below:
private void ibtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Classes.CChecklist cchecklist = new Classes.CChecklist();
dgChecklist.AutoGenerateColumns = true;
int cnt;
if(lbISOClauseNo.Items.Count > 0 && lbISODesc.Items.Count > 0 && txtExpected.Text != "" && txtTimeAllocated.Text != "")
{
for(cnt = 0 ; cnt < lbISOClauseNo.Items.Count ; cnt++)
{
if(cnt == lbISOClauseNo.Items.Count -1)
{
htxtFISOClauseNo.Value += lbISOClauseNo.Items[cnt].Value;
htxtFISOClauseDesc.Value += lbISODesc.Items[cnt].Value;
}
else
{
htxtFISOClauseNo.Value += lbISOClauseNo.Items[cnt].Value + "," ;
htxtFISOClauseDesc.Value += lbISODesc.Items[cnt].Value + "," ;
}
}
htxtFExpected.Value = txtExpected.Text;
htxtFTimePlanned.Value = txtTimeAllocated.Text;
cnt = Int32.Parse(htxtFChecklistNo.Value);
cnt++;
htxtFChecklistNo.Value = cnt.ToString();
}
if(ViewState["dtbl"]!=null)
{
//Datatable is available in viewstate.
DataRow dr = dt.NewRow();
dr["Checklist No"] = htxtFChecklistNo.Value;
dr["ISO Clause No"] = htxtFISOClauseNo.Value;
dr["Activities"] = htxtFISOClauseDesc.Value;
dr["Expected"] = htxtFExpected.Value;
dr["Time Allocated"] = htxtFTimePlanned.Value;
dt.Rows.Add(dr);
}
else
{
DataTable dt = new DataTable("CheckList");
//Checklist No
DataColumn dc = new DataColumn();
dc = dt.Columns.Add("Checklist No",System.Type.GetType("System.Int32"));
dc.Caption = "checklist_no";
//ISO Clause No
dc = dt.Columns.Add("ISO Clause No",System.Type.GetType("System.String"));
dc.Caption = "checklist_isoclause";
//Activities
dc = dt.Columns.Add("Activities",System.Type.GetType("System.String"));
dc.Caption = "checklist_activities";
//Expected Evidence
dc = dt.Columns.Add("Expected",System.Type.GetType("System.String"));
dc.Caption = "checklist_expected";
//Time Planned
dc = dt.Columns.Add("Time Allocated",System.Type.GetType("System.String"));
dc.Caption = "checklist_timeplanned";
ViewState["dtbl"] = dt;
DataRow dr = dt.NewRow();
dr["Checklist No"] = htxtFChecklistNo.Value;
dr["ISO Clause No"] = htxtFISOClauseNo.Value;
dr["Activities"] = htxtFISOClauseDesc.Value;
dr["Expected"] = htxtFExpected.Value;
dr["Time Allocated"] = htxtFTimePlanned.Value;
dt.Rows.Add(dr);
}
dgChecklist.DataSource = dt;
dgChecklist.DataBind();
}
Please help me...
Thanks and regards
|
|
|
|
|
Hi,
I have modified the code. You copy and paste this and check.
private void ibtnAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)<br />
{<br />
Classes.CChecklist cchecklist = new Classes.CChecklist();<br />
dgChecklist.AutoGenerateColumns = true;<br />
int cnt;<br />
if(lbISOClauseNo.Items.Count > 0 && lbISODesc.Items.Count > 0 && txtExpected.Text != "" && txtTimeAllocated.Text != "")<br />
{<br />
for(cnt = 0 ; cnt < lbISOClauseNo.Items.Count ; cnt++)<br />
{<br />
if(cnt == lbISOClauseNo.Items.Count -1)<br />
{<br />
htxtFISOClauseNo.Value += lbISOClauseNo.Items[cnt].Value;<br />
htxtFISOClauseDesc.Value += lbISODesc.Items[cnt].Value;<br />
}<br />
else<br />
{<br />
htxtFISOClauseNo.Value += lbISOClauseNo.Items[cnt].Value + "," ;<br />
htxtFISOClauseDesc.Value += lbISODesc.Items[cnt].Value + "," ;<br />
}<br />
}<br />
htxtFExpected.Value = txtExpected.Text;<br />
htxtFTimePlanned.Value = txtTimeAllocated.Text;<br />
cnt = Int32.Parse(htxtFChecklistNo.Value);<br />
cnt++;<br />
htxtFChecklistNo.Value = cnt.ToString();<br />
}<br />
if(ViewState["dtbl"]!=null)<br />
{<br />
DataRow dr = dt.NewRow();<br />
dr["Checklist No"] = htxtFChecklistNo.Value;<br />
dr["ISO Clause No"] = htxtFISOClauseNo.Value;<br />
dr["Activities"] = htxtFISOClauseDesc.Value;<br />
dr["Expected"] = htxtFExpected.Value;<br />
dr["Time Allocated"] = htxtFTimePlanned.Value;<br />
<br />
DataTable dt = (DataTable)ViewState["dtbl"];<br />
dt.Rows.Add(dr);<br />
ViewState["dtbl"] = dt;<br />
dgChecklist.DataSource = (DataTable)ViewState["dtbl"];<br />
dgChecklist.DataBind();<br />
}<br />
else<br />
{<br />
DataTable dt = new DataTable("CheckList");<br />
DataColumn dc = new DataColumn();<br />
dc = dt.Columns.Add("Checklist No",System.Type.GetType("System.Int32"));<br />
dc.Caption = "checklist_no";<br />
<br />
dc = dt.Columns.Add("ISO Clause No",System.Type.GetType("System.String"));<br />
dc.Caption = "checklist_isoclause";<br />
<br />
dc = dt.Columns.Add("Activities",System.Type.GetType("System.String"));<br />
dc.Caption = "checklist_activities";<br />
<br />
dc = dt.Columns.Add("Expected",System.Type.GetType("System.String"));<br />
dc.Caption = "checklist_expected";<br />
<br />
dc = dt.Columns.Add("Time Allocated",System.Type.GetType("System.String"));<br />
dc.Caption = "checklist_timeplanned";<br />
ViewState["dtbl"] = dt;<br />
<br />
DataRow dr = dt.NewRow();<br />
dr["Checklist No"] = htxtFChecklistNo.Value;<br />
dr["ISO Clause No"] = htxtFISOClauseNo.Value;<br />
dr["Activities"] = htxtFISOClauseDesc.Value;<br />
dr["Expected"] = htxtFExpected.Value;<br />
dr["Time Allocated"] = htxtFTimePlanned.Value;<br />
<br />
dt.Rows.Add(dr);<br />
<br />
dgChecklist.DataSource = dt;<br />
dgChecklist.DataBind();<br />
}
And see where you were doing wrong. : -D
Best Regards,
Apurva Kaushal
|
|
|
|
|
Thanks a lot.Now its working.Thanks again.Bye
Regards,
Varsha.
|
|
|
|
|
always welcome.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi again,
Now can u please tell me how do I save that Datagrid into Database???
Thanks and regards,
Varsha
|
|
|
|
|
Hi,
You can send the records from front end as comma separated sting and then in your stored procedure you can separate it and store it into the database.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Hi,
Thanks for reply.Can't we directly fire a query in a loop where I pass the datatable to my Class file.I mean Y do we have to write a Stored Procedure.
Thanks and Regards
|
|
|
|
|
ya sure it can be done like that also. You can go through the records one by one(in a loop) and then execute a insert statement to insert the record in the database.
Best Regards,
Apurva Kaushal
|
|
|
|