我在 ASP.NET 数据网格中有以下代码:
<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>
根据用户需要给出的答案类型,占位符会动态填充文本框或下拉列表。呈现初始答案字段(文本框或下拉列表)的代码工作正常。
但是,当我触发按钮单击事件时,占位符的控件属性始终为空。为什么会这样?我已经为此绞尽脑汁好几天了,这对我来说没有任何意义。
下面是我试图访问数据网格的动态添加控件的片段......我做错了什么?
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;
}