2014-12-30 117 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(); 
     } 
    } 
} 
相關問題