简体   繁体   English

选择的DropdownList值始终返回第一个值

[英]DropdownList value selected is always return first value

At the moment i have a method that retrieves a list of name from my database and return as an arraylist. 目前,我有一个方法可以从数据库中检索名称列表,并以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;
    }

And in my page load method, i use DataSource and DataBind to fill up a dropdownlist i have on my main page. 在页面加载方法中,我使用DataSourceDataBind来填充我在主页上显示的下拉列表。 Note UploadLocation is the id of my dropdownlist 注意UploadLocation是我的下拉列表的ID

UploadLocation.DataSource= MBFolder.GenerateFolderLocation(Context.User.Identity.Name);
UploadLocation.DataBind();

And in my main page i have this dropdownlist and i also have a submit button 在我的主页上,我有这个下拉列表,还有一个提交按钮

 <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"/>

What my problem is that when i click my submit button it fires the "NewUploadFile_Click" and in that method i want to retrieve the selected value of my dropdownlist. 我的问题是,当我单击提交按钮时,它会触发“ NewUploadFile_Click”,而在这种方法中,我想检索我的下拉列表的选定值。 However right now i am able to retrieve the value but it is the first value in my dropdownlist. 但是现在我能够检索该值,但这是我的下拉列表中的第一个值。 So for example, in my arraylist there would be (test1,test2,test3) and if i select "test2" my method would retrieve "test1" instead. 因此,例如,在我的数组列表中将有(test1,test2,test3),如果我选择“ test2”,我的方法将改为检索“ test1”。 In "NewUploadFile_click" i use UploadLocation.SelectedItem.Text; 在“ NewUploadFile_click”中,我使用UploadLocation.SelectedItem.Text; to get the selected value. 获得选定的值。 What am i doing wrong? 我究竟做错了什么? How can i retrieve the selected data. 我如何检索所选数据。 Thx 谢谢

Try wrapping your list initialisation code within an !IsPostBack {} section in the PageLoad Event. 尝试将列表初始化代码包装在PageLoad事件的!IsPostBack {}部分中。

ie in Page_Load 即在Page_Load

if (!IsPostback)
{
 ... Initialise List here
}

Looks like you may be rebinding the list before you have got the data you need. 看起来您可能在获得所需数据之前就重新绑定了列表。

When you are calling a method to bind dropdownlist, try to add it inside In PageLoad event: 当您调用绑定下拉列表的方法时,请尝试将其添加到In PageLoad事件中:

if(!IsPostBack)
{ 
    fillvalues();
}

So, It will not fire everytime you change your selections 因此,每次更改选择都不会触发

Try setting AutoPostBack="true"; 尝试设置AutoPostBack="true"; into your DropDownList 进入您的DropDownList

<asp:DropDownList ID="UploadLocation" runat="server" 
         AutoEventWireup="true" EnableViewState="true" AutoPostBack="true";>
 </asp:DropDownList>

Confirm that you page level ViewState is enabled 确认您已启用页面级ViewState
and also confirm that your databinding code is inside !IsPostback condition 并确认您的数据绑定代码在!IsPostback条件内

if (!IsPostback)
{
.... databinding code..
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM