2

我有一个中继器:

<ul>
    <asp:Repeater ID="Repeater4" runat="server" DataSourceID="SqlDataSource5" onitemcommand="Repeater4_ItemCommand"> 
    <ItemTemplate> 
    <li class="sports_menu1">  
    <asp:LinkButton ID="LinkButton1" runat="server" class="selected  onclick="sideMenuSports1_Click"> 
    <%#Eval("iconURL")%> 
    </asp:LinkButton></li> 
    </ItemTemplate>
    </asp:Repeater>
</ul>

我想处理同一功能中的所有按钮:

protected void sideMenuSports1_Click(object sender, EventArgs e)
{ 
    changeDataSources();   /*-----> this function supose to derict to URL(in a div on the same page) acording the id  of the item of the repeater   */ 
    removeAllClasses();  /*this supose to remove all class named selected from the element in the <asp:button> element */
    ((LinkButton)sender).Attributes.Add("class", "selected");
}

总而言之,我需要的是:

  1. 我需要删除所有转发器 <asp:button>元素中的“选定”类,然后仅将其添加到用户单击的元素中
  2. 单击的每个按钮都重定向到其他 URL,我如何将转发器元素的 id 传递给按钮?
4

1 回答 1

0

遇到了同样的问题,这样解决:

.aspx 部分:

<asp:Repeater ID="itemsRepeater" runat="server" OnItemCommand="itemsRepeater_ItemCommand">
    <ItemTemplate>
        <li>
            <asp:LinkButton id="location_button" CommandArgument='<%# Eval("Key") %>' Text='<%# Eval("Value") %>' runat="server"></asp:LinkButton>
        </li>
    </ItemTemplate>
</asp:Repeater>

代码隐藏部分:

protected void itemsRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    // for the sample purpose, setting the value here - replace it with your own
    int locationId = 2;

    // looping through all the repeater items (buttons)
    foreach (RepeaterItem i in ((Repeater)source).Items)
    {
        // finding the repeater items having ID set to "location_button"
        LinkButton btn = (LinkButton)i.FindControl("location_button");

        // custom class added when location matches button's CommandArgument
        if (btn.CommandArgument == locationId.ToString())
            btn.CssClass = "button_main active";
        else
            btn.CssClass = "button_main";
    }
}
于 2018-06-27T13:11:57.107 回答