简体   繁体   English

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

[英]C# ASP.NET insert html code into repeater based on the data from db

So needed help again 再次需要帮助

Sorry a bit confusing but, I am trying to create a chat box which user can submit and then retrieve it again from the DB, I am following this script https://bootsnipp.com/snippets/EkQe7 对不起,有点混乱,但是,我正在尝试创建一个聊天框,用户可以提交该聊天框,然后再次从数据库中检索它,我正在遵循此脚本https://bootsnipp.com/snippets/EkQe7

As you can see, from the HTML code, there is two types, one is when you chat and another is when other people chat, 如您所见,在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 -->

So now, what I am trying to do is from the retrieve the message from DB then code-behind submit the HTML code into the Repeater, If it is "You", then will use this code 所以现在,我想做的是从数据库中检索消息,然后代码隐藏,然后将HTML代码提交到Repeater中,如果是“ You”,则将使用此代码

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

If it is other people, then will use this code 如果是其他人,那么将使用此代码

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

My question is how to do that from Code-Behind PageLoad? 我的问题是如何从代码隐藏的PageLoad中做到这一点? How to add the code above into the Repeater from code-behind? 如何将以上代码从后台代码添加到Repeater中?

Thanks! 谢谢!

First, you'll have to retrieve the records in the database in the code behind. 首先,您必须在后面的代码中检索数据库中的记录。 (See this msdn article for details) (有关详细信息,请参阅此msdn文章

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

Now we can use Repeater, in which the html is separated by author, to iterate the data. 现在,我们可以使用Repeater(其中html由作者分隔)来迭代数据。 Note that I am assuming how your database of messages is structured. 请注意,我假设您的邮件数据库的结构。

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

I haven't tested this code, but its a high level idea of what to do. 我没有测试此代码,但是它是做什么工作的高级概念。 You may also want to see some Examples on using Repeater . 您可能还想看看有关使用Repeater的一些示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM