Reputation: 1549
I'm trying to make a specific label in my repeater visible after binding. I don't want all the labels of every items in the repeater to be visible. Just the one where I click the button. When I click the button update I'm updating the information for my tourney item in my DB then I want to show a label to say the change was a success but only for the item I updated.
Here's the code behind. The [...] is where I do the update in the DB
protected void repeatTourney_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "btnUpdate_Click")
{
[...]
Label lblSuccess= (Label)e.Item.FindControl("lblUpdateSuccess");
bindRepeater(ddlEvents.Text);
lblSuccess.Visible = true;
}
}
Here's the aspx. The [...] is the textboxes and other stuff that contains info for my DB item.
<asp:Repeater ID="repeatTourney" runat="server" OnItemDataBound="repeatTourney_ItemDataBound"
OnItemCommand="repeatTourney_ItemCommand">
<ItemTemplate>
<div class="form">
[...]
<asp:Label ID="lblUpdateSuccess" runat="server" Text="Update success" Visible="false" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" CssClass="button" CommandName="btnUpdate_Click" />
[...]
</div>
</ItemTemplate>
</asp:Repeater>
In the end it should look like this
Item
Info
BtnUpdate
lblSuccess.Visible = false
Item
Info
BtnUpdate <== Clicked
lblSuccess.Visible = true
Thank you for any help provided.
Edit : Here's my bindRepeater code
private void bindRepeater(string name)
{
List<Tourney> list = TourneyDAL.GetByNameEvent(name);
[...]
repeatTournois.DataSource = list;
repeatTournois.DataBind();
[...]
}
Edit 2 : Thank you for the idea of an ID to tell which one need to be visible after the binding.
Worked just fine. :)
Here my new code
private void bindRepeater(string name, int index)
{
List<Tourney> list = TourneyDAL.GetByNameEvent(name);
[...]
repeatTourney.DataSource = list;
repeatTourney.DataBind();
[...]
if (index != 0)
{
Label lblReussie = (Label)repeatTourney.Items[index].FindControl("lblUpdateSuccess");
lblSuccess.Visible = true;
}
protected void repeatTourney_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "btnUpdate_Click")
{
[...]
Label lblSuccess= (Label)e.Item.FindControl("lblUpdateSuccess");
bindRepeater(ddlEvenements.Text, e.Item.ItemIndex);
lblSuccess.Visible = true;
}
}
}
Upvotes: 1
Views: 1826
Reputation: 460058
You haven't said what's going wrong, do you get an exception?
You could use ItemDataBound
to set the visibility. But therefore you have to store which index/id you have updated last, e.g in a field:
protected void repeatTourney_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "btnUpdate_Click")
{
updatedID = int.Parse(e.CommandArgument.ToString());
bindRepeater(ddlEvents.Text);
}
}
private int? updatedID = null;
protected void repeatTourney_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var tourney = (Tourney) e.Item.DataItem;
Label lblUpdateSuccess = (Label)e.Item.FindControl("lblUpdateSuccess");
lblUpdateSuccess.Visible = updatedID.HasValue && tourney.Id == updatedID.Value;
}
}
Upvotes: 2