0

在这里画一个完整的空白应该非常简单,但我尝试的一切都行不通。

我只是想从 aTextBox中获取价值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>

我的点击代码如下

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;
     }
}

值从标签中返回没有问题,但文本框无法正常工作,因为您可以看到我同时使用了 Request.Form[txtAnswer.Text] 和 txtAnswer.Text 但两者都不起作用。

请任何人都可以帮忙。

4

2 回答 2

2

您是否将数据绑定到 Page_Load 中的中继器?因为如果您不检查 PostBack,数据将在您的按钮单击事件触发之前重新填充!

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

如果您没有 IF 语句检查 IsPostBack 值,那么无论如何它都会重新填充您的中继器,这就是标签工作的原因。TextBox 没有绑定任何数据,因此输入的任何文本都会被删除。

于 2013-10-29T21:34:15.240 回答
0

改变:

strAnswer = Request.Form[txtAnswer.Text];

至:

strAnswer = txtAnswer.Text; 

这应该有效。您已经通过以下方式引用了文本框:

 var txtAnswer = (TextBox)ri.FindControl("txtAnswers");
于 2013-10-29T21:27:32.730 回答