Thursday, 27 March 2014

Populate One Asp.net Dropdown based on Selection in Another Dropdown


http://www.aspdotnet-suresh.com/2010/10/how-to-populate-dropdown-based-on-other.html



Introduction

Here I will explain how to populate one dropdown based on selection in another dropdown asp.net using c#.

Description

I have three dropdowns Country dropwdown, State dropdown, Region dropdown I need to populate states dropdown based on country dropdown and I need to populate region dropdown based on states dropdown for that what we have to do first design three tables in sql server with data like this 

CountryTable 

StateTable

RegionTable

After that design your aspx page like this 


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CasCading Dropdowns Sample</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">
<tr>
<td>
Select Country:
</td>
<td>
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select State:
</td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlState_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
Select Region:
</td>
<td>
<asp:DropDownList ID="ddlRegion" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

In code behind write like this 


private String strConnection = "Data Source=XZCBJ017550;Initial Catalog=MySamplesDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindContrydropdown();
}

}
/// <summary>
/// Bind COuntrydropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from CountryTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "CountryID";
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 State 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 * from StateTable where CountryID="+CountryID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlState.DataSource = ds;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "StateID";
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 Region 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 * from RegionTable where StateID=" + StateID, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlRegion.DataSource = ds;
ddlRegion.DataTextField = "RegionName";
ddlRegion.DataValueField = "RegionID";
ddlRegion.DataBind();
ddlRegion.Items.Insert(0, new ListItem("--Select--", "0"));
}

Tuesday, 4 March 2014

How to make Scrollable GridView with a Fixed Header (freeze row) in .NET

http://dotnetawesome.blogspot.in/2014/03/how-to-make-scrollable-gridview-with-fixed-header-with-freeze-row.html


Saturday, 1 March 2014

MVC interview questions

http://www.codeproject.com/Articles/556995/MVC-interview-questions-with-answers#What_is_MVC(Model_view_controller)

Wednesday, 20 November 2013

Multi select dropdown-check-list

https://code.google.com/p/dropdown-check-list/

Description

Dropdown Check List is a javascript plugin based on the jQuery library that transforms a regular select html element into a dropdown checkbox list.

Disclaimer

Whilst Warren Omohundro has done an excellent job in keeping DDCL maintained after Adrian faced time constraints, he has also found that he is short of time. I (Richard Bairwell) have therefore stepped forward to try and maintain DDCL in at least the short to medium term. However, if any body feels they can devote time to maintaining and developing DDCL, then feel free to get in contact.

Usage

    $("#select_id").dropdownchecklist();
The existing select element with id 'select_id' is replaced by a dropdown checkbox list widget.
See the DEMO page for a detailed description, documentation, and live examples. This page is based on the functionality in the current, downloadable release.
See the TESTCASE page for a series of test cases that exercise DDCL in various ways. This page is based directly on the most current version checked into SVN, not on the stable release version.

Download

From download location.

Limitations

1) Keyboard / Tab processing is iffy at best. I have found no reliable way of detecting loss of focus to another component in order to force an auto-close of the dropdown.
2) <LABEL> tags associated with the underlying <SELECT> just do not work, since the <LABEL> is wired to the original <SELECT>, not the DDCL component. In particular, do not include the original <SELECT> within the span of the <LABEL> tag itself.
3) IFrame support is questionable
4) DDCL calculates size and position on widget initialization. This only operates properly if the wrapper elements containing the <SELECT> are not hidden. Therefore, the use of DDCL within dynamic components that hide/show the wrapper element is problematic.
5) DDCL V1.4 was the last tested with IE6. No future version of DDCL will test with IE6.
6) DDCL V1.4 is NOT compatible with jQuery 1.8

History

See change history.
Version 1.4 - requires jQuery 1.6.1, ui 1.8.13
Version 1.3 - copy of <OPTION> styling into the dynamic components
Version 1.2 (which never made it out of QA) - the various pop-up positioning problems reported in the issue log have been addressed
Version 1.1 - DDCL now based on jQuery 1.4.2, ui 1.8.4
Version 1.0 - the most significant difference between version 1.0 and 0.9 is the switch-over to jQuery ThemeRoller based styling. While many of the 0.9 class tags are still in place, some have been retired in favor of ThemeRoller settings. If you have customized your CSS based on 0.9, you will likely have to change some of your class name pattern matches.

License

Licensed like jQuery, see http://docs.jquery.com/Licensing

Acknowledgments

Due to time constraints, Adrian has had to step back from the active support of DDCL. But as of 09Sept2010, he has graciously allowed me to step in and re-activate the project.
Adrian, many thanks from all your users. Warren C. Omohundro - Ittrium, LLC