Solved

Creating a page that displays results from dropdownlist ASP.net

Posted on 2014-10-31
1
100 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
ID: 40416824
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

839 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