避免在日历上加载页面
本文关键字:加载 日历 | 更新日期: 2023-09-27 18:35:51
我正在 asp.net 上建立一个网站。在我的页面中,我有一个日历,用户可以在其中选择连接到SQL Server数据库的已执行手术的日期。我想显示两条不同的消息:
-
如果在所选日期进行了手术,它将显示 GridView。我想显示一条消息,上面写着"已执行手术"。
-
如果当天没有进行任何手术,我想要一个标签,上面写着"未进行手术"
protected void Page_Load(object sender, EventArgs e) { GridView1.DataSourceID = ""; GridView1.DataSource = SqlDataSource1; GridView1.DataBind(); GridView1.Visible = true; SurgeonsLabel.Visible = false; NursesLabel.Visible = false; if (GridView1.Rows.Count == 0) { UnscheduledSurgery.Text = "No Surgeries had been performed on the selected date. Please select other date."; } else { UnscheduledSurgery.Text = "Surgeries performed on the selected date: "; } }
我找不到可以为这项工作编写代码的事件。我无法在页面加载时执行此操作,因为它总是显示某些内容,而且我也无法使其在Calendar_Selected上运行,因为 GridView1 尚未更新(因此首先显示第一条消息)。我应该使用哪个事件?
试试这个
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
GridView1.Visible = true;
SurgeonsLabel.Visible = false;
NursesLabel.Visible = false;
if (GridView1.Rows.Count == 0)
{
UnscheduledSurgery.Text = "No Surgeries had been performed on the selected date. Please select other date.";
}
else
{
UnscheduledSurgery.Text = "Surgeries performed on the selected date: ";
}
}
}
下面是
一个简化的示例,演示如何按所需方式使用 GridView。 您必须根据自己的具体情况进行调整,但它说明了在没有数据时如何显示消息。 我在Visual Studio中编写了这段代码,因此它已经过测试并且可以工作。 试试吧。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
<EmptyDataTemplate>
There is no data.
</EmptyDataTemplate>
</asp:GridView>
<asp:Button runat="server" ID="btnShow" Text="Show" OnClick="btnShow_Click" />
<asp:Button runat="server" ID="btnHide" Text="Hide" OnClick="btnHide_Click" />
</div>
</form>
</body>
后面的代码如下所示。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataBind();
}
protected void btnShow_Click(object sender, EventArgs e)
{
var data = new Dictionary<int, string>();
data.Add(1, "John");
data.Add(2, "Bob");
GridView1.DataSource = data;
GridView1.DataBind();
}
protected void btnHide_Click(object sender, EventArgs e)
{
GridView1.DataSource = null;
GridView1.DataBind();
}
}
}