Reputation: 1419
I'm very new to asp.net and c# I would like to get some help with dropdownlist. I want to return the ClassName value for a specific student from the Students table in a drop down list. I also want the drop down list to be populated with all the ClassNames from the StudentClasses table. But my code behind does return any values in the drop down list.
Can someone tell me what I've done wrong?
I have a table called StudentClasses with the following data, e.g.:
ClassID ClassName
==
1 5B
2 6C
3 K/1F
I have another table called Students where the ClassID from StudentClasses is a Foreign Key, e.g.:
StudentID FirstName LastName ClassID
==
1 John Smith 1
2 Sarah Jones 2
3 Billy Cain 2
I want to return on my page view the following:
Class will be a drop down list of all className from StudentClasses and show John Smith's Class, 5B. First Name and Last Name are textboxes. I also have an Update button so I can edit the details as well To edit Class, they can only choose from the drop down list.
First Name: John Last Name: Smith Class: 5B
This is the code behind by aspx page.
protected void rptStudents_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DropDownList ddlClassName = (DropDownList)e.Item.FindControl("ddlClassName");
ddlClassName.DataTextField = "ClassName";
ddlClassName.DataValueField = "ClassID";
//this is the object that is bound to this particular item (row)
StudentClass className = (StudentClass)e.Item.DataItem;
//TO DO: student should contain class id instead of name.
ddlClassName.SelectedValue = (className.ClassID).ToString();
//to do get list of class items here and set datasource and then databind();
Result<List<StudentClass>> result = StudentClassController.GetAllStudentClasses();
ddlClassName.DataSource = result.Data;
ddlClassName.DataBind();
}
Upvotes: 1
Views: 1938
Reputation: 7134
Make sure that your control is set to AutoPostBack=true and that on Page_Load() you aren't calling anything that would erase or overwrite the contents of the dropdown list. Specifically, I always always always screw this up myself by doing stuff like
protected void Page_Load(object sender, EventArgs e)
{
PopulateDropDownList();
}
but really you need to say
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateDropDownList();
}
}
I'm having a little trouble following your code without knowing the class structure and stuff, but here's a snippet of code that I use in our application to populate a drop down list with a text and a value:
EmployeeList allemployees = EmployeeManager.GetList();
EmployeeEDropDownList.Items.Clear();
EmployeeEDropDownList.Items.Add(""); //add a blank item on top to simulate no selection
foreach (Employee currentemp in allemployees)
{
EmployeeEDropDownList.Items.Add(new ListItem(currentemp.Name.FullNameE, currentemp.EmployeeID.ToString()));
}
Maybe that will give you an alternate idea. In my opinion this is a little bit "simpler" and easier to understand than what you have, but like I said, without seeing your entire application, I can't really make that judgment :)
Hope this helps.
Upvotes: 2