除非有回发事件,否则asp.net GridView不会显示正确的数据

时间:2017-06-20 05:52:11

标签: c# asp.net gridview

在我的gridview中,有一列IsYearly。值可以是1或0.

在页面加载时,所有数据的默认值都是0,但是当我更新数据时,gridview将成为页面加载时的内容。

这是gridview:

<asp:GridView ID="gridView_Holidays" runat="server" AutoGenerateColumns="False" Style="margin-right: 10px; margin-top: 0px;" AllowPaging="True" OnPageIndexChanging="grdHolidays_PageIndexChanging" OnRowDataBound="grdHolidays_RowDataBound" PageSize="10" CssClass="table-grid">
        <Columns>
            <asp:BoundField DataField="HolidayName" HeaderText="Name">
                <HeaderStyle BackColor="#0078D7" ForeColor="White" Width="320px" />
                <ItemStyle BackColor="White" ForeColor="Black" Width="320px" />
            </asp:BoundField>
            <asp:BoundField DataField="HolidayDate" HeaderText="Date">
                <HeaderStyle BackColor="#0078D7" ForeColor="White" Width="256px" />
                <ItemStyle BackColor="White" ForeColor="Black" Width="256px" />
            </asp:BoundField>
            <asp:BoundField DataField="HolidayID" HeaderText="Holiday ID">
                <HeaderStyle BackColor="#0078D7" ForeColor="White" CssClass="HideControl" />
                <ItemStyle BackColor="White" ForeColor="Black" CssClass="HideControl" />
            </asp:BoundField>
            <asp:BoundField DataField="HolidayIsYearly" HeaderText="Holiday Yearly">
                <HeaderStyle BackColor="#0078D7" ForeColor="White" CssClass="HideControl" />
                <ItemStyle BackColor="White" ForeColor="Black" CssClass="HideControl" />
            </asp:BoundField>

            <asp:TemplateField ItemStyle-Width="40px" HeaderText="" ItemStyle-CssClass="tblItem" HeaderStyle-CssClass="header">
                <ItemTemplate>
                    <asp:ImageButton ID="imgBtnEditHoliday" runat="server" OnClick="lnkEditHoliday_Click" ImageUrl="~/Images/EDIT.GIF" ToolTip="Edit" />
                    <asp:ImageButton ID="imgBtnDeleteHoliday" runat="server" Width="16px" OnClick="lnkDeleteHoliday_Click" ImageUrl="~/Images/deleteuser.png" ToolTip="Delete" />
                </ItemTemplate>
                <HeaderStyle BackColor="#0078D7" ForeColor="White" Font-Size="Medium" />
                <ItemStyle BackColor="White" ForeColor="Black" Font-Size="Medium" />
            </asp:TemplateField>
        </Columns>

        <EmptyDataTemplate>
            No results to show.
        </EmptyDataTemplate>
    </asp:GridView>

背后的代码:

protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack) {
                HolidayList();
                imgBtnSearchApprover_Click(this, EventArgs.Empty);
            }
        }

        public void HolidayList() {
            Admin = new List<DTR_Admin>();
            Admin = mServiceHoliday.GetList();
            gridView_Holidays.DataSource = Admin;
            gridView_Holidays.DataBind();
        }

当我单击“保存”按钮时会发生这种情况,其中gridview(我认为)将刷新以显示正确的数据

protected void btnHolidaySave_Click(object sender, ImageClickEventArgs e) {
            if (string.IsNullOrEmpty(txtHolidayName.Text) || string.IsNullOrEmpty(txtHolidayDate.Text)) {
                lblCostCenterError.Text = "Please complete all fields.";
                ModalPopupExtender2.Show();
            }
            else {
                if (Admin.Exists(x => (Convert.ToDateTime(x.HolidayDate) == Convert.ToDateTime(txtHolidayDate.Text) && x.HolidayID != holidayID))) {
                    ClientScript.RegisterStartupScript(GetType(), "load", "alert('Date already exists.');", true);
                    ModalPopupExtender2.Show();
                }
                else {
                    DTR_HolidayService holidayService = new DTR_HolidayService();
                    DTR_Admin pEntity = new DTR_Admin();
                    pEntity.HolidayID = holidayID;
                    pEntity.HolidayDate = txtHolidayDate.Text;
                    pEntity.HolidayName = txtHolidayName.Text;
                    pEntity.HolidayIsYearly = cbYearly.Checked ? 1 : 0;

                    holidayService.EditHoliday(pEntity);
                    HolidayList();
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

我认为您可以使用此代码在数据库代码中设置默认值

'a'

答案 1 :(得分:0)

public byte HolidayIsYearly { get; set; }

请检查此解决方案