Hi All
I am trying to create a page for news in asp.net using ajax. What I want to achieve in this page is very complicated and a serious thing, and I want its solution from our community, because before posting this stuff here, I have googled for 4-5 days, but didnt get any solution. I cant waste anymore days searching the solution on internet.
Coming directly to the problem.
I want to create a news page with like,dislike button for each news and these two buttons are to be added dynamically when i fetch data from database.
what i am doing is,
1. fetch data from database
2. add data to newscontainer with id "newsdiv" using LiteralControl.
3. each time the loop executes, i create a new div or panel dynamically and add the database stuff. at same time i also create linkbuttons "like" and "dislike" and add them to newly created div or panel.
It means I add the fetched data into a panel or div and then i add this panel or div to main news container with id "newsdiv" as shown in below code. i also have a link button below "newsdiv" named "load more" when i click this link it loads one more news and displays in updatepanel.so this stuff works properly but the newly created liknbuttons "like" and "dislike" dont react to the existing update panel.I tried adding triggers for them to existing updatepanel dynamically but it is not working. when i click them, full page is loaded and the view of page gets refreshed. Updatepanel doesn't fire for newly created linkbuttons.
one more thing, i have come to know that if we add controls and triggers during form load, it works fine but if we try to do this in button click event then it doesnt work.
Is is possible to do what i am trying? and if yes, then how to do it? Please let me know how to achieve this
I have the following code in my news.aspx page:
<pre lang="HTML">
<asp:UpdatePanel ID="updatepane" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click"/>
</Triggers>
<ContentTemplate>
<asp:Panel id="newsdiv" class="mainbar" runat="server">
</asp:Panel>
<asp:LinkButton ID="Button1" runat="server" Text="Load More" AutoPostBack="true" onclick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
And my "load more" link button has following code:
protected void Button1_Click(object sender,EventArgs e)
{
newsdiv.Controls.Remove(Button1);
Panel pn = new Panel();
LiteralControl lt = new LiteralControl();
lt = new LiteralControl("<h2><span>" + dr1.GetValue(3).ToString() + "</span></h2>");
pn.Controls.Add(lt);
lt = new LiteralControl("<div class=\"clr\"></div>");
pn.Controls.Add(lt);
LinkButton lbt = new LinkButton();
lbt.Text = "Dislike";
lbt.Attributes.Add("id","did"+did.ToString());
lbt.Attributes.Add("style", "float:right;margin:10px 10px;top:0px;");
lbt.Click += new EventHandler(dislikelinkbutton_click);
pn.Controls.Add(lbt);
AsyncPostBackTrigger trg = new AsyncPostBackTrigger();
trg.ControlID = "did" + did.ToString();
trg.EventName = "Click";
updatepane.Triggers.Add(trg);
lbt = new LinkButton();
lbt.Text = "Like";
lbt.Attributes.Add("id","lid" + lid.ToString());
lbt.Attributes.Add("style", "float:right;top:0px;margin-top:10px;");
lbt.Click += new EventHandler(likelinkbutton_click);
pn.Controls.Add(lbt);
trg = new AsyncPostBackTrigger();
trg.ControlID = "lid" + lid.ToString();
trg.EventName = "Click";
updatepane.Triggers.Add(trg);
lt = new LiteralControl("<p>Posted by <a href=\"#\">" + postedby.ToString() + "</a> <span > • </span> Date : <a href=\"\">" + dr1.GetValue(2).ToString() + "</a></p>");
pn.Controls.Add(lt);
lt = new LiteralControl("<p>" + dr1.GetValue(4).ToString() + "<br />Total Likes <a href=\"\">" + likecount.ToString() + " people like this</a> Total Dislikes <a href=\"\">" + dislikecount.ToString() + " people dislike this</a> </p>");
pn.Controls.Add(lt);
pn.ID = "panel" + dr1.GetValue(0).ToString();
pn.CssClass = "article";
newsdiv.Controls.Add(pn);
newsdiv.Controls.Add(Button1);
}