라디오 버튼 목록, 체크 박스 목록 또는 텍스트 상자가 될 수있는 질문 및 답변 목록이있는 설문 조사 페이지를 만듭니다. 이러한 컨트롤은 Controls.Add를 사용하여 ItemDataBound 이벤트의 Repeater에 동적으로 추가됩니다.중계기에서 동적으로 추가 된 컨트롤의 선택된 값을 찾을 수 없습니다
페이지를 제대로 렌더링 할 수 있지만 양식을 제출하고 repeater의 컨트롤을 반복하여 radiobuttons 및 textbox 값의 selectedvalues를 얻으면 FindControl은 null을 반환합니다. 선택한 값을 얻으려면 무엇을해야합니까? RepeaterItems 반복 it 시도하지만 null도 반환했습니다. FindControl의 다른 유형을 시도했지만 컨트롤 유형을 해결하지 못합니다. 나는 그러나이
<asp:Repeater ID="rptSurvey" runat="server" Visible="true" EnableViewState="true" >
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Question") %>
</ItemTemplate>
</asp:Repeater>
처럼 리피터에 선언 DataBinder을 추가하는 경우 내가 컨트롤을 동적으로 추가 할 싶지만이 일에 내가 제출하는 selectedvalues를 얻을 캔트, 작동합니다. 이것은 내 코드의 그쪽으로 주요 구조 ...
<html>
<asp:Repeater ID="rptSurvey" runat="server" Visible="true">
</asp:Repeater>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</html>
protected void Page_Load(object sender, EventArgs e)
{
...
if (!IsPostBack)
{
rptSurvey.DataSource = GetQuestions();
rptSurvey.DataBind();
}
...
}
protected void rptSurvey_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string question = (DataBinder.Eval(e.Item.DataItem, "Question")).ToString();
litQuestion = new Literal();
litQuestion.Text = question;
RadioButtonList rblAnswer = (RadioButtonList)item;
rptSurvey.Controls.Add(rblAnswer);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
...
Dictionary<int, string> answers = new Dictionary<int, string>();
try
{
var list = FindControls(rptSurvey, c => c is RadioButtonList || c is CheckBoxList || c is TextBox);
foreach (Control item in list)
{
QuestionId = int.Parse(Questions.Rows[list.IndexOf(item)][0].ToString());
if (item is TextBox)
{
TextBox txtAnswer = (TextBox)item;
answers.Add(QuestionId, txtAnswer.Text);
}
else if (item is RadioButtonList)
{
RadioButtonList rblAnswer = (RadioButtonList)item;
answers.Add(QuestionId, rblAnswer.SelectedItem.Text);
}
else if (item is CheckBoxList)
{
// Iterate through the Items collection of the CheckBoxList
string cblMultiAnswer = "";
for (int i = 0; i < cblAnswer.Items.Count; i++)
{
if (cblAnswer.Items[i].Selected)
{
cblMultiAnswer += cblAnswer.Items[i].Value + ",";
}
}
answers.Add(QuestionId, cblMultiAnswer);
}
}
bSurvey.BLInsertSurveyAnswers(answers, dateCreated, _userEmail);
}
}
public static List<Control> FindControls(Control parent, Predicate<Control> match)
{
var list = new List<Control>();
foreach (Control ctl in parent.Controls)
{
if (match(ctl))
list.Add(ctl);
list.AddRange(FindControls(ctl, match));
}
return list;
}
덕분에 Karlis 페이지와 컨트롤이 확인 렌더링합니다. 그것들이 Null이라는 값을 얻기 위해 컨트롤을 열거하려고 할 때. OnItemDataBound 컨트롤을 추가함으로써 컨트롤 트리를 생성하고 컨트롤 트리가 있다는 것을 알았습니다. 근본적으로 다른 것을 놓치고 있습니까? – Brendan
코드를 변경하여 controltree를 항상 빌드 했습니까? 어쩌면 당신은 당신의 코드를 inital post에서 업데이트 할 것입니다. – karlis
나는 당신의 소스 코드를 다른 모습으로 보았습니다 : rptSurvey.Controls.Add (rblAnswer) -> 나는 괜찮다고 생각합니다, 당신은 중계기에 추가하고 있지만, 항목에 추가해야합니다 -> e.Item.Controls.Add (rblAnswer) – karlis