I have an asp Button and four drop down lists that are populated dynamically from a database. When clicking the button, supposed to pass the selected values from the drop down list to a SQL stored procedure. Instead the first value in each drop down list is being passed instead of the selected value. Here is the code on the aspx page:
<asp:DropDownList runat="server" ID="ddlCountry" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlTypeStructure" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlCategory" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlSegment" AppendDataBoundItems="True">
</asp:DropDownList>
<asp:Button runat="server" ID="btnSearhProjects" Text="Go"
CausesValidation="False" onclick="btnSearhProjects_Click" UseSubmitBehavior="False" />
And here is the C# code behind for the button:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountries();
BindCategories();
BindSegments();
BindStructures();
}
}
public void SearchProjects()
{
Project project = new Project();
string country = ddlCountry.SelectedItem.Text;
string category = ddlCategory.SelectedItem.Text;
string segment = ddlSegment.SelectedItem.Text;
string structure = ddlTypeStructure.SelectedItem.Text;
List<Project> projectList = project.GetProjects(country, category, segment, structure);
gvProjects.DataSource = projectList;
gvProjects.DataBind();
}
protected void btnSearhProjects_Click(object sender, EventArgs e)
{
SearchProjects();
}
And this is how I am populating each drop down list:
public void BindCountries()
{
List<Country> countryList = new List<Country>();
Country country = new Country();
countryList = country.GetCountries();
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryID";
ddlCountry.DataSource = countryList;
ddlCountry.DataBind();
}
I have tried using ddlCountry.SelectedValue.Text and ddlCountry.SelectedItem.Value and they do not work. Any help would be greatly appreciated. Thank you.
I found the solution to my problem. I had an extra form tag on my master page with
method="post". This was causing the page to post back to itself. Once I removed the form tag, I was able to use the drop down lists properly. Thanks.