在这里画一个完整的空白,这应该是非常简单的,但我尝试的一切都不会起作用。
我只是想从TextBox
中的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,但两者都没有工作。
请任何人都可以帮忙。
答案 0 :(得分:2)
您是否将数据绑定到Page_Load中的Repeater?因为如果您没有检查PostBack,那么在您点击按钮事件之前,数据将被重新填充!
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
rptQuestions.DataSource = yourDataSource;
rptQuestions.DataBind();
}
}
如果您没有IF语句检查IsPostBack值,那么它将重新填充您的Repeater,这就是标签工作的原因。 TextBox没有绑定到它的数据,因此输入的任何文本都将被删除。
答案 1 :(得分:0)
变化:
strAnswer = Request.Form[txtAnswer.Text];
为:
strAnswer = txtAnswer.Text;
这应该有效。您已经在文本框中引用了:
var txtAnswer = (TextBox)ri.FindControl("txtAnswers");