【问题标题】:C# ASP.NET insert html code into repeater based on the data from dbC# ASP.NET 根据 db 中的数据将 html 代码插入转发器
【发布时间】:2017-07-07 00:52:41
【问题描述】:

所以再次需要帮助

抱歉有点混乱,但是我正在尝试创建一个用户可以提交然后从数据库中再次检索它的聊天框,我正在关注这个脚本https://bootsnipp.com/snippets/EkQe7

可以看到,从HTML代码来看,有两种,一种是你聊天的时候,一种是别人聊天的时候,

<div class="chat">   
      <div class="chat-history">
        <ul class="chat-ul">
          <li>
            <div class="message-data">
              <span class="message-data-name"><i class="fa fa-circle you"></i> You</span>
            </div>
            <div class="message you-message">
            A new client?!?! I would love to help them, but where are we going to find the time?

            </div>
          </li>
          <li class="clearfix">
            <div class="message-data align-right">
              <span class="message-data-name">Ada, your OperationsAlly</span> <i class="fa fa-circle me"></i>
            </div>
            <div class="message me-message float-right"> We should take a look at your onboarding and service delivery workflows, for most businesess there are many ways to save time and not compromise quality.  </div>
          </li>

        </ul>

      </div> <!-- end chat-history -->

    </div> <!-- end chat -->

所以现在,我要做的是从数据库中检索消息,然后将 HTML 代码以代码隐藏方式提交到中继器中, 如果是“你”,那么将使用此代码

    <div class="message-data">
      <span class="message-data-name"><i class="fa fa-circle you"></i> You</span>
    </div>
    <div class="message you-message">
    A new client?!?! I would love to help them, but where are we going to find the time?

    </div>

如果是其他人,则使用此代码

  <li class="clearfix">
    <div class="message-data align-right">
      <span class="message-data-name">Ada, your OperationsAlly</span> <i class="fa fa-circle me"></i>
    </div>
    <div class="message me-message float-right"> We should take a look at your onboarding and service delivery workflows, for most businesess there are many ways to save time and not compromise quality.  </div>
  </li>

我的问题是如何从 Code-Behind PageLoad 中做到这一点?如何将上面的代码从代码隐藏添加到中继器中?

谢谢!

【问题讨论】:

  • "You" 文本包装在&lt;asp:Label&gt; 中。将&lt;asp:Literal&gt; 放在这些交替代码所在的中继器中。在RepeaterItemDataBound 事件中,确保项目类型为ItemAlternatingItem。使用e.Item.FindControl("YourLiteralID")e.Item.FindControl("YourLabelID") 获取控件。根据 YourLabelId.Text 的值,用您的 HTML 填充文字。
  • 为什么不在前端使用 foreach 而不是 Repeater? &lt;% foreach (var msg in msgs) { %&gt; &lt;div&gt;...&lt;%= msg.txt %&gt;...&lt;/div&gt; &lt;% } %&gt;
  • @Santi 感谢您的帮助!但是我还是不太明白,可以给我代码吗?对不起!
  • @chakeda 嗨,我该怎么做?抱歉,我是新手,第一次听说 foreach

标签: c# html css asp.net


【解决方案1】:

首先,您必须在后面的代码中检索数据库中的记录。 (详见msdn article

messages.DataSource = myMessages;
messages.DataBind();

现在我们可以使用Repeater,其中html由作者分隔,来迭代数据。请注意,我假设您的消息数据库的结构。

<asp:Repeater id="messages" runat="server">
    <ItemTemplate>
        <div runat="server" visible='<% (Container.DataItem("author") == "us") %>'>
              <li>
                <div class="message-data">
                  <span class="message-data-name"><i class="fa fa-circle you"></i> You</span>
                </div>
                <div class="message you-message">
                    <%# Eval("testMessage") %>
                </div>
              </li>
        </div>
        <div runat="server" visible='<% (Container.DataItem("author") != "them") %>'>
              <li class="clearfix">
                <div class="message-data align-right">
                  <span class="message-data-name"><%# Eval("authorName") %></span> <i class="fa fa-circle me"></i>
                </div>
                <div class="message me-message float-right">
                    <%# Eval("testMessage") %>
                </div>
              </li>
        </div>          
    </ItemTemplate>
</asp:Repeater>

我还没有测试过这段代码,但它是一个高层次的想法。您可能还想查看一些Examples on using Repeater

【讨论】:

  • 非常感谢 只是需要澄清一下这部分,(Container.DataItem("author") == "us",所以这意味着我的数据库有一个名为 "author" 的属性,如果里面的值是“我们”,那么当那个转发器被启动时,那个特定的代码会执行吗?
  • 正确!正如我所提到的,此代码未经测试 - 因此您在实现此代码时可能必须解决一些错误。
  • 好的!非常感谢。学到很多
  • 如果有帮助请点赞/接受,如有其他问题,请随时发表评论。
  • 您好,我收到了此错误消息,无法从其字符串表示中创建类型为“System.Boolean”的对象 ' ' 对于“可见”属性。你知道是什么触发了这个吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2015-07-09
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多