0
我在ASP.NET网站中有一个gridview。ASP.NET动态复选框或下拉列表
有一个TemplateField现在工作正常 - 这是一个调查表,并在默认情况下问题的回答使用文本框:
<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>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAnswer" Text='<%# Bind("Answer") %>' runat="server"
TextMode="MultiLine" Height="76px" MaxLength="2000" Width="377px">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
的要求是要立即增加可以通过dropdownlists回答新问题。我已经知道如何做到这一点(将问题编号传入函数并检查是否存在存储在另一个表中的下拉答案),但是我正在根据问题类型动态更新模板字段...有什么建议么?
UPDATE
我设法得到这部分通过一个辅助函数工作。 (可能不是我最好的代码,但它几乎完成了工作)事情是我得到一个字符串,告诉我在我的输出中的类型,而不是实际的控件(分别为文本框或下拉列表)......我该如何纠正?
public Control GetAnswerControl(string QuestionID, string Answer)
{
List<ListItem> lstOptions = new List<ListItem>();
SqlCommand cmd = new SqlCommand("pDropDownAnswers_Get", functions.NewSupplierRequest);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@QuestionID", SqlDbType.Int);
cmd.Parameters["@QuestionID"].Value = Int32.Parse(QuestionID);
try
{
functions.NewSupplierRequest.Open();
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
lstOptions.Add(new ListItem(r["DropDownAnswer"].ToString(), r["DropDownAnswer"].ToString()));
}
}
catch (Exception err)
{
this.lblError.Text = err.Message;
}
finally
{
functions.NewSupplierRequest.Close();
}
if (lstOptions.Count == 0)
{
TextBox tb = new TextBox();
tb.ID = "txtAnswer";
tb.Text = Answer;
tb.TextMode = TextBoxMode.MultiLine;
tb.Height = 76;
tb.Width = 377;
tb.MaxLength = 2000;
return tb;
}
else
{
DropDownList dl = new DropDownList();
dl.DataSource = lstOptions;
dl.DataBind();
dl.SelectedValue = Answer;
return dl;
}
}
您是否也在为此使用JavaScript? – Smoore
目前,没有......只是直接ASP.NET代码。 – Tim