如何用updatepanel解决linkbutton

时间:2013-12-25 08:53:32

标签: c# asp.net .net updatepanel

我有一个LinkBut​​ton和一个UpdatePanel。当我在UpdatePanel中单击LinkBut​​ton时,它第一次完美地工作并显示数据。但是,当我再次单击它时,数据不会显示。当我删除UpdatePanel时,它可以很好地工作,但由于页面刷新,我不想这样做。这是我使用的代码:

<ul id="filter-tours" class="filter-out">
    <li><asp:LinkButton ID="lnkBenglore" runat="server" CommandArgument="benglore" OnClick="lnkCity_Click" >Benglore</asp:LinkButton></li>
    <li><asp:LinkButton ID="lnkVadodara" runat="server" CommandArgument="vadodara" OnClick="lnkCity_Click">Vadodara</asp:LinkButton></li>
    <li class="header" >
        <asp:LinkButton ID="lnkAhmedabad" runat="server" CommandArgument="ahmedabad" OnClick="lnkCity_Click">
            <div class="header-outer">
                <div class="section-header">
                    <div class="circle-outer">
                        <div class="inner-1">
                            <div class="inner-2"></div>
                        </div>
                    </div>
                    <span>Ahmedabad</span>
                    <div class="head-bottom-bar"></div>
                </div>
            </div>
        </asp:LinkButton>
    </li>
    <li><asp:LinkButton ID="lnkSurat" runat="server" CommandArgument="surat" OnClick="lnkCity_Click">Surat</asp:LinkButton></li>
    <li><asp:LinkButton ID="lnkPune" runat="server" CommandArgument="pune" OnClick="lnkCity_Click">Pune</asp:LinkButton></li>
    <li><asp:LinkButton ID="lnkMumbai" runat="server" CommandArgument="mumbai" OnClick="lnkCity_Click">Mumbai</asp:LinkButton></li>
</ul>
<div class="clearfix"></div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <ContentTemplate>
        <asp:Repeater id="rptHotel" runat="server" >
            <HeaderTemplate>
                <div class="tours-wrapper">
            </HeaderTemplate>
            <ItemTemplate>
                <div class="tour <%# Eval("CityName") %>" style="margin-right:5px !important;">
                    <div class="tour-visual"> 
                        <a href="SearchHotel.aspx?HotelID=<%# Eval("HotelID") %>" >
                            <img src="Admin/Hotel/Main/<%#Eval("Logo")%>" alt="" />
                            <div class="hover"></div>
                        </a>
                    </div>
                    <div class="head">
                        <a href="SearchHotel.aspx?HotelID=<%# Eval("HotelID") %>" >
                            <%#Eval("HotelName")%>
                        </a>
                    </div>
                    <div class="tag-line">
                        <%# Limit(Eval("Address"), 55)%>, <%#Eval("CityName")%>
                    </div>
                    <div class="rate">
                        <div class="rating" >
                            <asp:Rating ID="Rating1" runat="server" ReadOnly="true" StarCssClass="Star" WaitingStarCssClass="WaitingStar" EmptyStarCssClass="Star" FilledStarCssClass="FilledStar" CurrentRating='<%# Eval("Rating") %>' >
                            </asp:Rating>
                        </div>
                    </div>
                    <div class="records">
                        <%--<span class="icon-comment"><a href="#">196</a></span>--%>
                        <%--<span class="icon-circle-arrow-right"><a href="#">77245</a></span>--%>
                        <span class="icon-map-marker"><a href="#">show maps</a></span>
                    </div>
                    <div class="tour-bottom-line"></div>
                    <a href="SearchHotel.aspx?HotelId=<%# Eval("HotelID") %>" class="booknow">Book Now</a>
                </div>
            </ItemTemplate>
            <FooterTemplate>
                </div>
                <div class="clearfix"></div>
            </FooterTemplate>
        </asp:Repeater>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="lnkBenglore" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="lnkVadodara" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="lnkAhmedabad" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="lnkSurat" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="lnkPune" EventName="Click" />
        <asp:AsyncPostBackTrigger ControlID="lnkMumbai" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

aspx.cs文件如下:

protected void lnkCity_Click(object sender, EventArgs e)
{
    string myConnectionString = "my connection string";

    LinkButton lnkCity = sender as LinkButton;

    if (lnkCity.CommandArgument.Equals("benglore"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

        int cityID = 2;
        rptHotel.DataSource = dalMST_Hotel.SelectTop4(cityID, myConnectionString);
        rptHotel.DataBind();
    }
    else if (lnkCity.CommandArgument.Equals("vadodara"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

        int cityID = 3;
        rptHotel.DataSource = dalMST_Hotel.SelectTop4(cityID, myConnectionString);
        rptHotel.DataBind();
    }
    else if (lnkCity.CommandArgument.Equals("ahmedabad"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

        int cityID = 1;
        rptHotel.DataSource = dalMST_Hotel.SelectTop4(cityID, myConnectionString);
        rptHotel.DataBind();
    }
    else if (lnkCity.CommandArgument.Equals("surat"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

        int cityID = 4;
        rptHotel.DataSource = dalMST_Hotel.SelectTop4(cityID, myConnectionString);
        rptHotel.DataBind();
    }
    else if (lnkCity.CommandArgument.Equals("pune"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

        int cityID = 5;
        rptHotel.DataSource = dalMST_Hotel.SelectTop4(cityID, myConnectionString);
        rptHotel.DataBind();
    }
    else if (lnkCity.CommandArgument.Equals("mumbai"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

        int cityID = 6;
        rptHotel.DataSource = dalMST_Hotel.SelectTop4(cityID, myConnectionString);
        rptHotel.DataBind();
    }
}

0 个答案:

没有答案