![](/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.