views:

605

answers:

3

I have a listview displaying our current projects.

In the itemediting event handler of the listview, I have a number of checkboxes that are being rendered using nested repeaters. After rendering, I loop through all checkboxes and set the correct state based on data retrieved from the DB.

The idea is that I can check or uncheck any of the checkboxes, and the changes are saved in the db.

My problem lies with the itemupdating event handler: I am unable to retain the changed checkbox states. I rebind the nested repeaters, but this seems to overwrite the checkbox states that were set during editing.

Any pointers on how to retain checkbox states generated by a repeater in the edititemtemplate of a listview would be greatly appreciated!

Thanks Stijn

A: 

If you rebind the nested repeaters, they will be updated from the original datasource (overwriting your changes). Try not rebinding.

Max
Thanks Max, but how can I then reference the checkboxes at the itemupdating event without rebinding?
Stijn Van Loo
Could you please post a code sample? Thanks!
Max
Thanks for providing the code. It's a bit more complicated than I thought in the beginning. I'll try to look into it when I have more time.Anyone else has suggestions to help Stijn?
Max
+1  A: 

First I bind the rptDepts repeater at itemediting

Public Sub lvProjects_OnItemEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewEditEventArgs)

    Dim rptDepts As Repeater = lvProjects.EditItem.FindControl("rptDepts")
    rptDepts.DataSource = bllDept.getServices()
    rptDepts.DataBind()

    'get tasks for projectID
    Dim hdnprojectID As HiddenField = lvProjects.EditItem.FindControl("hdnStudyID")
    getTasks(hdnProjectID.Value, rptDepts)

End Sub

Then when rptDepts is databound, I bind the rptTasks repeater

Protected Sub lvDepts_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

    'get tasks for service               
    Dim rptTasks As Repeater = e.Item.FindControl("rptTasks")
    rptTasks.DataSource = bllDept.getTasksForService(e.Item.DataItem("pk_dept_id"))
    rptTasks.DataBind()

End Sub

Then, at itemupdating, I rebind rptDepts (which you said I shouldn't do

Public Sub lvProjects_OnItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs)

    'this item
    Dim itmProject As ListViewItem = lvProjects.Items(e.ItemIndex)

    'rebind depts
    'Dim rptDepts As Repeater = itmProject.FindControl("rptDepts")
    'rptDepts.DataSource = bllDept.getServices()
    'rptDepts.DataBind()

    'update project
    bllProject.updateProject(itmProject, lblTest)

    'unset edit status
    lvProjects.EditIndex = -1

    'success message                
    pnlFeedback.CssClass = "success"
    ltlFeedback.Text = "Project <b>" & txtName.Text & "</b> was successfully updated."

    'rebind
    bindProjects()

End Sub

But in the bllProject.updateProject method, I need to be able to reference the checkboxes to save the changes to the DB

Stijn Van Loo
A: 

Checked='<%# Eval("PreAcqClaim") ==DBNull.Value?false:true %>' MARKUP

------------------CODE BEHIND-------------------------------- using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

public partial class Claim : System.Web.UI.Page { CheckBox OurFaultCheckBox = new CheckBox(); CheckBox PicturesCheckBox = new CheckBox(); CheckBox ReportedInsCheckBox = new CheckBox(); CheckBox ReportLateCheckBox = new CheckBox(); CheckBox AssistRepairCheckBox = new CheckBox(); CheckBox LitigationCheckBox = new CheckBox(); CheckBox PreAcqClaimCheckBox = new CheckBox();

DetailsDataTableAdapters.tblClaimsTableAdapter _adapter = new DetailsDataTableAdapters.tblClaimsTableAdapter();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        lvDetails.DataSource = _adapter.GetDataByPK_Claim_ID(Convert.ToInt32(Request.QueryString["PK_Claim_ID"]));
        lvDetails.DataBind();
    }
}
protected void objDetails_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{

}
protected void lvDetails_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    //CheckBox OurFaultCheckBox = (CheckBox)lvDetails.FindControl("OurFaultCheckBox");
    //OurFaultCheckBox.Checked = true;
    //OurFaultCheckBox = (CheckBox)e.Item.FindControl("OurFaultCheckBox");
    //PicturesCheckBox = (CheckBox)e.Item.FindControl("PicturesCheckBox");
    //ReportedInsCheckBox = (CheckBox)e.Item.FindControl("ReportedInsCheckBox");
    //ReportLateCheckBox = (CheckBox)e.Item.FindControl("ReportLateCheckBox");
    //AssistRepairCheckBox = (CheckBox)e.Item.FindControl("AssistRepairCheckBox");
    //LitigationCheckBox = (CheckBox)e.Item.FindControl("LitigationCheckBox");
    //PreAcqClaimCheckBox = (CheckBox)e.Item.FindControl("PreAcqClaimCheckBox");
}
protected void objDetails_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
    CheckBox OurFaultCheckBox = (CheckBox)lvDetails.FindControl("OurFaultCheckBox");
    e.InputParameters.Add("OurFaultCheckBox", OurFaultCheckBox.Checked);
}
protected void objDetails_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
    e.InputParameters.Add("OurFault", OurFaultCheckBox.Checked);
    e.InputParameters.Add("Pictures", PicturesCheckBox.Checked);
    e.InputParameters.Add("ReportedIns", ReportedInsCheckBox.Checked);
    e.InputParameters.Add("ReportLate", ReportLateCheckBox.Checked);
    e.InputParameters.Add("AssistRepair", AssistRepairCheckBox.Checked);
    e.InputParameters.Add("Litigation", LitigationCheckBox.Checked);
    e.InputParameters.Add("PreAcqClaim", PreAcqClaimCheckBox.Checked);
}
protected void lvDetails_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
    DetailsDataTableAdapters.tblClaimsTableAdapter _adapter = new DetailsDataTableAdapters.tblClaimsTableAdapter();


    OurFaultCheckBox = (CheckBox)lvDetails.EditItem.FindControl("OurFaultCheckBox");
    PicturesCheckBox = (CheckBox)lvDetails.EditItem.FindControl("PicturesCheckBox");
    ReportedInsCheckBox = (CheckBox)lvDetails.EditItem.FindControl("ReportedInsCheckBox");
    ReportLateCheckBox = (CheckBox)lvDetails.EditItem.FindControl("ReportLateCheckBox");
    AssistRepairCheckBox = (CheckBox)lvDetails.EditItem.FindControl("AssistRepairCheckBox");
    LitigationCheckBox = (CheckBox)lvDetails.EditItem.FindControl("LitigationCheckBox");
    PreAcqClaimCheckBox = (CheckBox)lvDetails.EditItem.FindControl("PreAcqClaimCheckBox");

    try
    {
        _adapter.Update("eventNum", "jobNum","test", "1", DateTime.Now, "", "", "",
                        "", "", "", DateTime.Now, "", "", "", "54143", "", "", "",
                        OurFaultCheckBox.Checked, PicturesCheckBox.Checked,
                        ReportedInsCheckBox.Checked, ReportLateCheckBox.Checked,
                        AssistRepairCheckBox.Checked, LitigationCheckBox.Checked,
                        PreAcqClaimCheckBox.Checked,
                        Convert.ToInt32(Request.QueryString["PK_Claim_ID"]));
    }
    catch (Exception ex)
    {

    }
    lvDetails.EditIndex = -1;

}
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{

}

protected void lvDetails_ItemEditing(object sender, ListViewEditEventArgs e)
{
    lvDetails.EditIndex = e.NewEditIndex;
}

}

nick