如何解决LinkBut​​ton Click事件并找到该ID

时间:2013-12-24 08:17:01

标签: c# asp.net .net updatepanel linkbutton

我有一个linkBut​​tons的问题,点击事件没有被触发我想对链接按钮使用相同的事件,当我点击任何linkBut​​ton绑定数据到Repeater,这是城市明智 以下是我的代码:

<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-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>
                            </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>

代码背后的代码是:

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();
        }

    }

1 个答案:

答案 0 :(得分:1)

使用Repeater的ItemCommand事件,您的代码将是这样的

protected void rpHotel_ItemCommand (object source, RepeaterCommandEventArgs e)
{
  if (e.CommandName.Equals("benglore"))
    {
        rptHotel.DataSource = null;
        rptHotel.DataBind();

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

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

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

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

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

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