Solved

Creating a page that displays results from dropdownlist ASP.net

Posted on 2014-10-31
1
98 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

920 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

14 Experts available now in Live!

Get 1:1 Help Now