2

I have the following code in an ASP.NET datagrid:

<asp:TemplateField HeaderText="" Visible="True" >
                            <ItemTemplate>
                            <%--<asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server" 
                                       TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px"  >
                            </asp:TextBox>--%>
                            <asp:HiddenField ID="hfQuestionID" Value='<%# Bind("QuestionID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswerID" Value='<%# Bind("AnswerID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswer" Value='<%# Bind("Answer") %>' runat="server" />
                            <asp:PlaceHolder ID="ph1" runat="server" />
                            <%--<%# GetAnswerControl(DataBinder.Eval(Container.DataItem,"QuestionID").ToString(), 
                                DataBinder.Eval(Container.DataItem,"Answer").ToString()) %>--%>
                            </ItemTemplate>
                           <EditItemTemplate>
                            <asp:HiddenField ID="hfQuestionID" Value='<%# Bind("QuestionID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswerID" Value='<%# Bind("AnswerID") %>' runat="server" />
                            <asp:HiddenField ID="hfAnswer" Value='<%# Bind("Answer") %>' runat="server" />
                            <asp:PlaceHolder ID="ph1" runat="server" />
                               <%--<asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server" 
                                       TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px"  ></asp:TextBox>--%>
                           </EditItemTemplate>
                         </asp:TemplateField>

The placeholder is dynamically populated with either a textbox or a dropdownlist depending on the type of answer the user needs to give. The code to render the initial answer fields (textbox or dropdown) works fine.

However, when I fire my button click event, the controls property of the placeholder is always empty. Why is this happening? I've been wracking my brain for days over this and it doesn't make any sense to me.

A snippet where I'm trying to access the datagrid's dynamically added controls follows...what am I doing wrong?

foreach (GridViewRow r in gridUserSupplierTypeQuestionsAndAnswers.Rows)
        //if (IsRowModified(r))
        //{
        //    gridUserSupplierTypeQuestionsAndAnswers.UpdateRow(r.RowIndex, false);
        //}
        {
            PlaceHolder ph =(PlaceHolder) r.FindControl("ph1");
            Control c = ph.FindControl("Answer");
            string answer = string.Empty;
            if(c.GetType() == typeof(TextBox))
            {
                TextBox tb = (TextBox) c;
                answer = tb.Text;
            }
            else if (c.GetType() == typeof(DropDownList))
            {
                DropDownList dl = (DropDownList)c;
                answer = dl.SelectedValue;
            }
Tim
  • 4,051
  • 10
  • 36
  • 60
  • 4
    Dynamically created controls have to be created each and every post-back... doing it once is not enough. This is assuming that is what you're doing! – freefaller Dec 14 '12 at 15:27
  • For reference: http://forums.asp.net/t/1098870.aspx/1 – Jason Evans Dec 14 '12 at 15:27
  • @JasonEvans , shouldnt you guys post that in the answers? I think that will help those that find this question in the future. – jmrnet Dec 14 '12 at 16:12
  • Problem solved in this post http://stackoverflow.com/questions/17589268/dynamically-created-controls-losing-data-after-postback – Steve Cole Jan 27 '17 at 17:09

1 Answers1

0

I wound up abandoning the placeholder and instead went with two controls - a textbox and a dropdownlist - with their visibility set to false.

I store the answer type in the database, and depending on that value turn the appropriate control on through a helper function. There are probably more elegant ways to handle this, but I'm tight on time and just need to get this working.

Tim
  • 4,051
  • 10
  • 36
  • 60