首页 > 代码库 > 前台直接访问数据库,DataList的数据绑定

前台直接访问数据库,DataList的数据绑定

 

前台直接访问数据库.把全部数据取出来就可以.我个人比较喜欢这种方法.很简单.

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

            listbind2();

            //listbind3();
        }
    }
    /// <summary>
    /// DataList1的列表
    /// </summary>
    protected void listbind()
    {
        string MenuSQL = "select MenuID,MenuName,MenuUnit,MenuPrice,MenuDesc,OriPrice from wx_bu_Menu";
        //重构之后的代码调用
        PublicDataListBase.ShowDataList(DataList1, MenuSQL);
    }

    /// <summary>
    /// DataList2的列表
    /// </summary>
    protected void listbind2()
    {
        string ClassSql = "select ClassName from wx_bu_Class";
        //重构之后的代码调用
        PublicDataListBase.ShowDataList(DataList2, ClassSql);
    }
View Code
<asp:DataList ID="DataList2" runat="server" RepeatColumns="1" HorizontalAlign="Justify" RepeatDirection="Horizontal" >
                    <ItemTemplate><a href=http://www.mamicode.com/"index1.html"><li><%# Eval("ClassName")%></li></a></ItemTemplate>
                    </asp:DataList>
                </ul>
                <div class="blank25">
                </div>
            </div>
            <div style="height: 712px; width: 1355px;" id="page_right_contents">
                <div id="products" class="container">
                <asp:DataList ID="DataList1" runat="server" Height="354px" RepeatColumns="1" HorizontalAlign="Justify" RepeatDirection="Horizontal" Width="1355px">
                     <ItemTemplate>
                      <div class="item">
                            <div class="img">
                                <img src=http://www.mamicode.com/"UpLoadCaiPinImages/<%# Eval("MenuID")%>.jpg" onclick="ShowDiv(‘MyDiv‘,‘fade‘)" alt="千岛百叶" />
                            </div>
                            <div class="inkcom">
                                <div class="name" onclick="ShowDiv(‘MyDiv‘,‘fade‘)"><%# Eval("MenuName")%></div>
                                <div class="price">¥<%# Eval("MenuPrice")%>/<%# Eval("MenuUnit")%></div>
                                <s>¥<%# Eval("OriPrice")%></s>
                             </div>
                            <div class="inkcom_bt">
                                  <div class="clearfix goodssbox" id="Div1" style="float: right;">
                                      <span class="lgadd fr" id="Span1">
                                        <input id="btnSub" type="button" value=http://www.mamicode.com/"-" class=lgplus />
                                        <input id="txtCount" type="text" value=http://www.mamicode.com/"0" name=t1 size=2 class="addtext" maxlength=3 datatype=Number readonly="readonly" msg=必须为数字 />
                                        <input id="btnAdd" type="button" value=http://www.mamicode.com/"+" class=lgminus />
                                        <a id="btnAddstart">点选</a>
                                      </span>
                                  </div>
                            </div>
                            <div class="clear">
                            </div>
                      </div>
                  </ItemTemplate>
                 </asp:DataList>  
View Code

 这个是公共类库里面的方法.

View Code