GridView CheckBox如果选中默认但用户取消选中,则仍会选中回发复选框

时间:2012-10-19 19:04:07

标签: c# asp.net gridview

我有一个包含5个网格的页面。每个网格中的第一个字段是模板内的字段。在页面加载和如果没有回发我设置此复选框.checked基于从数据库返回。

如果用户取消选中该复选框,则回发时不会保留此checkeboxed.checked = false。有趣的是,如果用户检查未选中的框,则该更改确实会在回发后完成。如果有人对如何修复有任何建议,请告诉我。谢谢!

<asp:GridView ID="commonAdultClinicReferrals" CssClass="Report" runat="server" AutoGenerateColumns="false"
                            Width="270" AllowPaging="false" AlternatingRowStyle-CssClass="DataAlternateRow"
                            CellPadding="1" CellSpacing="1" HeaderStyle-HorizontalAlign="Center" EmptyDataText="No Results."
                            DataKeyNames="Id,Name,Description,FollowUpList"  OnRowDataBound="GridView_RowDataBound">
                            <Columns>
                                <asp:TemplateField ItemStyle-Width="10">
                                    <ItemTemplate>
                                        <center>
                                            <asp:CheckBox ID="selectedRowCb" runat="server"  />
                                        </center>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="Name" HeaderText="Common Adult Clinic Referrals" ItemStyle-Width="250"
                                    ItemStyle-Wrap="true" />
                            </Columns>
                        </asp:GridView>

类别:

public class FollowUp
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public bool FollowUpList { get; set; }
    public bool Selected { get; set; }

    public FollowUp(int id, string name, string description, bool followUpList, bool selected)
    {
        this.Id = id;
        this.Name = name;
        this.Description = description;
        this.FollowUpList = followUpList;
        this.Selected = selected;
    }
}

的DataBind:

 protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        hdnWindowClose.Value = string.Empty;

        loadUi();
    }


}

private void loadUi()
{

    _adultServices = GetFollowUps(2, true);
    adultClinicReferrals.DataSource = _adultServices;
    adultClinicReferrals.DataBind();

}

选中设置复选框:

protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow && !Page.IsPostBack)
    {
        CheckBox cb = ((CheckBox)e.Row.FindControl("selectedRowCb"));
        FollowUp fol = (FollowUp)e.Row.DataItem;
        bool selected = fol.Selected;

        cb.Checked = selected;

    }

}

保存:

 protected void saveButton_Click(object sender, EventArgs e)
{
    GetSelectedCodes(followUpAppointments);
    GetSelectedCodes(adultClinicReferrals);
    GetSelectedCodes(commonAdultClinicReferrals);
    GetSelectedCodes(otherAdultClinicReferrals);
    GetSelectedCodes(pediClinicReferrals);

    hdnWindowClose.Value = "yes";


    JavaScriptSerializer js = new JavaScriptSerializer();
    string str = js.Serialize(_selectedServices);

    hdnFollowUp.Value = str;
    SetNotePickCodes();
}

最后在GetSelectedCodes()内部,如果复选框默认为true,则checkbox.check状态不起作用:

private void GetSelectedCodes(GridView gv)
{

    for (int i = 0; i < gv.Rows.Count; i++)
    {
        GridViewRow row = gv.Rows[i];
        bool isChecked = ((CheckBox)row.FindControl("selectedRowCb")).Checked;

        if (isChecked)
        {
            int id = (Int32)gv.DataKeys[i]["Id"];
            string name = gv.DataKeys[i]["Name"].ToString();
            string description = gv.DataKeys[i]["Description"].ToString();
            bool followup = (bool)gv.DataKeys[i]["FollowUpList"];

            _selectedServices.Add(new FollowUp(id,name,description,followup,true));
        }


    }

if(isChecked)分支正在执行,当我不期望它时。例如,当用户取消选中复选框时。如果不是page.postback,我的数据绑定仅在页面加载时完成。

再次感谢任何帮助。

谢谢,

0 个答案:

没有答案