ASP.NET转发器动态值

发布于 2025-01-19 21:59:55 字数 2745 浏览 4 评论 0原文

我尝试在 ASP.NET 中的中继器中使用中继器,但我想更改每次重复的数据源。

我的 aspx 标记是:

<div class="container px-4 py-5" id="custom-cards">
    <asp:Repeater ID="RepeaterKategori" runat="server" OnItemDataBound="ItemBound">
        <ItemTemplate>
            <h2 class="pb-2 border-bottom"><%#Eval("kategoriAd") %></h2>
            <div class="row row-cols-1 row-cols-md-3 g-4">
                <asp:Repeater ID="RepeaterAltKategori" runat="server">
                    <ItemTemplate>
                        <div class="col">
                            <div class="card h-100">
                                <img src="<%#Eval("altkategoriResim") %>" class="card-img-top" alt="...">
                                <div class="card-body">
                                    <h5 class="card-title"><%#Eval("altkategoriBaslik") %></h5>
                                    <p class="card-text"><%#Eval("altkategoriAciklama") %></p>
                                </div>
                                <div class="card-footer">
                                    <small class="text-muted">Teşekkürler : <%#Eval("altkategoriDestekci") %></small>
                                </div>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
            <div class="d-grid gap-2 my-4">
                <a href="/yardim-kampanyalari.aspx" class="btn btn-outline-info" role="button">Tümünü Görüntüle</a>
            </div>
        </ItemTemplate>
    </asp:Repeater>
</div>

我的 aspx.cs 代码背后是:

rehber kod = new rehber();

protected void Page_Load(object sender, EventArgs e)
{
    RepeaterKategori.DataSource = kod.getDataTable("SELECT KategoriAd FROM kategoriler");
    RepeaterKategori.DataBind();
}

protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
    if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataTable katsay = kod.getDataTable("SELECT * FROM altkategoriler");
        int kategoris = katsay.Rows.Count;

        for (int i = 1; i == kategoris; i++)
        {
            Repeater RepeaterAltKategori = (Repeater)args.Item.FindControl("RepeaterAltKategori");
            RepeaterAltKategori.DataSource = kod.getDataTable("SELECT TOP 3 * FROM altkategoriler WHERE kategoriId="+i+"");
            RepeaterAltKategori.DataBind();
        }
    }
}

我想要另一个 id 数据,如每次重复 1,2,3,4。我该怎么做?谢谢..

I try to use a repeater in repeater in ASP.NET, but I want to change datasource from every repeat.

My aspx markup is:

<div class="container px-4 py-5" id="custom-cards">
    <asp:Repeater ID="RepeaterKategori" runat="server" OnItemDataBound="ItemBound">
        <ItemTemplate>
            <h2 class="pb-2 border-bottom"><%#Eval("kategoriAd") %></h2>
            <div class="row row-cols-1 row-cols-md-3 g-4">
                <asp:Repeater ID="RepeaterAltKategori" runat="server">
                    <ItemTemplate>
                        <div class="col">
                            <div class="card h-100">
                                <img src="<%#Eval("altkategoriResim") %>" class="card-img-top" alt="...">
                                <div class="card-body">
                                    <h5 class="card-title"><%#Eval("altkategoriBaslik") %></h5>
                                    <p class="card-text"><%#Eval("altkategoriAciklama") %></p>
                                </div>
                                <div class="card-footer">
                                    <small class="text-muted">Teşekkürler : <%#Eval("altkategoriDestekci") %></small>
                                </div>
                            </div>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
            <div class="d-grid gap-2 my-4">
                <a href="/yardim-kampanyalari.aspx" class="btn btn-outline-info" role="button">Tümünü Görüntüle</a>
            </div>
        </ItemTemplate>
    </asp:Repeater>
</div>

My aspx.cs code behind is:

rehber kod = new rehber();

protected void Page_Load(object sender, EventArgs e)
{
    RepeaterKategori.DataSource = kod.getDataTable("SELECT KategoriAd FROM kategoriler");
    RepeaterKategori.DataBind();
}

protected void ItemBound(object sender, RepeaterItemEventArgs args)
{
    if (args.Item.ItemType == ListItemType.Item || args.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataTable katsay = kod.getDataTable("SELECT * FROM altkategoriler");
        int kategoris = katsay.Rows.Count;

        for (int i = 1; i == kategoris; i++)
        {
            Repeater RepeaterAltKategori = (Repeater)args.Item.FindControl("RepeaterAltKategori");
            RepeaterAltKategori.DataSource = kod.getDataTable("SELECT TOP 3 * FROM altkategoriler WHERE kategoriId="+i+"");
            RepeaterAltKategori.DataBind();
        }
    }
}

I want another id data like 1,2,3,4 for every repeat. How can I do this? Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2025-01-26 21:59:55

然后,您需要筑巢两个中继器。

您还拥有

top table - feed the main repeater - 1 record for each.

child table - feed the child repeater - child table - many records for each.

,请确保您始终使用If(!IsPostback)具有真实的第一页加载 - 如果您不执行大部分(如果不是全部)加载和数据指标,则在!Ispostback内部?

您甚至无法真正建立一个可以在后备后生存的工作页面。

好的,所以我们有主人(顶部)中继器。

对于儿童中继器,您可能有一个记录,也许有很多。没关系。但是重要的是,您现在拥有一个全新的独立数据集和全新的eval()以及属于该子表或子数据集的绑定。

因此,让我们进行主要中继器(我们的酒店),在每种情况下,我们都会放入另一个中继器以显示儿童数据(在本例中,人们在酒店预订的人)。

因此,我们有此标记 - 请注意嵌套的第二个中继器。

        <asp:Repeater ID="repHotels" runat="server" OnItemDataBound="repHotels_ItemDataBound">
            <ItemTemplate>
                <asp:Label ID="txtHotel" runat="server" Width="240px"
                    Text='<%# Eval("HotelName") %>' Font-Size="X-Large"                 > 
                </asp:Label>

                <asp:TextBox ID="txtDescription" runat="server"
                    Text='<%# Eval("Description") %>'
                    TextMode="MultiLine" Rows="5" Columns="40" Style="margin-left:20px" >
                </asp:TextBox>
                <br />
                <h4>People Booked</h4>
                <asp:Repeater ID="repPeopleBooked" runat="server">
                    <ItemTemplate>
                        <asp:Label ID="lFirst" runat="server" Text='<%# Eval("FirstName") %>'>
                        </asp:Label>
                        <asp:Label ID="lLast" runat="server" Text='<%# Eval("LastName") %>' 
                            style="margin-left:10px">
                        </asp:Label>
                        <br />
                    </ItemTemplate>
                </asp:Repeater>
                <%-- our repeat separator --%>
                <div style="height:20px">
                    <hr style="border:1px solid" />
                </div>
            </ItemTemplate>
        </asp:Repeater>

和我们的代码加载。我们仅加载主要的中继器数据源,这将触发项目数据绑定事件,然后在该事件中,我们处理了儿童中继器的每个单独实例。

这样的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }


    void LoadData()
    {            
        SqlCommand cmdSQL = new SqlCommand("SELECT * from MyHotels ORDER BY HotelName");

        repHotels.DataSource = MyRstP(cmdSQL);
        repHotels.DataBind();
    }


    protected void repHotels_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ( (e.Item.ItemType == ListItemType.Item) || 
            (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            // get child nested repeater
            Repeater rChild = e.Item.FindControl("repPeopleBooked") as Repeater;

            DataRowView rRow = ((DataRowView)e.Item.DataItem);
            string strSQL = "SELECT * FROM People WHERE Hotel_ID = @id";
            SqlCommand cmdSQL = new SqlCommand(strSQL);
            cmdSQL.Parameters.Add("@id", SqlDbType.Int).Value = rRow["ID"];

            rChild.DataSource = MyRstP(cmdSQL);
            rChild.DataBind();

        }
    }
    public DataTable MyRstP(SqlCommand cmdSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (cmdSQL)
            {
                cmdSQL.Connection = conn;
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
            }
        }
        return rstData;
    }

我们现在得到了:

”“在此处输入图像说明”

所以,我们看到了驱动主要中继器的3个主要记录,然后我们看到了嵌套中继器的子记录。

现在,就您而言,您可能只有一个孩子纪录 - 但这没关系。真正重要的是,您有能力处理和绑定,并具有该儿童中继器的单独的评估()和数据。

You need to nest two repeaters then.

You have

top table - feed the main repeater - 1 record for each.

child table - feed the child repeater - child table - many records for each.

Also, make sure you ALWAYS use if (!isPostBack) to have a real first page load - if you don't do most (if not all) of your loading and databinding inside of the !IsPostback?

you cannot even really quite much build a working page that can survive post-backs.

Ok, so we have the master (top) repeater.

for the child repeater, you might have one record, maybe many. It don't matter. But WHAT DOES matter is you now have a whole new seperate data set and whole new set of Eval() and binding that belongs to that child table or child data set.

So, lets take a main repeater (our hotels), and for each instance, we drop in another repeater to display the child data (in this case people booked in the hotel).

So, we have this mark-up - note the nested 2nd repeater.

        <asp:Repeater ID="repHotels" runat="server" OnItemDataBound="repHotels_ItemDataBound">
            <ItemTemplate>
                <asp:Label ID="txtHotel" runat="server" Width="240px"
                    Text='<%# Eval("HotelName") %>' Font-Size="X-Large"                 > 
                </asp:Label>

                <asp:TextBox ID="txtDescription" runat="server"
                    Text='<%# Eval("Description") %>'
                    TextMode="MultiLine" Rows="5" Columns="40" Style="margin-left:20px" >
                </asp:TextBox>
                <br />
                <h4>People Booked</h4>
                <asp:Repeater ID="repPeopleBooked" runat="server">
                    <ItemTemplate>
                        <asp:Label ID="lFirst" runat="server" Text='<%# Eval("FirstName") %>'>
                        </asp:Label>
                        <asp:Label ID="lLast" runat="server" Text='<%# Eval("LastName") %>' 
                            style="margin-left:10px">
                        </asp:Label>
                        <br />
                    </ItemTemplate>
                </asp:Repeater>
                <%-- our repeat separator --%>
                <div style="height:20px">
                    <hr style="border:1px solid" />
                </div>
            </ItemTemplate>
        </asp:Repeater>

And our code to load. We ONLY load the main repeater data source, and THAT will trigger the item data bound event, and in that event then we deal with EACH separate instance of the child repeater.

Code like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();
    }


    void LoadData()
    {            
        SqlCommand cmdSQL = new SqlCommand("SELECT * from MyHotels ORDER BY HotelName");

        repHotels.DataSource = MyRstP(cmdSQL);
        repHotels.DataBind();
    }


    protected void repHotels_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ( (e.Item.ItemType == ListItemType.Item) || 
            (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            // get child nested repeater
            Repeater rChild = e.Item.FindControl("repPeopleBooked") as Repeater;

            DataRowView rRow = ((DataRowView)e.Item.DataItem);
            string strSQL = "SELECT * FROM People WHERE Hotel_ID = @id";
            SqlCommand cmdSQL = new SqlCommand(strSQL);
            cmdSQL.Parameters.Add("@id", SqlDbType.Int).Value = rRow["ID"];

            rChild.DataSource = MyRstP(cmdSQL);
            rChild.DataBind();

        }
    }
    public DataTable MyRstP(SqlCommand cmdSQL)
    {
        DataTable rstData = new DataTable();
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            using (cmdSQL)
            {
                cmdSQL.Connection = conn;
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
            }
        }
        return rstData;
    }

and we now get this:

enter image description here

So, we see the 3 main records that drive the main repeater, and then we see the child records for the nested repeater.

Now, in your case, you might only ever have one child record - but it don't matter. What really matters is that you have the ability to deal with, and bind, and have separate Eval() and data for that child repeater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文