2017-01-13 35 views
0

我的ASP.NET Webform上有一個EmptyDataTemplate,它允許用戶向數據庫添加記錄。根據用戶的權限,如果沒有找到數據,這個EmptyDataTemplate需要可見和隱藏(我有這個工作!) 例如,我的用戶只有閱讀權限。當他們搜索一個特定的標準時,沒有顯示結果,他們看不到EmptyDataTemplate。但是,如果他們搜索條件,並且有數據,則數據將顯示在沒有標題的情況下。隱藏EmptyDataTemplate,但留下標題可見

有人可以請幫忙解釋爲什麼會發生這種情況,如果有解決方法嗎?

標題是TemplateField上的HeaderText。

我希望這是一個普通的伎倆。 預先感謝您的幫助! 請注意,它是我想要顯示的TemplateField中的HeaderText - 而不是emptyDataTemplate中的HeaderText,因爲它們將頭部搜索與搜索條件匹配的數據列。

編輯:代碼添加作爲請求 隱藏了EmptyDataTemplate:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     Control control = null; 
     control = GridView1.Controls[0].Controls[0]; 

     if (userManagement.getMIFReadWriteAccess() == "Read") 
     { 
      control.Visible = false; 
      Export_All.Visible = true; 
     } 
     else if (userManagement.getMIFReadWriteAccess() == "Write") 
     { 
      control.Visible = true; 
      Export_All.Visible = true; 
     } 
} 

在標記爲標題文字(我只示出一列,但該標記是相同的爲所有的)

<asp:TemplateField HeaderText="ID"> 
          <ItemTemplate> 
           <asp:Label ID="lbl_Index" runat="server" Text='<%#Eval("id") %>'></asp:Label> 

           <asp:Label ID="lbl_ID" runat="server" Text="" Visible="false"></asp:Label> 
          </ItemTemplate> 
         </asp:TemplateField> 

EmptyDataTemplate:

<EmptyDataTemplate> 
          <div id="emptyData" runat="server"> 
          <tr> 
           <th></th> 
           <th>Serial Number</th> 
           <th>Comments</th> 
           <th>Review Date</th> 
           <th>Approved By</th> 
          </tr> 
          <tr> 
           <td> 
            <asp:Button runat="server" ID="btInsert" Text="In" OnClick="Add" CommandName="EmptyDataTemplate" Class="Button" OnClientClick="return confirm('You are about to confirm this action. Please confirm this action by clicking OK. If you do not wish to do this, please select Cancel.');" /> 
            <br /> 
            <asp:Button runat="server" ID="btInsertOut" Text="Out" OnClick="AddOut" CommandName="EmptyDataTemplate" Class="Button" OnClientClick="return confirm('You are about to confirm this action. Please confirm this action by clicking OK. If you do not wish to do this, please select Cancel.');" /> 
           </td> 
           <td> 
            <asp:TextBox runat="server" ID="tb_Serial_Number" CssClass="text"></asp:TextBox> 
           </td> 
           <td> 
            <asp:TextBox ID="tb_comments" Width="100%" MaxLength="50" runat="server" placeholder="max 50 characters"/> 
           </td> 
           <td> 
            <asp:TextBox ID="tb_reviewDate" runat="server" DataFormatString="{0:dd/mm/yyyy}" Text='<%#Eval("review_by") %>'></asp:TextBox> 
           </td> 
           <td><asp:DropDownList ID="tb_approved_by" runat="server">      
        </asp:DropDownList> </td> 
          </tr> 
</div> 

         </EmptyDataTemplate> 
+0

你可以分享你的代碼 – Raghavendra

+0

嗨,我現在已經增加了。謝謝 – akb29

回答

0

庭審結束後和錯誤,我發現編程方式在C#中添加一個標題行的技巧。可能不是最好的方法,但它的工作。

代碼如下:

#region show the emptyDataTemplate depending on user rights 

    Control control = null; 
    control = GridView1.Controls[0].Controls[0]; 

    if (userManagement.getMIFReadWriteAccess() == "Read") 
    { 
     control.Visible = false; 
     GridViewRow HeaderRow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert); 

     TableCell HeaderCell2 = new TableCell(); 
     HeaderCell2.Text = "Country"; 
     HeaderCell2.ColumnSpan = 1; 
     HeaderRow.Cells.Add(HeaderCell2); 

     //Repeat the above for every column of data you have! 

     GridView1.Controls[0].Controls.AddAt(0, HeaderRow); 
相關問題