如何使用C#在gridview中显示/隐藏DIV

时间:2014-04-16 20:16:00

标签: c# asp.net gridview

当数据绑定到gridview时,当电子邮件地址为空时,我试图隐藏<div>,但它无效。不知道哪里出错了。请帮忙。

代码背后:

protected void gvReportList_RowDataBound(object sender, GridViewRowEventArgs e)
{
       if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            string emailID = drv["email_list"].ToString();
            string tRepID = drv["REPORT_SCHEDULE_ID_C"].ToString();
            string tDvId = "dv" + tRepID;
            if (emailID == "")
            {
               Response.Write("<script type='text/javascript'>tDvId.style.display = 'none';</script>");
            }
        }
   }
}

标记:

<asp:GridView ID="gvReportList" runat="server">
   <Columns>
      <asp:TemplateField HeaderText="Title of Report" SortExpression="REPORT_NAME_N">
         <ItemTemplate>
            <%# Eval("REPORT_NAME_N")%>
         </ItemTemplate>   
         <ItemStyle Width="50%"/>    
      </asp:TemplateField>
   </Columns> 
   <Columns>
      <asp:TemplateField  ControlStyle-Width="40%" HeaderStyle-Width="45%" HeaderText="Recipients">
         <ItemTemplate>           
             <div id='dv<%# Eval("REPORT_SCHEDULE_ID_C") %>'> 
                  <table id='tbl<%# Eval("REPORT_SCHEDULE_ID_C") %>'><tr><td><%# Eval("email_list")%></td></tr></table>             
             </div>             
          </ItemTemplate> 
      </asp:TemplateField>
   </Columns>  
</asp:GridView>

1 个答案:

答案 0 :(得分:0)

您可以在ItemTemplate中使用ASP脚本...

      <ItemTemplate>
         <% if (!String.IsNullOrEmpty(Eval("email_list"))) { %>
         <div id='dv<%# Eval("REPORT_SCHEDULE_ID_C") %>'> 
              <table id='tbl<%# Eval("REPORT_SCHEDULE_ID_C") %>'><tr><td><%# Eval("email_list")%></td></tr></table>
          <% } %>
         </div>             
      </ItemTemplate>