[英]Selected Index Changed event not firing for a DropDownList during databind at pageload
[英]Using dropdownlist to auto populate page during pageload
我是asp.net的新手,所以我的標題可能有點令人困惑,所以讓我解釋一下,我也可能這樣做的方式是錯誤的,因此,如果您有任何建議都很好。
當用戶登錄到我的網站時,他們將獲得其客戶端和網站的下拉列表。 他們可以有多個客戶端或站點。 他們登錄后,將被發送到顯示有關該站點信息的常規儀表板。
我為名為sitepicker的sitedrop下拉列表創建了一個用戶控件,該控件使用存儲過程填充2個下拉列表。 許多用戶只有一個客戶端和站點,因此我希望它自動選擇下拉列表中填充的第一個客戶端和站點,並將其用於常規儀表板。
這就是我填充網站下拉列表的方式。
void PopulateSiteList()
{
DataAccess da = new DataAccess();
da.AddParameter("portaluserid", Page.User.Identity.Name, DataAccess.SQLDataType.SQLString, 256);
da.AddParameter("ClientID", Session["ClientID"], DataAccess.SQLDataType.SQLInteger, 4);
DataSet SiteList = da.runSPDataSet("Portal_SitePickerSiteList");
DropDownListSite.DataSource = SiteList;
DropDownListSite.DataValueField = "SiteID";
DropDownListSite.DataTextField = "SiteName";
DropDownListSite.DataBind();
}
這是站點選擇器的頁面加載量。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["ClientName"] != null)
ClientButton.Text = Session["ClientName"].ToString();
if (Session["SiteName"] != null)
SiteButton.Text = Session["SiteName"].ToString();
LoadClientDDL();
if (DropDownListClient.Items.Count.Equals(1))
{
ClientButton.Enabled = false;
DropDownListClient.Visible = false;
int ClientID = int.Parse(DropDownListClient.SelectedItem.Value);
ClientButton.Text = DropDownListClient.SelectedItem.Text;
ClientButton.Visible = true;
Session["ClientID"] = ClientID;
Session["ClientName"] = DropDownListClient.SelectedItem.Text;
{
PopulateSiteList();
}
if (DropDownListSite.Items.Count > 0)
{
DropDownListSite.SelectedIndex = 1;
DropDownListSite.Visible = false;
SiteButton.Visible = true;
int SiteID = int.Parse(DropDownListSite.SelectedItem.Value);
SiteButton.Text = DropDownListSite.SelectedItem.Text;
Session["SiteID"] = SiteID;
Session["SiteName"] = DropDownListSite.SelectedItem.Text;
}
}
這樣一切都很好。 我的問題是,一般的儀表板頁面一旦加載,除非我點擊刷新,否則所有標簽都不會更新。
這是常規信息中心的page_load
protected void Page_Load(object sender, EventArgs e)
{
if (Session["SiteID"] != null)
{
SiteID = int.Parse(Session["SiteID"].ToString());
PopulateAccountData();
PopulateAccountInformation2();
PopulateSiteNodes();
}
else
LabelSiteName.Text = "No Site Selected";
}
void PopulateAccountData()
{
DataAccess da = new DataAccess();
da.AddParameter("SiteID", SiteID, DataAccess.SQLDataType.SQLInteger, 4);
SiteHeader = da.runSPDataSet("Portal_GetDashboardInfo");
LabelGeneralManagerFirstName.Text = SiteHeader.Tables[0].Rows[0]["FirstName"].ToString();
LabelGeneralManagerLastName.Text = SiteHeader.Tables[0].Rows[0]["LastName"].ToString();
LabelSite.Text = SiteHeader.Tables[0].Rows[0]["SiteName"].ToString();
}
我不確定自己是否做對了。 用戶登錄后,他們將被定向到儀表板頁面,除非刷新,否則它將始終顯示“未選擇任何站點”。
有關如何正確執行此操作的任何想法?
網站選擇器的HTML代碼
<table>
<tr>
<td><asp:Button ID="ClientButton" runat="server" OnClick="ClientButton_Click" Text="Select Client" /></td>
<td style="vertical-align:top"><asp:DropDownList ID="DropDownListClient" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListClient_SelectedIndexChanged" Visible="False" Height="36px">
</asp:DropDownList></td>
<td> </td>
<td><asp:Button ID="SiteButton" runat="server" OnClick="SiteButton_Click" Text="Select Site" /></td>
<td style="vertical-align:top"><asp:DropDownList ID="DropDownListSite" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownListSite_SelectedIndexChanged" Visible="False" Height="36px">
</asp:DropDownList></td>
</tr>
</table>
我認為您正在考慮的是postback
轉到您的xhtml,並確保下拉列表中的AutoPostback="True"
。
這將“ postback
”,並使您的頁面刷新並應用更改。 這允許在asp.net中進行客戶端和服務器通信。
這可能是為您提供更多信息的好資源:
http://msdn.microsoft.com/zh-CN/library/aa720416(v=vs.71).aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.