簡體   English   中英

除非發生回發事件,asp.net GridView不會顯示正確的數據

[英]asp.net GridView not showing correct data unless a postback event

在我的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();
        }

這是當我單擊“保存”按鈕時發生的情況,網格視圖(我認為)將在其中刷新,從而顯示正確的數據

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

我認為您可以使用此代碼在數據庫的代碼中設置默認值

public int _HolidayIsYearly= 1;
public int HolidayIsYearly
        {
            get { return _HolidayIsYearly; }
            set { _HolidayIsYearly= value; }
        }
public byte HolidayIsYearly { get; set; }

請檢查此解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM