Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:
C#
protected void Button1_Click(object sender,EventArgs e)
{
//removing "load more" button
newsdiv.Controls.Remove(Button1);
//creating new panel for holding current newsdata
            Panel pn = new Panel();
          
           //adding news title
            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);

            //create dislike linkbutton and add to newly created panel
            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);
            //adding newly created linkbutton to newly created panel
            pn.Controls.Add(lbt);

            //triggerto updatepanel
            AsyncPostBackTrigger trg = new AsyncPostBackTrigger();
            trg.ControlID = "did" + did.ToString();
            trg.EventName = "Click";

            //adding trigger to updatepanel
            updatepane.Triggers.Add(trg);

           //adding like linkbutton
            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);
           //adding newly created linkbutton to newly created panel
            pn.Controls.Add(lbt);

            //triggerto updatepanel
             trg = new AsyncPostBackTrigger();
             trg.ControlID = "lid" + lid.ToString();
             trg.EventName = "Click";
             updatepane.Triggers.Add(trg);
   
            //adding a paragraph line includeing postedby:"name" and post date
            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);

//newsshort body "dr1.GetValue(4).ToString()" and total likes and dislikes link when i hover it, it will show hovermenu(to be added in future)
            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);

           
            
         
            //assingning id and css class to newly created panel
            pn.ID = "panel" + dr1.GetValue(0).ToString();
            pn.CssClass = "article";
//adding newly created panel to main newscontainer
            newsdiv.Controls.Add(pn);
//adding "load more" button after updating the updatepanel
            newsdiv.Controls.Add(Button1);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900