Click here to Skip to main content
15,921,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datalist and in that there is a button whoes ID="btnComment". when clicked on this button controls should be dynamically generated and added to panel named "PlComment".
ASPX code:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                  <asp:DataList ID="dlAnswers" runat="server" RepeatColumns="1" RepeatDirection="Vertical">

                                  <div class="comment-body comment-body-answered clearfix">
                             <div class="avatar">
                                 <asp:Image ID="ProfileImg" runat="server" ImageUrl='<%#Eval("ProfileImg") %>' />
                             <div class="comment-text">
                                 <div class="author clearfix">
                                     <div class="comment-author"><a href='<%#Eval("FirstName")+" "+Eval("LastName") %>'></a></div>
                                     <div class="comment-vote">
                                         <ul class="question-vote">
                                             <li><a href="#" class="question-vote-up" title="Like"></a></li>
                                             <li><a href="#" class="question-vote-down" title="Dislike"></a></li>
                                     <span class="question-vote-result">+1</span>
                                     <div class="comment-meta">
                                         <div class="date"><i class="icon-time"></i><%#Eval("AnsDate") %>

                                          <a class="button small green-button" href="#" style="float:right;padding:5px 15px; margin-left:300px">Accept Solution</a>

                                 <div class="text">
                                     <asp:Label ID="lblAnswer" runat="server" Text='<%#Eval("Description")%>'></asp:Label></p>
                                 <div id="AnsCommentBox" class="question-answered question-answered-done">

                                    <%-- <i class="icon-comments"></i>--%>
                                   <%--  <input type="button" value="Have a Comment?" class="button btn btn-small" onclick="CreateControls()" />--%>
                                     <div id="divComment">




                                     <asp:Button ID="btnComment" runat="server" Text="Have a Comment?" CssClass="button btn btn-small" OnClick="PostComment" />
                                       <asp:Panel ID="PlComment" runat="server" ></asp:Panel>

C# code

protected void PostComment(object sender,EventArgs e)
          TextBox txtAnswerComment = new TextBox();
          Button btnSubmit = new Button();
          Button btnCancel = new Button();
          btnSubmit.Text = "Submit";
          btnCancel.Text = "Cancel";
          btnCancel.Style.Add("font-size", "12px;");
          btnCancel.Style.Add("Padding", "4px 10px;");
          btnCancel.Style.Add("font-size", "12px;");
          btnSubmit.Style.Add("Padding", "4px 10px;");

          btnSubmit.Attributes.Add("class", "button small blue-button");
          btnSubmit.Attributes.Add("runat", "server");
          btnSubmit.Style.Add("font-size", "12px");
          btnCancel.Attributes.Add("class", "button small red-button");
          btnCancel.Attributes.Add("runat", "server");

          txtAnswerComment.ID = "txtAnswerComment";
          txtAnswerComment.TextMode = TextBoxMode.MultiLine;
          txtAnswerComment.Style.Add("Height", "80px;");
          txtAnswerComment.Attributes.Add("runat", "server");
          txtAnswerComment.Font.Size = 9;
          txtAnswerComment.Style.Add("resize", "none;");

          PlComment .Controls.Add(btnCancel);
          btnCancel.Click += btnCancel_Click;

          //btnComment.Text = "Write Comment";

       protected void btnCancel_Click(object sender,EventArgs e)

            PlComment.Visible = false;
            btnComment.Visible = true;

What I have tried:

I have tried using FindControl() method but could not get it.
Panel p = (Panel)dlAnswers.FindControl("PlComment");
F-ES Sitecore 14-Apr-16 7:42am    
Where are you doing the FindControl? When you add controls dynamically you have to re-add them with every postback, so if you're not doing that and trying FindControl after the postback that'll be why it isn't working.
AP900 14-Apr-16 10:35am    
The code above is changed. I have tried Findcontrol before....
Richard Deeming 14-Apr-16 9:26am    
Why not just add the controls in the markup, and toggle the panel's Visible property when you click the button? That way, you don't have to worry about recreating the controls on postback.
AP900 15-Apr-16 6:58am    
I liked your idea and have successfully implemented it. Thank you
AP900 14-Apr-16 10:38am    
I tried using findcontrol but it throw error: Object reference not set...

And also the panel is in how to access it...?

As it is the datalist suppose there are three records fetched then for every record the panel should be displayed when clicked on the button...

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