?
Solved

Creating a page that displays results from dropdownlist ASP.net

Posted on 2014-10-31
1
Medium Priority
?
106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

770 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