Reputation: 95
At the moment i have a method that retrieves a list of name from my database and return as an 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. Note UploadLocation is the id of my dropdownlist
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. 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.
In "NewUploadFile_click" i use UploadLocation.SelectedItem.Text;
to get the selected value. What am i doing wrong? How can i retrieve the selected data. Thx
Upvotes: 0
Views: 2616
Reputation: 1121
Confirm that you page level ViewState
is enabled
and also confirm that your databinding code is inside !IsPostback
condition
if (!IsPostback)
{
.... databinding code..
}
Upvotes: 0
Reputation: 61
Try setting AutoPostBack="true";
into your DropDownList
<asp:DropDownList ID="UploadLocation" runat="server"
AutoEventWireup="true" EnableViewState="true" AutoPostBack="true";>
</asp:DropDownList>
Upvotes: 0
Reputation: 874
When you are calling a method to bind dropdownlist, try to add it inside In PageLoad event:
if(!IsPostBack)
{
fillvalues();
}
So, It will not fire everytime you change your selections
Upvotes: 0
Reputation: 1616
Try wrapping your list initialisation code within an !IsPostBack {} section in the PageLoad Event.
ie in Page_Load
if (!IsPostback)
{
... Initialise List here
}
Looks like you may be rebinding the list before you have got the data you need.
Upvotes: 2