【问题标题】:ASP.net C# Text from response.write() not displayed as i want来自 response.write() 的 ASP.net C# 文本未按我想要的方式显示
【发布时间】:2013-08-31 09:18:31
【问题描述】:

ASP.net C# 我正在使用一个 Jquery 新闻代码,在其中我从我的数据库中获取动态值。我已将 db 中的值提取到 newsticker 列表的 `<OL> 中。现在,问题是,我无法将它嵌入到我的网页中的正确位置。我有一个 div,div id="newsticker",其中应显示所有数据,但我从 news_ticker() 类中的 db 检索的值未显示在该 div 中。它们显示在页面顶部。

这是我的代码,

.cs 文件:

protected void news_ticker()
{

    SqlConnection connection = ConnectionManager.getConnection();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = connection;

    cmd.CommandText = ("SELECT [job_title] FROM [job_post]");
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    da = new SqlDataAdapter(cmd);
    dt.Clear();
    da.Fill(dt);

    Response.Write("<ol id='sample' class='ticker'>");

    for (int i = 0; i < dt.Rows.Count - 1; i++)
    {

        Response.Write("<li><a href='#' class='read'>Read more</a>" + (dt.Rows[i][0]) + "</li>");

    }
    Response.Write("</ol>");


    connection.Close();

}

.aspx 文件:

  <div id="newsticker">

<div class="pull-left">Latest News   <img src="../images/rss1.png" alt="rss" width="15" height="15" /> |</div>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [job_title] FROM [job_post]"></asp:SqlDataSource>
    <br />    
  </div>

.css 文件:

    .ticker {
margin: 0;
padding: 5px;
list-style-type: none;
/*border: 1px solid #ccc;*/
color:#F9A813;
       }
      .ticker li {
    line-height:1.5;
}

.ticker-active li {
    display:none;
    overflow:hidden;
    white-space:nowrap;
}
    .read{
float: right;
   }

   .pull-left{
float:left;
/*border-right: 1px solid #000000;*/
padding:8px;
color:#FFF;
    }



     #newsticker {
position: absolute;
left: 11px;
top: 400px;
width: 700px;
height: 39px;
z-index: 16;
visibility: visible;
    }

附:请注意,我已经在&lt;ol&gt;, 中使用简单的虚拟值尝试了此代码,并且它们在 newsticker div 中完美显示。但是当我使用数据库中的动态值时,它们不会显示在 newsticker div 中。我无法弄清楚我错过了什么?我尝试了很多东西,但现在我不知所措。请帮忙?

P.p.s.我没有在此处包含 jscript 文件,因为它似乎工作正常。

【问题讨论】:

    标签: c# jquery html asp.net css


    【解决方案1】:

    你不能在你的情况下使用 response.write,你需要像这样在 ASPX 中有一个转发器控件

    <asp:Repeater ID="JobsRepeater" runat="server">
        <HeaderTemplate>
            <ol id='sample' class='ticker'>
        </HeaderTemplate>
        <ItemTemplate>
            <li><a href='#' class='read'>Read more</a> <%#Eval("job_title")%> </li>
        </ItemTemplate>
        <FooterTemplate>
            </ol>
        </FooterTemplate>
    </asp:Repeater>
    

    然后在后面的代码中将数据表绑定为转发器的数据源

    JobsRepeater.DataSource= dt;
    JobsRepeater.DataBind();
    

    【讨论】:

    • 非常感谢,这不仅解决了我的问题,还清除了我的概念。
    猜你喜欢
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多