Also, please ignore the post title. I'm talking about the NewsList control, not NewsItem. It's been a long day...
Could you post the markup you're using?
Here is a simple example of a working Newslist control:
Markup:
<EPiServer:NewsList runat="server" PageLink="<%# CurrentPage.PageLink %>">
<NewsTemplate>
<asp:LinkButton runat="server" ID="btnLink" Text='<%# DataBinder.Eval(Container.DataItem, "PageName") %>' OnCommand="Button_Click" />
</NewsTemplate>
</EPiServer:NewsList>
Code-behind:
protected void Button_Click(object sender, CommandEventArgs e)
{
string foo = ((LinkButton) sender).Text;
}
Hi Arild, thanks for answering. Markup is as follows (... = code I've removed for sake of verbosity):
<EPiServer:NewsList runat="server" ID="MyList">
<NewsTemplate>
...
<asp:LinkButton ID="btnMyButton" CssClass="myCssClass" runat="server" CommandName="MyButton"
CommandArgument="<%# Container.CurrentPage.PageLink%>" OnCommand="btn_Click">
<asp:Literal ID="litMy" runat="server" Text="<% # Container.CurrentPage.GetTxt()%>"></asp:Literal>
</asp:LinkButton>
...
</NewsTemplate>
</EPiServer:NewsList>
Code behind:
protected void btn_Click(object sender, CommandEventArgs e)
{
...
}
Where do you bind data to your Newslist? This is normally done either in the markup (like I showed with PageLink="<%# CurrentPage.PageLink %>") or in codebehind (e.g. MyList.PageLink = CurrentPage.PageLink; MyList.DataBind();)
Btw I just copied and tried your code and it works (both with databinding in markup and in codebehind) - the btn_click event is firing.
Do you have AutoEventWireup="true" in your Page directive?
Hi Arild,
Thanks for responding.
The code appears to be bound in a hybrid way - there are elements in the code-in-front as you just described and there's a databind going on in the code behind too. AutoEventWireup is true in my Page directive.
Just to add, I added a LinkButton with that OnCommand outside of the NewsList tags and that works fine. So I presume it must be something to do with the DataBind that, for some reason, is removing those event handlers that are set in the code-in-front?
Yes, seems like it's related to databinding. Is the NewsList control listing the pages? Could you post more of the code/markup where databinding occurs?
Hi Arild, I've worked it out. The code beforehand was databinding within the PreRender, which appears to be far too late along the execution line. Placing it in OnInit works a treat. Thanks so much for your time. :)
I did try my best to search through current topics and couldn't find anything, so if this has already been answered, I sincerely apologise!
I have a NewsList control, and within the NewsTemplate element I have a LinkButton with an OnCommand event. I have also tried adding an OnClick event too, just to see whether it was an issue with that specific event, yet I see the same.
The event handlers are set in the code-in-front within the NewsList control tags. They're not created dynamically in the code behind. When the button is clicked, a postback occurs, but the event handler method is not called. There's clearly something I'm doing wrong and if anybody could point me in the right direction I'd appreciate it.
Also, first time on this forum, so hello! :)