欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

repeater 分列显示以及布局的实例代码

程序员文章站 2024-03-02 20:11:16
前台复制代码 代码如下:
         &...

前台

复制代码 代码如下:

<div>
        <table>
            <tr>
                <asp:repeater id="repeater1" runat="server" onitemdatabound="repeater1_itemdatabound">
                    <itemtemplate>
                        <td>
                            <table>
                                <tr>
                                    <td colspan="2">
                                        <img src='<%#"images/"+eval("foodpicture") %>' />
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <asp:label id="label1" runat="server" text='<%#eval("foodname") %>'></asp:label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:label id="label2" runat="server" text=' <%#eval("foodprice") %>'></asp:label>
                                    </td>
                                    <td>
                                        <input type="image" src="images/product_add.png" onclick="product_add() " />
                                        <%--    <asp:imagebutton id="imagebutton1" runat="server" imageurl="images/product_add.png" />--%>
                                        <asp:textbox id="textbox1" text="1" runat="server" width="15px" readonly="true"></asp:textbox>
                                        <input type="image" src="images/product_reduce.png" onclick="product_reduce()" />
                                        <%--<asp:imagebutton id="imagebutton2" runat="server" imageurl="images/product_reduce.png" />--%>
                                    </td>
                                </tr>
                                <tr>
                                    <th colspan="2">
                                        <asp:imagebutton id="imagebutton3" imageurl="images/btn_order.gif" runat="server" />
                                    </th>
                                </tr>
                            </table>
                        </td>
                    </itemtemplate>
                </asp:repeater>
            </tr>
        </table>
    </div>


后台

复制代码 代码如下:

  public   int i = 1;               

protected void page_load(object sender, eventargs e)

        {

            string sqlstr = @"data source=pc-lenove\sqlexpress;initial catalog=kfc;user id=sa;password=abing520";

            sqlconnection con = new sqlconnection(sqlstr);

            sqlcommand cmd = new sqlcommand();

            cmd.commandtext = "select * from foods";

            cmd.connection = con;

            sqldataadapter adapter = new sqldataadapter(cmd);

            datatable dt = new datatable();

            adapter.fill(dt);

            cmd.dispose();

            con.dispose();

            this.repeater1.datasource = dt;

            this.repeater1.databind();

        }

        protected void repeater1_itemdatabound(object sender, repeateritemeventargs e)

        {

            if (i % 4 == 0)//4是一行显示列数

            {

                e.item.controls.add(new literalcontrol("</tr><tr>"));

            }

            i++;

        }