我有以下情形:ASP.NET:動態創建控件
我的頁面有一個允許用戶選擇類別的下拉列表。 對於每個類別,都有一組屬性,其值應從用戶獲取。每個類別的屬性數量都不相同。 根據用戶選擇的類別,應根據屬性創建一組下拉列表,並填充相應的屬性值。
由於需要的頁面不應該重新加載,我打算取數據(從SQL Server 2008)使用AJAX(?)。我是ASP.NET新手,雖然我對C#感到滿意,但還沒有使用過AJAX。需要關於如何進行的建議。如果我需要動態生成組合框,this是有用的嗎?
我有以下情形:ASP.NET:動態創建控件
我的頁面有一個允許用戶選擇類別的下拉列表。 對於每個類別,都有一組屬性,其值應從用戶獲取。每個類別的屬性數量都不相同。 根據用戶選擇的類別,應根據屬性創建一組下拉列表,並填充相應的屬性值。
由於需要的頁面不應該重新加載,我打算取數據(從SQL Server 2008)使用AJAX(?)。我是ASP.NET新手,雖然我對C#感到滿意,但還沒有使用過AJAX。需要關於如何進行的建議。如果我需要動態生成組合框,this是有用的嗎?
您可以使用這兩種情況下UpdatePanel
或PageMethods
,並在任何情況下,我會說,你需要知道的JavaScript,當你想使用AJAX,使動態Web應用程序。這需要一段時間,但是不用擔心。
您可以在所以在這裏搜索一下PageMethod的,例如看到這一個:
你可以用下面的辦法(如果你感覺不舒服建設與JavaScript更復雜的UI)。
它通過動態創建屬性DropDownLists,當頁面加載(您將基於數據庫查詢的結果實現它)並隱藏每個屬性,以備稍後顯示時使用。
選擇一個類別後,正確的DropDownLists將會變得可見(再次,這裏的查詢可以確定哪個屬性DropDownLists變爲可見)。
很明顯,它將需要一些修改,可能會生成一個Panel,其中包含每個DropDownList和一個Label控件,而不是隻創建一些DropDownLists。
然後,您將顯示/隱藏面板而不是DropDownList,但希望它指向正確的方向。
希望這會有所幫助。
標記
<style type="text/css">
select
{
display:block;
margin-top:10px;
}
</style>
....
<asp:ScriptManager ID="scriptManager" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<!-- Category selection -->
<asp:DropDownList ID="categoryDropDownList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="categoryDropDownList_SelectedIndexChanged">
<asp:ListItem Text="Please select a category" Value="0"></asp:ListItem>
</asp:DropDownList>
<br />
<!-- Used to store the drop downs -->
<asp:Panel ID="dropDownContainer" runat="server"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
代碼
protected void Page_Load(object sender, EventArgs e)
{
LoadDropDownLists();
}
private void LoadDropDownLists()
{
//Dummy data, you would pull your categories/attributes from whichever datasource
//you are using
var categories = new[]{
new { Name = "Category 1", Id = 1, Attributes = new string[]{"GA", "FA", "RA"} },
new { Name = "Category 2", Id = 2, Attributes = new string[]{"GA", "NA"} }
};
//Loop through the categories, load the dropdown
foreach (var category in categories)
{
if (!IsPostBack)
categoryDropDownList.Items.Add(new ListItem(category.Name, category.Id.ToString()));
//For each attribute create a drop down and populate with data as required
foreach (var attribute in category.Attributes)
{
string dropDownListId = FormatDropDownListId(attribute);
if (!DropDownListExists(dropDownListId))
{
DropDownList dropDownList = new DropDownList();
dropDownList.ID = dropDownListId;
dropDownList.Visible = false;
dropDownList.Items.Add(new ListItem(attribute));
dropDownContainer.Controls.Add(dropDownList);
}
}
}
}
private bool DropDownListExists(string id)
{
DropDownList dropDownList = (DropDownList)dropDownContainer.FindControl(id);
return dropDownList != null;
}
protected void categoryDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
//Reset all visible dropdowns
HideAllDropDownLists();
//Get the selected category
string selectedItem = categoryDropDownList.SelectedItem.Value;
switch (selectedItem)
{
case "1":
{
//Here you would connect to db and pull correct attributes
//then set the visible dropdowns as required
SetDropDownVisibility(FormatDropDownListId("GA"));
SetDropDownVisibility(FormatDropDownListId("FA"));
SetDropDownVisibility(FormatDropDownListId("RA"));
} break;
case "2":
{
SetDropDownVisibility(FormatDropDownListId("GA"));
SetDropDownVisibility(FormatDropDownListId("NA"));
} break;
}
}
private void SetDropDownVisibility(string id)
{
DropDownList dropDownList = (DropDownList)dropDownContainer.FindControl(id);
if(dropDownList != null)
dropDownList.Visible = true;
}
private void HideAllDropDownLists()
{
foreach (Control control in dropDownContainer.Controls)
{
control.Visible = false;
}
}
private string FormatDropDownListId(string id)
{
return string.Format("dropDown{0}", id);
}
如果你使用ASP.NET WebForms的話,我不相信你需要使用AJAX或JavaScript。
我會做以下
autopostback = true
添加事件處理程序的組合框可以EIT她隱藏組合框(正如我在@jdavies答案中看到的),或者沒有任何動作並動態創建&將它們添加到表單中。
有同樣的問題這個問題涉及:
你怎麼樣用JavaScript?因爲這就是你需要的AJAX。你可以在UpdatePanel中使用ASP.Net AJAX工具包,但它可能非常麻煩。好處是你並不需要知道JavaScript。 –
我沒有使用JavaScript :( – devnull
那麼這可能會阻礙你一點。如果這是時間緊迫,你可以使用ASP AJAX UpdatePanel控件http://msdn.microsoft.com/en-us/library/bb399001.aspx大多數你需要知道的是用c#完成的,但是我建議你學習javascript和jQuery(或者其他框架),因爲它在web開發中對你是非常有益的。 –