2014-12-30 157 views
-2

我正在asp.net上构建网站。 在我的页面中,我有一个日历,用户可以选择连接到SQL Server数据库的已完成手术的日期。我想显示两条不同的消息:避免在日历上加载页面

  1. 如果在选定的日期进行了手术,它将显示一个GridView。我想显示一条消息,说“进行手术”。

  2. 如果没有对选定的日期进行任何手术,我希望有一个标签说:“外科手术不执行”

    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

我编辑了您的标题。请参阅:“[应该在其标题中包含”标签“](http://meta.stackexchange.com/questions/19190/)”,其中的共识是“不,他们不应该”。 –

回答

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: "; 
    } 
} 
} 
+0

它不起作用,因为我想要。 我想要一个事件,可以在处理Gridview之后立即找到行。在手术日期的情况下,它会显示消息,表明没有和GridView出现。如果我再次点击相同的日期,消息会改变(因为gridview已经存在)。 非常感谢。 – user3762965

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(); 
     } 
    } 
} 
相关问题