How to change the view using the button on the repeater?
I have the following structure
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
<ContentTemplate>
<asp:MultiView ID="MyViews" runat="server">
<asp:View ID="List" runat="server">
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<asp:LinkButton ID="Button1" runat="server" Text="Button1" CommandName="View" />
</ItemTemplate>
</asp:Repeater>
</asp:View>
<asp:View ID="Single" runat="server">
<asp:LinkButton ID="Button2" runat="server" Text="Button2" />
</asp:View>
</asp:MultiView>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind, I get the repeater event ItemDataBound, get the control with var button1 = e.Item.FindControl("Button1") as LinkButton;, then I assign CommandArgumentwith the id of the current element.
This is done immediately after the method CreateChildControls.
Repeater1.ItemDataBound += (s, e) =>
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
var Button1 = e.Item.FindControl("Button1") as LinkButton;
Button1.CommandArgument = item.ID.ToString();
}
};
Repeater1.ItemCommand += (s, e) =>
{
if (e.CommandName == "View")
{
var item = provider.Get(Convert.ToInt64(e.CommandArgument));
BuildSingleView(item);
}
};
This method will simply change the view ...
public void BuildSingleView(var item)
{
MyViews.ActiveViewIndex = 1;
/* Edit
I've tried to call here
Initialize(), EnsureChildControls(), CreateChildControls()
but it was a useless try. I also tried to catch exceptions,
but none happened.
*/
}
The problem is that the view does not change. When I click LinkButton, it executes CreateChildControlsand after the ItemCommandevent is fired, the event triggers BuildSingleView, the value is ActiveViewIndexchanged, but nothing happens on the page.
I do not understand why this does not change. Is this a problem with the order of events? What can I do to change the view when I click LinkButton?
- ( Initialize - , CreateChildControls) http://pastebin.com/2qwrKNxf
:
Repeater1.ItemDataBound += (s, e) =>
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
var Button1 = e.Item.FindControl("Button1") as LinkButton;
Button1.CommandArgument = item.ID.ToString();
}
};
Repeater1.DataBind();
, ItemDataBound DataBind(). ItemDataBound , .
,
Repeater1.DataSource = List;
Repeater1.DataBind();
Repeater1.ItemDataBound += (s, e) =>
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
var item = e.Item.DataItem as Person;
var Button1 = e.Item.FindControl("Button1") as LinkButton;
}
};
, :
Repeater1.ItemDataBound += (s, e) =>
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
var item = e.Item.DataItem as Person;
var Button1 = e.Item.FindControl("Button1") as LinkButton;
}
};
Repeater1.DataSource = List;
Repeater1.DataBind();
.