1

我有一个有 5 个网格的页面。每个网格中的第一个字段是模板内的。在页面加载时,如果不是回发,我会根据从数据库的返回设置此复选框。检查。

如果用户取消选中该复选框,则此 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;
    }
}

数据绑定:

 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,我的数据绑定仅在页面加载时完成。

再次非常感谢您的帮助。

谢谢,

周杰伦

4

0 回答 0