我正在asp.net上建立一个网站。 在我的页面中,我有一个日历,用户可以选择连接到SQL Server数据库的已执行手术的日期。我想展示两个不同的消息:
如果在所选日期进行了手术,它将显示GridView。我想要显示一条消息,说“" Surgeries Performed"。
如果在选定日期没有进行任何手术,我想要一个标签说"没有进行手术"
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尚未更新(因此首先显示第一条消息)。 我应该使用哪个活动?
答案 0 :(得分:0)
试试这个
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: ";
}
}
}
答案 1 :(得分:0)
下面是一个简化示例,演示了如何以您希望的方式使用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();
}
}
}