Click here to Skip to main content
15,888,113 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.NET
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                         <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                              <ContentTemplate>
                                  <asp:DataList ID="dlAnswers" runat="server" RepeatColumns="1" RepeatDirection="Vertical">

                                 <ItemTemplate>
                                  <div class="comment-body comment-body-answered clearfix">
                             <div class="avatar">
                                 <asp:Image ID="ProfileImg" runat="server" ImageUrl='<%#Eval("ProfileImg") %>' />
                             </div>
                             <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>
                                         </ul>
                                     </div>
                                     <span class="question-vote-result">+1</span>
                                     <div class="comment-meta">
                                         <div class="date"><i class="icon-time"></i><%#Eval("AnsDate") %>

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

                                 </div>
                                 <div class="text">
                                     <asp:Label ID="lblAnswer" runat="server" Text='<%#Eval("Description")%>'></asp:Label></p>
                                 </div>
                                 <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">

                                     </div>

                                    </div>
                             </div>
                         </div>

                             </ItemTemplate>
                      </asp:DataList>


                                     <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>
                                     </ContentTemplate>
                             </asp:UpdatePanel>


---------------------------------------------------------------------------
C# code
--------------------------------------------------------------------------

C#
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("Width","700px;");
          txtAnswerComment.Style.Add("Height", "80px;");
          txtAnswerComment.Attributes.Add("runat", "server");
          txtAnswerComment.Font.Size = 9;
          txtAnswerComment.Style.Add("resize", "none;");

          PlComment.Controls.Add(txtAnswerComment);
         PlComment.Controls.Add(btnSubmit);
          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");
p.Controls.Add(txtAnswerComment);
p.Controls.Add(btnSubmit);
p.Controls.Add(btnCancel);
Posted
Comments
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 datalist..so 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