列表视图的itemupdating事件中的复选框状态

时间:2009-08-04 10:11:06

标签: asp.net listview repeater

我有一个列表视图,显示我们当前的项目。

在listview的itemediting事件处理程序中,我有许多使用嵌套转发器呈现的复选框。渲染后,我循环遍历所有复选框,并根据从DB中检索的数据设置正确的状态。

我的想法是我可以选中或取消选中任何复选框,并将更改保存在数据库中。

我的问题在于itemupdating事件处理程序:我无法保留更改的复选框状态。我重新绑定了嵌套的转发器,但这似乎覆盖了在编辑期间设置的复选框状态。

如果在listview的edititemtemplate中保留由转发器生成的复选框状态的任何指针,将不胜感激!

由于 斯泰恩

3 个答案:

答案 0 :(得分:1)

首先,我在itemediting

绑定rptDepts转发器
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

然后当rptDepts是数据绑定时,我绑定了rptTasks中继器

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

然后,在itemupdating,我重新绑定rptDepts(你说我不应该这样做

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

但是在bllProject.updateProject方法中,我需要能够引用复选框来保存对DB的更改

答案 1 :(得分:0)

如果重新绑定嵌套转发器,它们将从原始数据源更新(覆盖您的更改)。尽量不要重新绑定。

答案 2 :(得分:0)

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;
    }

}