.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Checkbox Checking in Datalist in asp.net

Posted By: pavani     Posted Date: July 04, 2009    Points:2   Category :ASP.Net
Hi
I have a Datalist where i have written code for page navigation using PagedDataSource class.I have checkbox column in datalist now my problem is when i check some box in the first page and navigate to second page and come back to first page am loosing my checkbox status that is checked in datalist
I donot want to loose the status of my checkbox during page navigation.i have tried to viewstate concept by making enableviewstate as true for datalist and also for datalist but its not working
As said by one of our frds
Using ArrayList i stored checked items in a session.In ItemDataBound Event I assign back the checked item which is available in arraylist session variable .am getting an exception
unable to cast System.String to System.ArrayList
This is my code jst have alook
ArrayList al = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
foreach (DataListItem dll in DataList1.Items)
{

CheckBox chk = (CheckBox)dll.FindControl("CheckBox2");
if (chk.Checked == true)
{

al.Add(chk);
Session["checkedboxes"] = al.ToString();
//Response.Write("" + al.Count);

}
}
if (!Page.IsPostBack)
{
BindList();//binding datalist


}
}
//BinList method
public void BindList()
{

PagedDataSource pds = new PagedDataSource();
try
{

DataSet ds = Class1.Getstudentdetails("11", "8");
pds.AllowPaging = true;
pds.DataSource = ds.Tables["Student"].DefaultView;
pds.PageSize = 3;
pds.CurrentPageIndex = CurrentPage;

DataList1.DataSource = pds;
al = (ArrayList)Session["checkedboxes"];
DataList1.DataBind();

}
catch (Exception ex)
{
throw ex;
}
protected void lbtnPrev_Click(object sender, System.EventArgs e)
{
CurrentPage -= 1;
al = (ArrayList)Session["checkedboxes"];
BindList();

}
protected void lbtnNext_Click(object sender, System.EventArgs e)
{

CurrentPage += 1;
al = (ArrayList)Session["checkedboxes"];
BindList();


}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
al = (ArrayList)Session["checkedboxes"];
BindList();
}
Is my code wrong?
Then i have one more problem
in 1st page i checked three checkboxes, so my arralist constists 3 k in 2nd page i chekd 2 i will add my arralist is 5 i go back 2 1st page then how wil iknow in 5 items which r checked in ist page and soon
Please help me out
Thanks in advance

Responses
Author: Amit Mehra             
Posted Date: July 04, 2009     Points: 5   

Hi,
Good..you are going in right direction. Here is the wrong one you are assigning string value to session

Session["checkedboxes"] = al.ToString();


And here you are type casting to arraylist.

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
al = (ArrayList)Session["checkedboxes"];
BindList();
}

Now you can change the code

This
Session["checkedboxes"] = al.ToString();

With this one
Session["checkedboxes"] = al;

The second question you have asked you need to have some ID associated with each checkbox say CustomerId and that customerID you have to store in an arraylist.

Thanks,
Amit
Please "Mark As Answer " if this post help you.


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend