Solved

Creating a page that displays results from dropdownlist ASP.net

Posted on 2014-10-31
1
96 Views
Last Modified: 2014-11-11
Dear all.

I have now created a form with 3 dropdlists that cascade succesfully.

Being unfamiliar with the terminology could anyone please assist with the following

1, I now need a submit button that loads a new page with the selected results of the 3rd ddlist in a repeating grid view. In addition to this two fields must be editable.

Please see my below code for what I have this far.

Note please ignore the country references this is from a previous tutorial file that I plan to clear up once more familiar with ASP.net

Codebehind.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;


public partial class CountryDropdowns : System.Web.UI.Page
{
private String strConnection = "Data Source=££££££;Initial Catalog=£££££;Persist Security Info=True;User ID=BIS;Password=£££££";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindContrydropdown();
}

}
/// <summary>
/// Bind Departmentdropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [DeptSID],[DeptCode],[DeptName],[ParentSID] FROM [dbo].[vIP_Dept]", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "DeptName";
ddlCountry.DataValueField = "DeptSID";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));

}
/// <summary>
/// Bind Curriculum Dropdown Based on CountryID
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [CAreaSID],[CAreaCode],[CAreaName],[ParentSID],[LedgerCode] FROM [dbo].[vIP_CurriculumArea] where ParentSID=" + CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlState.DataSource = ds;
ddlState.DataTextField = "CAreaName";
ddlState.DataValueField = "CAreaSID";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
if(ddlState.SelectedValue=="0")
{
ddlRegion.Items.Clear();
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
}

}
/// <summary>
/// Bind Course Offering dropdown based on Re
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(ddlState.SelectedValue);
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [CAreaSID],[OfferingCode], [OfferingDescription] FROM [dbo].[vIP_Offering] WHERE [CAreaSID] =" + StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlRegion.DataSource = ds;
ddlRegion.DataTextField = "OfferingDescription";
ddlRegion.DataValueField = "OfferingCode";
ddlRegion.DataBind();
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
}
protected void Button1_Click(object sender, EventArgs e)
{

}
}

Open in new window


DDlist.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CountryDropdowns.aspx.cs" Inherits="CountryDropdowns" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CasCading Dropdowns Sample</title>
    <style type="text/css">
        .style1
        {
            height: 26px;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td colspan="2" height="200px" valign="bottom">
<h3>Functional Skills Maths and English</h3> 
</td>
</tr>
<tr>
<td>
Select Department:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" 
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td class="style1">
Select Curriculum Area:
</td>
<td class="style1">
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" 
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
    Select Offering:
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>

Open in new window


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CountryDropdowns.aspx.cs" Inherits="CountryDropdowns" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CasCading Dropdowns Sample</title>
    <style type="text/css">
        .style1
        {
            height: 26px;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td colspan="2" height="200px" valign="bottom">
<h3>Functional Skills Maths and English</h3> 
</td>
</tr>
<tr>
<td>
Select Department:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true" 
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td class="style1">
Select Curriculum Area:
</td>
<td class="style1">
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true" 
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
    Select Offering:
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:Michael Arthey
1 Comment
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
Comment Utility
Hi,

I think what you need on that button click event is pass the values as querystring from there to another page.

Response.Redirect("editPage.aspx?countryId=" + ddlCountry.SelectedValue + "&stateId= " + ddlState.SelectedValue+ "&regionId= " + ddlRegion.SelectedValue);

Open in new window


This assumes that yuo are grabbing the values of all three (country, state, region).

Then you pass the values to the page you wish to perform the edit. In my example, I used editPage.aspx.

Finally, on editPage.aspx, you grab those values something like this:

var selectedCountryval = Request.QueryString["countryId"];
 var selectedStateval = Request.QueryString["stateId"];
var selectedRegionval = Request.QueryString["stateId"];

Open in new window


Then you can query your db and perform your update sates and then edit whatever record.

Note, I am not a C# so my casing might be off.

Good luck
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now