【问题标题】:Create controls as the datatable count on ASPX Design page在 ASPX 设计页面上创建控件作为数据表计数
【发布时间】:2026-01-26 18:50:01
【问题描述】:

我想在每个类别的每一行中显示 4 条最新记录。

我得到了结果,但我被困在我显示数据的方式上。

ASPX 页面上的 HTML

            <div class="row">
                <!--Start OF Live Feeds-->
                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsOne" runat="server" onserverclick="LiveFeedsOneEvent">
                                <img src="images/gallery/1.jpg" id="imgLiveFeedsOne" runat="server" />
                            <h4>
                     <asp:Label ID="lblLiveFeedsOne" runat="server" Text="Don’t quit your job if you work in"></asp:Label></h4>
                            </a>
                        </div>
                    </div> 
                </div>

                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsTwo" runat="server" onserverclick="LiveFeedsTwoEvent">
                                <img src="images/gallery/1.jpg" id="imgLiveFeedsTwo" runat="server" />
                                <h4>
                                    <asp:Label ID="lblLiveFeedsTwo" runat="server" Text="Live Feeds Two: Don’t quit your job if you work in 1 of these 5 industries"></asp:Label></h4>
                            </a>
                        </div>

                    </div>
                </div>

                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsThree" runat="server" onserverclick="LiveFeedsThreeEvent">
                                <img src="images/gallery/1.jpg" alt="" id="imgLiveFeedsThree" runat="server" />
                                <h4>
                                    <asp:Label ID="lblLiveFeedsThree" runat="server" Text="Live Feeds Three: Don’t quit your job if you work in 1 of these 5 industries"></asp:Label></h4>
                            </a>
                        </div>
                    </div>
                </div>

                <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsFour" runat="server" onserverclick="LiveFeedsFourEvent">
                                <img src="images/gallery/1.jpg" alt="" id="imgLiveFeedsFour" runat="server" />
                                <h4>
                                    <asp:Label ID="lblLiveFeedsFour" runat="server" Text="Live Feeds Four: Don’t quit your job if you work in 1 of these 5 industries"></asp:Label></h4>
                            </a>
                        </div>
                    </div>
                </div>

                </div>

绑定数据

    private void BindData()
     {

        using (SqlConnection con = new SqlConnection(cn))
        {
            using (SqlCommand cmd = new SqlCommand("usp_NewsByCategories"))
            {

                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;

                    sda.Fill(dt);
                    ViewState["data"] = dt;                

                    imgLiveFeedsOne.Src = dt.Rows[55]         
                    imgPromotionFour.Src = dt.Rows[4]["ImagePath"].ToString();
                    hrefPromotionFour.HRef = dt.Rows[4]["Identity"].ToString();
                    lblPromotionFour.Text = dt.Rows[4]["Headline"].ToString();

                    imgPromotionFive.Src = dt.Rows[3]["ImagePath"].ToString();
                    hrefPromotionFive.HRef = dt.Rows[3]["Identity"].ToString();
                    lblPromotionFive.Text = dt.Rows[3]["Headline"].ToString();

                    imgPromotionSix.Src = dt.Rows[2]["ImagePath"].ToString();
                    hrefPromotionSix.HRef = dt.Rows[2]["Identity"].ToString();
                    lblPromotionSix.Text = dt.Rows[2]["Headline"].ToString();

                    imgPromotionSeven.Src = dt.Rows[1]["ImagePath"].ToString();
                    hrefPromotionSeven.HRef = dt.Rows[1]["Identity"].ToString();
                    lblPromotionSeven.Text = dt.Rows[1]["Headline"].ToString();

                    imgPromotionEight.Src = dt.Rows[0]["ImagePath"].ToString();
                    hrefPromotionEight.HRef = dt.Rows[0]["Identity"].ToString();
                    lblPromotionEight.Text = dt.Rows[0]["Headline"].ToString();


                   }
            }
        }
    }

这是完全错误的做法,因为在一个 aspx 页面上会有 80 条记录

问题是我如何编写代码以根据 Datatable 的计数来获取设计,并在 foreach 循环中绑定它,并根据计数动态创建图像、标签,在 MVC 中很容易

喜欢

@{  
<div class="row">
@foreach (var item in Model)
{
 <div class="col-xs-6 col-sm-3 mix livefeeds">
                    <div class="work-item">
                        <div class="image-holder">
                            <a href="images/gallery/1.jpg" id="hrefLiveFeedsFive">
                                <img src="@item.imageID" alt=""/>
                                <h4>
                                    <span>@Html.Raw(@item.Title)</span>
                            </a>
                        </div>

                    </div>
                </div>

}
    </div>
  }

我如何像在 MVC Razor 上一样在 ASPX 设计视图上编写相同的内容。 注意:由于服务器不支持 MVC,所以在 Webform 中进行。

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-4 razor


    【解决方案1】:

    绑定数据

         protected DataTable dt = new DataTable()
         private void BindData()
         {
    
            using (SqlConnection con = new SqlConnection(cn))
            {
                using (SqlCommand cmd = new SqlCommand("usp_NewsByCategories"))
                {
    
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
    
                        sda.Fill(dt);
                       }
                }
            }
        }
    

    ASPX 文件将是

    <div class="row">
    
    <% foreach (DataRow item in dt.Rows)
        {
    %>
         <div class="col-xs-6 col-sm-3 mix livefeeds">
                            <div class="work-item">
                                <div class="image-holder">
                                    <a href="<%=item["Identity"].ToString()%>">
                                        <img src="<%=item["ImagePath"].ToString()%>" alt=""/>
                                        <h4>
                                            <span><%=item["Headline"].ToString()%></span>
                                    </a>
                                </div>
    
                            </div>
                 </div>
    <%
    }
    %>
    </div>
    

    【讨论】:

    • 成功了吗,出现错误 CS0021:无法将 [] 索引应用于“对象”类型的表达式
    • 异常发生在哪一行?
    • 行号 45 和 ASPX 页面上的所有红线 .已添加屏幕短
    • 你是如何给我链接的,即带有#id 的*.com/questions/2325777/…,因为它恰好落在页面的同一区域。否则,网址是 *.com/questions/2325777/…
    • 答案下方有分享链接,如果我点击它,就会出现确切位置的网址。