TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
indradeo kumar
1.2k
546
7.5k
how to add dropdown list with gridview and update when sele
Jul 26 2019 5:48 AM
Dear Team,
kindly find my code details and help to complate same still i unable to complate database structur.
.aspx==
<
%@ Page
Language
=
"C#"
AutoEventWireup
=
"true"
Codefile
=
"DeptCotractDetai;s.aspx.cs"
Inherits
=
"Demoexsql.DeptCotractDetai_s"
%
>
<!DOCTYPE html
>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
>
</
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
asp:GridView
ID
=
"GridView1"
runat
=
"server"
AutoGenerateColumns
=
"false"
EnableViewState
=
"true"
OnRowDataBound
=
"GridView1_RowDataBound"
>
<
Columns
>
<
asp:BoundField
HeaderText
=
"Id"
DataField
=
"CountryId"
/>
<
asp:TemplateField
HeaderText
=
"Country"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"ddlCountries"
runat
=
"server"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"drpcountry_SelectedIndexChanged"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"State"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"ddlState"
runat
=
"server"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"drpstate_SelectedIndexChanged"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"City/Town"
>
<
ItemTemplate
>
<
asp:DropDownList
runat
=
"server"
ID
=
"ddlCity"
ClientIDMode
=
"Static"
Width
=
"100px"
>
</
asp:DropDownList
>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
<
br
/>
<
asp:Button
ID
=
"Button1"
Text
=
"Save"
runat
=
"server"
OnClick
=
"Save"
/>
<
br
/>
<
asp:GridView
ID
=
"Gridview2"
runat
=
"server"
AutoGenerateColumns
=
"false"
>
<
Columns
>
<
asp:BoundField
HeaderText
=
"Country"
DataField
=
"Country"
/>
<
asp:BoundField
HeaderText
=
"State"
DataField
=
"State"
/>
<
asp:BoundField
HeaderText
=
"City"
DataField
=
"City"
/>
</
Columns
>
</
asp:GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
.aspx.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;
using
System.Configuration;
namespace
Demoexsql
{
public
partial
class
DeptCotractDetai_s : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GridView1.DataSource = GetData(
"SELECT * FROM Countries"
);
GridView1.DataBind();
}
DataTable dt =
new
DataTable();
dt.Columns.AddRange(
new
DataColumn[] {
new
DataColumn(
"Country"
,
typeof
(
string
)),
new
DataColumn(
"State"
,
typeof
(
string
)),
new
DataColumn(
"City"
,
typeof
(
string
)) });
DropDownList ddlCountries =
null
;
DropDownList ddlStates =
null
;
DropDownList ddlCities =
null
;
foreach
(GridViewRow row
in
GridView1.Rows)
{
if
(row.RowType == DataControlRowType.DataRow)
{
ddlCountries = (row.FindControl(
"ddlCountries"
)
as
DropDownList);
ddlStates = (row.FindControl(
"ddlState"
)
as
DropDownList);
ddlCities = (row.FindControl(
"ddlCity"
)
as
DropDownList);
string
country = (ddlCountries.SelectedItem.Value ==
"0"
?
string
.Empty : ddlCountries.SelectedItem.Value);
string
state = (ddlStates.SelectedItem.Value ==
"0"
?
string
.Empty : ddlStates.SelectedItem.Value);
string
city = (ddlCities.SelectedItem.Value ==
"0"
?
string
.Empty : ddlCities.SelectedItem.Value);
if
(country !=
""
|| state !=
""
|| city !=
""
)
{
dt.Rows.Add(ddlCountries.SelectedItem.Text ==
"Select Country"
?
"NULL"
: ddlCountries.SelectedItem.Text, ddlStates.SelectedItem.Text ==
"Select State"
?
"NULL"
: ddlStates.SelectedItem.Text, ddlCities.SelectedItem.Text ==
"Select City"
?
"NULL"
: ddlCities.SelectedItem.Text);
}
else
{
dt.Rows.Add(country ==
""
?
"NULL"
: ddlCountries.SelectedItem.Value, country ==
""
?
"NULL"
: ddlStates.SelectedItem.Value, country ==
""
?
"NULL"
: ddlCities.SelectedItem.Value);
}
}
Gridview2.DataSource = dt;
Gridview2.DataBind();
}
}
private
DataSet GetData(
string
query)
{
string
conString = ConfigurationManager.ConnectionStrings[
"constr"
].ConnectionString;
SqlCommand cmd =
new
SqlCommand(query);
using
(SqlConnection con =
new
SqlConnection(conString))
{
using
(SqlDataAdapter sda =
new
SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using
(DataSet ds =
new
DataSet())
{
sda.Fill(ds);
return
ds;
}
}
}
}
private
void
BindDropDownList(DropDownList ddl,
string
query,
string
text,
string
value,
string
defaultText)
{
string
conString = ConfigurationManager.ConnectionStrings[
"constr"
].ConnectionString;
SqlCommand cmd =
new
SqlCommand(query);
using
(SqlConnection con =
new
SqlConnection(conString))
{
using
(SqlDataAdapter sda =
new
SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0,
new
ListItem(defaultText,
"0"
));
}
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
DropDownList ddlCountries =
null
;
DropDownList ddlStates =
null
;
DropDownList ddlCities =
null
;
if
(e.Row.RowType == DataControlRowType.DataRow)
{
ddlCountries = (e.Row.FindControl(
"ddlCountries"
)
as
DropDownList);
ddlStates = (e.Row.FindControl(
"ddlState"
)
as
DropDownList);
ddlCities = (e.Row.FindControl(
"ddlCity"
)
as
DropDownList);
BindDropDownList(ddlCountries,
"SELECT DISTINCT CountryName,CountryId FROM Countries"
,
"CountryName"
,
"CountryId"
,
"Select Country"
);
BindDropDownList(ddlStates,
"SELECT DISTINCT StateName,StateId FROM States"
,
"StateName"
,
"StateId"
,
"Select Country"
);
BindDropDownList(ddlCities,
"SELECT DISTINCT CityName,CityId FROM Cities"
,
"CityName"
,
"CityId"
,
"Select Country"
);
ddlStates.Enabled =
false
;
ddlCities.Enabled =
false
;
ddlStates.Items.Insert(0,
new
ListItem(
"Select State"
,
"0"
));
ddlCities.Items.Insert(0,
new
ListItem(
"Select City"
,
"0"
));
}
}
protected
void
drpcountry_SelectedIndexChanged(
object
sender, EventArgs e)
{
DropDownList ddlCountries = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlCountries.NamingContainer;
DropDownList ddlStates = (DropDownList)currentRow.FindControl(
"ddlState"
);
DropDownList ddlCities = (DropDownList)currentRow.FindControl(
"ddlCity"
);
ddlStates.Enabled =
false
;
ddlCities.Enabled =
false
;
ddlStates.Items.Clear();
ddlCities.Items.Clear();
ddlStates.Items.Insert(0,
new
ListItem(
"Select State"
,
"0"
));
ddlCities.Items.Insert(0,
new
ListItem(
"Select City"
,
"0"
));
int
countryId =
int
.Parse(ddlCountries.SelectedItem.Value);
if
(countryId > 0)
{
string
query =
string
.Format(
"select StateId, StateName from States where CountryId = {0}"
, countryId);
BindDropDownList(ddlStates, query,
"StateName"
,
"StateId"
,
"Select State"
);
ddlStates.Enabled =
true
;
}
}
protected
void
drpstate_SelectedIndexChanged(
object
sender, EventArgs e)
{
DropDownList ddlCountries = (DropDownList)sender;
GridViewRow currentRow = (GridViewRow)ddlCountries.NamingContainer;
DropDownList ddlStates = (DropDownList)currentRow.FindControl(
"ddlState"
);
DropDownList ddlCities = (DropDownList)currentRow.FindControl(
"ddlCity"
);
ddlCities.Enabled =
false
;
ddlCities.Items.Clear();
ddlCities.Items.Insert(0,
new
ListItem(
"Select City"
,
"0"
));
int
stateId =
int
.Parse(ddlStates.SelectedItem.Value);
if
(stateId > 0)
{
string
query =
string
.Format(
"select CityId, CityName from Cities where StateId = {0}"
, stateId);
BindDropDownList(ddlCities, query,
"CityName"
,
"CityId"
,
"Select City"
);
ddlCities.Enabled =
true
;
}
}
protected
void
Save(
object
sender, EventArgs e)
{
}
protected
void
Save(
object
sender, EventArgs e)
{
}
}
}
Regards,
Indradeo
Reply
Answers (
1
)
fetch value of two dropdown and then concate the value
what is factory and action page in entity framework ?