C# ASP.NET 根据来自 db 的数据将 html 代码插入转发器

标签 c# html css asp.net

再次需要帮助

抱歉有点困惑,但是,我正在尝试创建一个用户可以提交的聊天框,然后从数据库中再次检索它,我正在关注这个脚本 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 代码提交到 Repeater, 如果是“您”,则将使用此代码

    <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 做到这一点?如何从代码隐藏中将上面的代码添加到 Repeater 中?

谢谢!

最佳答案

首先,您必须在后面的代码中检索数据库中的记录。 (详见这个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

关于C# ASP.NET 根据来自 db 的数据将 html 代码插入转发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44953458/

相关文章:

html - 与页脚重叠的位置固定 div(或侧边栏)

c# - LINQ 等效的 MYSQL 查询

c# - 我不想在存储过程中转换的复杂 SQL

c# - 基准测试 .Net 正则表达式选项 - 或者 RightToLeft 做什么?

html - 在图像上悬停可见图像信息?

javascript - Bootstrap : rearrange order of different sized panels on mobile

c# - ASP.Net 中的数据缓存

javascript - 使用 JavaScript 将 Css 动画应用到带有 onclick 的类

html - 阻止Iframe接管页面

css - Twitter Bootstrap 还是 Zurb Foundation?你喜欢哪个?