【问题标题】:.Net in HTML to return true if reader object not null, otherwise return false如果 reader 对象不为 null,则 HTML 中的 .Net 返回 true,否则返回 false
【发布时间】:2012-10-06 11:20:19
【问题描述】:

我正在使用 DataList 来显示数据库中的一些数据并填充 html 端的字段。我现在需要根据 db 字段是否有数据来更改面板的可见性。

如果相关数据字段有内容,我需要能够显示面板,如果没有,则隐藏它。例如:

    <asp:Panel ID="pnlNew" runat="server" Style="margin:0; padding:0; width:42px; height:18px; bottom:5px; right:10px; float:right; position:relative; background:url(../_imgVideoBadge.png) no-repeat;" Visible='<%# Eval("cheese") != null %>' ToolTip="available"></asp:Panel>

显然,这在可见属性方面不起作用。但希望它能让我了解我想要实现的目标。任何帮助将不胜感激。

我之前看过一些类似的例子:

    a ?? b:c

这如何应用于上述要求??

提前致谢。

【问题讨论】:

    标签: c# asp.net .net c#-4.0 c#-3.0


    【解决方案1】:

    Visible='&lt;%# Information.IsDBNull(Eval("cheese")) %&gt;' 应该返回一个布尔值。

    【讨论】:

    【解决方案2】:

    这是我设法制定的解决方案:

        (Eval("cheese").ToString().Trim() == String.Empty) ? false : true
    

    看起来结果是一个空字符串而不是 null。

    【讨论】:

      【解决方案3】:
      protected void Page_Load(object sender, EventArgs e)
         {
            if (!Page.IsPostBack)          
              SqlConnection MyConnection;
              SqlCommand MyCommand;
              SqlDataReader MyReader;
              MyConnection = new SqlConnection();
              MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AppConnectionString1"].ConnectionString;
      
              MyCommand = new SqlCommand();
              MyCommand.CommandText = "SELECT TOP 10 * From PRODUCTS";
              MyCommand.CommandType = CommandType.Text;
              MyCommand.Connection = MyConnection;
      
              MyCommand.Connection.Open();
              MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
              if (MyReader != null)
              {
                  datalist1.DataSource = MyReader;
                  datalist1.DataBind();
                  pnlNew.Visible = true;
              }
              else
              {
                  pnlNew.Visible = false;
              }
              MyCommand.Dispose();
              MyConnection.Dispose();
          }
      }
      

      【讨论】:

      • 对不起。那真的没有回答这个问题。关键是在 html 端执行此操作。
      • 好的,肯定也会从 HTML 端尝试一下。
      • 那是代码隐藏!除非你很奇怪,否则你不会在 html 中这样做。
      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多