1
我已經添加了一個包含下拉菜單的動態用戶控件。加載用戶控件根據條件迭代'I'次。現在,我需要訪問下拉菜單中的選定項目。我創建了一個訪問器函數來獲取下拉選擇的值,但它給了我默認值,這是下拉的「00」。如何訪問c中用戶控件中的控件的數據#
如果有人需要額外的信息來解決我的問題, 請問我。
動態地添加用戶控件:
for (int i = 0; i < 3; i++)
{
//DailyControl is a userControl
DailyControl w1 = (DailyControl)LoadControl("~/DailyControl.ascx");
//InputPanel is a panel where user control is added.
InputPanel.Controls.Add(w1);
}
DailyControl.ascx
Select the Time:
<asp:DropDownList ID="clock" runat="server">
<asp:ListItem Text="am" />
<asp:ListItem Text="pm" />
</asp:DropDownList>
DailyControl.ascx.cs //訪問函數來獲取下拉
public string Clock
{
get
{
return clock.SelectedItem.Text.ToString();
}
}
您需要創建用戶控制訪問下拉的選擇價值屬性。一旦創建屬性,只需使用for循環和訪問屬性訪問頁面上的用戶控件。 –
請指定如何在用戶控件上創建屬性 – Ayushya
您已經擁有名爲'Clock'的屬性,因此當您訪問它時,您將獲得所選項目。改變你的返回語句爲:'返回clock.SelectedItem.Text.ToString();' –