2017-01-13 3 views
0

ASP.NET WebフォームにEmptyDataTemplateがあり、ユーザーはデータベースにレコードを追加できます。ユーザーの権限に応じて、このEmptyDataTemplateは、データが見つからない場合に表示され、非表示にする必要があります(私はこれが機能しています)。 たとえば、ユーザーには読み取り専用アクセス権があります。特定の条件を検索すると、EmptyDataTemplateが表示されない結果は表示されません。ただし、条件を検索してデータがある場合、データはヘッダーなしで表示されます。EmptyDataTemplateを非表示にしますが、ヘッダーを表示したままにします。

誰かがなぜこのようなことが起こっているのか、周囲に道があるのか​​説明するのに役立つことができますか?

ヘッダーはTemplateFieldsのHeaderTextです。

私はそれが一般的なトリックだと思っています。 ご協力いただきありがとうございます! 私は表示したいTemplateFieldsのHeaderTextです。空のDataTemplateには検索条件に一致するデータの列を表示しないので、表示しません。

編集:

(iは1列のみを示してきたが、マークアップはそれらのすべてについて同じである)ヘッダテキストのマークアップに

 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; 
     } 
} 

:要求されたコードがEmptyDataTemplateを隠すため を添加

<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); 
関連する問題