天天看點

Repeater多層嵌綁定數值套遇到的問題

2012-03-22 13:56

repeater三層嵌套

頁面部分代碼    

   <asp:Repeater ID="Repeater1" runat="server" 

                   onitemdatabound="Repeater1_ItemDataBound">

               <ItemTemplate>

                       <div class="privince"><%#Eval("provinces") %><asp:HiddenField ID="provice" runat="server"   Value='<%#Eval("provinces") %>'/></div>

                       <asp:Repeater ID="Repeater2" runat="server" onitemdatabound=" Repeater2_ItemDataBound“>

                       <ItemTemplate>

                           <div class="city"><%#Eval("city") %>市<asp:HiddenField ID="city" runat="server" Value='<%#Eval("city") %>'/> </div>

                           <asp:Repeater ID="Repeater3" runat="server" >

                           <HeaderTemplate><ul></HeaderTemplate>

                             <ItemTemplate>

                               <li id='<%#Eval("id") %>'><a href="javascript:void(0)" target="_blank" rel="external nofollow" ><%#Eval("sname") %></a><br /><span>位址:<%#Eval("addr") %></span></li>

                             </ItemTemplate>

                             <FooterTemplate></ul></FooterTemplate>

                           </asp:Repeater>

                       </ItemTemplate>

                       </asp:Repeater>

               </ItemTemplate>

               </asp:Repeater>

主要的cs代碼

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

        HiddenField hdd = (HiddenField)e.Item.FindControl("provice");

        Repeater repeater2 = (Repeater)e.Item.FindControl("Repeater2");

        repeater2.DataSource = ism.FindShopByCity(hdd.Value.ToString().Trim());

        repeater2.DataBind();

       }

    }

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

        HiddenField hdd = (HiddenField)e.Item.FindControl("city");

        Repeater repeater3 = (Repeater)e.Item.FindControl("Repeater3");

        repeater3.DataSource = ism.FindCityByprovince(hdd.Value.ToString());

        repeater3.DataBind();

       }

    }

    這樣寫發看上去沒有問題,但是結果卻不如人所料。

應該:

在Repeater1_ItemDataBound中加入一句

repeater2.ItemDataBound += new RepeaterItemEventHandler(Repeater2_ItemDataBound); 即可

變為

   protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            HiddenField hdd = (HiddenField)e.Item.FindControl("provice");

            Repeater repeater2 = (Repeater)e.Item.FindControl("Repeater2");

            repeater2.ItemDataBound += new RepeaterItemEventHandler(Repeater2_ItemDataBound); //最最重要的一句

            repeater2.DataSource = ism.FindCityByprovince(hdd.Value.ToString());

            repeater2.DataBind();

        }

    }

而repeater上面也不要onitemdatabound=" Repeater2_ItemDataBound“

寫成<asp:Repeater ID="Repeater1" runat="server" >

這樣就沒有問題了

繼續閱讀