![](/img/trans.png)
[英]Dynamic Dropdownlist is always returning the first value, not the selected value
[英]DropdownList value selected is always return first value
目前,我有一個方法可以從數據庫中檢索名稱列表,並以arraylist的形式返回。
public static ArrayList GenerateFolderLocation(String username)
{
// Get User ID
SqlDataReader sqlUserID = GetUserInformation(username);
sqlUserID.Read();
int userid = int.Parse(sqlUserID["userid"].ToString());
SqlCommand cmd = new SqlCommand("SELECT distinct foldername FROM mb_folder WHERE userid=@userid", SQLGetMBoxConnection());
cmd.Parameters.AddWithValue("@userid", userid);
SqlDataReader sqldr = cmd.ExecuteReader();
ArrayList locationList = new ArrayList();
while (sqldr.Read())
{
locationList.Add(sqldr["foldername"].ToString());
}
locationList.Sort();
return locationList;
}
在頁面加載方法中,我使用DataSource
和DataBind
來填充我在主頁上顯示的下拉列表。 注意UploadLocation是我的下拉列表的ID
UploadLocation.DataSource= MBFolder.GenerateFolderLocation(Context.User.Identity.Name);
UploadLocation.DataBind();
在我的主頁上,我有這個下拉列表,還有一個提交按鈕
<asp:DropDownList ID="UploadLocation" runat="server"
AutoEventWireup="true" EnableViewState="true">
</asp:DropDownList>
<asp:Button ID="NewUploadFile" runat="server" Text="Upload" OnClick="NewUploadFile_Click" ValidationGroup="UploadFileValidation" AutoPostBack="true"/>
我的問題是,當我單擊提交按鈕時,它會觸發“ NewUploadFile_Click”,而在這種方法中,我想檢索我的下拉列表的選定值。 但是現在我能夠檢索該值,但這是我的下拉列表中的第一個值。 因此,例如,在我的數組列表中將有(test1,test2,test3),如果我選擇“ test2”,我的方法將改為檢索“ test1”。 在“ NewUploadFile_click”中,我使用UploadLocation.SelectedItem.Text;
獲得選定的值。 我究竟做錯了什么? 我如何檢索所選數據。 謝謝
嘗試將列表初始化代碼包裝在PageLoad事件的!IsPostBack {}部分中。
即在Page_Load
if (!IsPostback)
{
... Initialise List here
}
看起來您可能在獲得所需數據之前就重新綁定了列表。
當您調用綁定下拉列表的方法時,請嘗試將其添加到In PageLoad事件中:
if(!IsPostBack)
{
fillvalues();
}
因此,每次更改選擇都不會觸發
嘗試設置AutoPostBack="true";
進入您的DropDownList
<asp:DropDownList ID="UploadLocation" runat="server"
AutoEventWireup="true" EnableViewState="true" AutoPostBack="true";>
</asp:DropDownList>
確認您已啟用頁面級ViewState
並確認您的數據綁定代碼在!IsPostback
條件內
if (!IsPostback)
{
.... databinding code..
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.