简体   繁体   English

在ASP.NET中创建动态转发器

[英]Creating dynamic repeater in asp.net

Hi I am building an online sports goods shop, 嗨,我正在建立一家网上体育用品商店,
I have this plan of loading in all Sports Type in the Home page, like Football,Cricket,Basketball etc 我有此计划加载到主页的所有体育类型中,例如足球,板球,篮球等
And the administrator can create a game at his own will, 管理员可以随意创建游戏,

Here's the confusion 这是混乱
How do I display the SubCategories of each Game if clicked (inside the repeater). 如果单击(在转发器内部),如何显示每个游戏的子类别。
I thought of adding an ImageButton to it!! 我想到要添加一个ImageButton !! But then how do I link that Image Button to the games, ie when the user clicks the respective Image button -> The subcategories of that game should be displayed 但是然后我该如何将“图像按钮”链接到游戏,即当用户单击相应的“图像”按钮时->应该显示该游戏的子类别

For example: 例如:
1. If I have games such as Cricket,Football etc. 1.如果我有板球,足球等游戏
2. The Repeater should show all the games in the repeater 2.转发器应显示转发器中的所有游戏
3. When The User clicks on for instance Cricket 3.当用户单击例如板球时
4. I wish to load all subcategories of cricket goods such as the BAT,BALL,STUMPS etc. 4.我希望加载板球商品的所有子类别,例如BAT,BALL,STUMPS等。

I attempted this by loading the games in Repeater as shown in below code snippet: 我尝试通过将游戏加载到Repeater来进行尝试,如以下代码片段所示:

       <asp:Repeater ID="RepDetails" runat="server" 
        ondatabinding="RepDetails_DataBinding">
        <HeaderTemplate>
            <table style="border: 1px solid #df5015; width: 500px" cellpadding="0">
                <tr style="background-color: #df5015; color: White">
                    <td colspan="2">
                        <b>Type of Sports</b>
                    </td>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr style="background-color: #EBEFF0">
                <td>
                    <table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; width: 500px">
                        <tr>
                            <td>
                                <asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>' />
                            </td>
                            <td>
                                <asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Category") %>' Font-Bold="true" />
                            </td>
                            <td>
                                <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton2_Click" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>

I've even added the ImageButton but confused about making it load the respective subcategories of that game! 我什至添加了ImageButton但对于使其加载该游戏的各个子类别感到困惑!

Suggestions are welcome if there can be another work around which can be more effective.. 如果有其他工作可以更有效地提出建议,我们欢迎您提出建议。

You could try a nested repeater 您可以尝试嵌套的中继器

In aspx 在aspx中

<asp:Repeater ID="RepDetails" runat="server" OnDataBinding="RepDetails_DataBinding">
    <HeaderTemplate>
        <table style="border: 1px solid #df5015; width: 500px" cellpadding="0">
            <tr style="background-color: #df5015; color: White">
                <td colspan="2">
                    <b>Type of Sports</b>
                </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr style="background-color: #EBEFF0">
            <td>
                <table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; width: 500px">
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>' />
                        </td>
                        <td>
                            <asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Category") %>' Font-Bold="true" />
                        </td>
                        <td>
                            <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton2_Click" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <asp:Repeater ID="SportsProps" runat="server">
            <ItemTemplate>
                <tr style="background-color: #EBEFF0">
                    <td>
                        <table style="background-color: #EBEFF0; border-top: 1px dotted #df5015; width: 500px">
                            <tr>
                                <td>
                                    <asp:Label ID="lblSubject" runat="server" Text='<%#Eval("Name") %>' Font-Bold="true" />
                                </td>
                                <td>
                                    <asp:ImageButton ID="ImageButton3" runat="server" OnClick="ImageButton3_Click" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

In code behind 在后面的代码中

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RepDetails.DataSource = GetData();
            RepDetails.DataBind();
        }
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Repeater repeater = ((ImageButton)sender).NamingContainer.FindControl("SportsProps") as Repeater;
        Label catLabel = ((ImageButton)sender).NamingContainer.FindControl("lblSubject") as Label;
        repeater.DataSource = GetDataDetail(catLabel.Text);
        repeater.DataBind();
    }

    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        //do something to hide the 
    }

    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(string));
        dt.Columns.Add("category", typeof(string));
        dt.Rows.Add("1 ", "Basketball");
        dt.Rows.Add("2 ", "Football");
        dt.Rows.Add("3 ", "Soccer");
        return dt;
    }

    private DataTable GetDataDetail(string category)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name", typeof(string));
        dt.Rows.Add("Bat");
        dt.Rows.Add("Ball");
        dt.Rows.Add("Stump");
        return dt;
    }

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

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