[英]ASP.NET dynamic checkbox or dropdownlist
我在ASP.NET站點中有一個gridview。
現在有一個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>
現在的要求是添加可以由下拉列表回答的新問題。 我已經知道如何執行此操作(將問題編號傳遞到函數中並檢查是否存在存儲在另一個表中的下拉答案),但是我正在努力根據問題類型動態更新templatefield ...有什么建議么?
更新
我設法通過助手功能來部分解決這個問題。 (可能不是我最好的代碼,但是幾乎可以完成工作)問題是我得到了一個字符串,該字符串告訴我輸出中的類型,而不是實際的控件(分別是文本框或下拉列表)...我該如何糾正?
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;
}
}
我最終想辦法以另一種方法來工作...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.