rs82uk
rs82uk

Reputation: 739

Cannot get Text from TextBox in repeater

Drawing a complete blank here this should be very straightforward but everything I try won't work.

I am just trying to get the value from a TextBox within a Repeater.

<asp:Repeater ID="rptQuestions" runat="server">
                <ItemTemplate>
                    <li>
                        <div class="">
                        <asp:Label ID="intQuestion" runat="server" Visible="false" Text='<%#Eval("intQuestion")%>'></asp:Label>
                        <asp:Label ID="intQuestionType" runat="server" Visible="false" Text='<%#Eval("intQuestionType")%>'></asp:Label>
                        <p>
                            <asp:Label ID="lblQuestion" runat="server" Text='<%#Eval("strQuestion").ToString().Replace("[*Garage*]", GarageName)%>'></asp:Label>
                        </p>
                        <asp:RadioButtonList ID="rblAnswers" RepeatDirection="Horizontal" TextAlign="Left" runat="server"></asp:RadioButtonList>
                        <asp:CheckBoxList ID="cblAnswers" RepeatDirection="Horizontal" TextAlign="Left"   runat="server" ></asp:CheckBoxList>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="rblAnswers" CssClass="parsley-error" ErrorMessage="This value is required"></asp:RequiredFieldValidator>                            
                        <asp:TextBox ID="txtAnswers" TextMode="MultiLine" runat="server"></asp:TextBox>
                        </div>

                    </li>
                </ItemTemplate>
            </asp:Repeater>

And my on click code is as follows

foreach (RepeaterItem ri in rptQuestions.Items)
{
  var QuestionId = (Label)ri.FindControl("intQuestion");
  var QuestionType = (Label)ri.FindControl("intQuestionType");
  int intQuestion = Convert.ToInt32(QuestionId.Text);
  int intQuestionType = Convert.ToInt32(QuestionType.Text);

  var txtAnswer = (TextBox)ri.FindControl("txtAnswers");

  if (txtAnswer == null)
     {
       intAnswer = 0;
       strAnswer = Request.Form[txtAnswer.Text];
       strAnswer = strAnswer + txtAnswer.Text;
       strAnswer = strAnswer + "No Data";
     }
     else
     {
       intAnswer = 0;
       strAnswer = Request.Form[txtAnswer.Text];
       strAnswer = strAnswer + "-" + Request.Form[txt.Text];
       strAnswer = strAnswer + "Test Stuff";
       strAnswer = strAnswer + txtAnswer.Text;
     }
}

The Values come back from the labels without an issue but the textbox just wont work as you can see ive used both Request.Form[txtAnswer.Text] and txtAnswer.Text but neither are working.

Please can anyone help.

Upvotes: 1

Views: 1845

Answers (2)

CaptainStealthy
CaptainStealthy

Reputation: 388

Are you binding the data to the Repeater in your Page_Load? Because if you don't check for a PostBack, the data will get re-populated BEFORE your button click event fires!

 protected void Page_Load(object sender, EventArgs e)
 {
      if (!Page.IsPostBack)
      {
           rptQuestions.DataSource = yourDataSource;
           rptQuestions.DataBind();
      }
 }

If you don't have the IF statement checking the IsPostBack value, then it will re-populate your Repeater regardless, which is why the Labels work. The TextBox has no data bound to it, so any text that was entered gets erased.

Upvotes: 3

PhillyNJ
PhillyNJ

Reputation: 3903

Change:

strAnswer = Request.Form[txtAnswer.Text];

to:

strAnswer = txtAnswer.Text; 

This should work. You already have a reference to the textbox with:

 var txtAnswer = (TextBox)ri.FindControl("txtAnswers");

Upvotes: 0

Related Questions