问题
so i have removed the datasouce and just have DataBind() then my page is still blowing and refreshing the page and not in a EDIT mode.
what i am trying to do is when the user click on Edit button then make it inline editing the repeater row.
END UPDATE
onItemCommand i have added DataBind()
rpt.DataSource = mydatasource;
rpt.DataBind();
after i do that my page is not in edit mode and it blow away and everyting is refreshed i have on page_load
if (!IsPostBack)
{
rpt.DataSource = mydatasource;
rpt.DataBind();
}
end update
I've used repeaters many times without problems but something is going on here. I have a repeater and I'm subscribing to the itemDatabound event, But when i click the button (which is a linkbutton inside my repeater itemtemplate) it does not go to the ItemDataBound
<asp:Repeater ID="rpt" runat="server" OnItemCommand="rpt_OnItemCommand" OnItemDataBound="rpt_OnItemDataBound">
<ItemTemplate>
<li>
<asp:Label ID="Label" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CommandArgument='<%# Eval("MyID") %>'
Text='<%# Eval("Title") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
}
protected void rpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == EditIndex)
{
// never come to this line.... after the user click on LinkButton
}
}
}
回答1:
Don't know if this helps but you must call DataBind() in order for for the OnItemDataBound event to fire. Also my guess is you are trying to set the EditIndex in the OnItemCommand and then use the value in the OnDataBind event. The events fire in the order OnItemDataBound then OnItemCommand so the EditIndex wouldn't be correct anyway in that situation.
Add rpt.DataBind to the OnItemCommand. This workded when I tried it from your code, NOTE that you will be binding twice if you aren't using !IsPostBack for original data bind.
rpt.DataSource = strings;
if (!IsPostBack)
{
rpt.DataBind();
}
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
rpt.DataBind();
}
回答2:
You must change your rpt_OnItemCommand function.
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
else if (e.CommandName == "Complete")
{
// your function goes here
}
}
回答3:
Why do you think that the ItemDataBound
is raised when you click your LinkButton
? ItemDataBound
is only fired when Repeater.DataBind()
is called.
Actually the repeater's ItemCommand
event is raised instead.
回答4:
I'm a little confused, but from the example above it looks like you've got it backwards. The button click would never fire the ItemDataBound
event. The ItemDataBound
event is only called after each item is bound to the repeater.
The button click should fire the ItemCommand
event however, and if that's not happening I would check to make sure you've actually assigned the ItemCommand
handler, and also make sure that the command name is valid.
On a side note, this behavior can also happen when the repeater is bound at every postback. Make sure that you're binding the repeater when !Page.IsPostBack
.
来源:https://stackoverflow.com/questions/10132215/respond-to-button-events-inside-repeater-itemtemplate