Posts tagged: multiple dropdownlist using asp.net

Sep 01 2009

asp.net checkbox control

The asp.net checkBox controls have an option to select or deselect
select means yes or true
Deselect means no or false

<asp:CheckBox ID=”CheckBox1″ runat=”server” Text=”Agree” />


Main Properties of CheckBox

Checked : whether checkbox is checked or not

OnCheckedChanged : when checkbox event is chanaged to be executed

AutopostBack : to activate OnCheckedChanged event make AutoPostBack         property is True

Text:
text appear next to the checkbox

How to check CheckBox is checked or not

if checkBox is checked then textbox1 display “He agree to terms and conditions.

If checkBox is not checked then textbox1 displays “He has not agree to terms and conditions.

Sample code:

In Default.aspx page

<div>
<asp:CheckBox ID=”CheckBox1″ runat=”server” Text=”Agree” />
<br />
<asp:TextBox ID=”TextBox1″ runat=”server” Width=”300px”></asp:TextBox>
<br />
<asp:Button ID=”Button1″ runat=”server” Text=”Button” onclick=”Button1_Click” />
</div>

In Default.aspx.cs page (Button Double click)

protected void Button1_Click(object sender, EventArgs e)
{
if (CheckBox1.Checked)
{
TextBox1.Text = “he ” + CheckBox1.Text + ” terms and condition”;
}
else
{
TextBox1.Text = “he has not ” + CheckBox1.Text + ” terms and condition”;
}
}

Getting asp.net CheckBox value into string

string chBoxItem = CheckBox1.Text;

Happy coding…

Aug 31 2009

Multiple DropDownlist Using Asp.net and Database

Step1 : Create category table in your Database (the table creation code is below link

category and subcategory tables

Step2: Create SubCategory table in Your Database

CREATE TABLE [dbo].[tblSubCategory](
[SubCategoryID] [int] IDENTITY(1,1) NOT NULL,
[CategoryID] [int] NOT NULL,
[SubCategory] [varchar](50)  NOT NULL,
CONSTRAINT [PK_tblSubCategory] PRIMARY KEY CLUSTERED
(
[SubCategoryID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Step 3: Enter Below Data into Subcategory table

subcategory

subcategory

step 4: Drag Two Dropdownlist in Default.aspx page

DropDownList1 Property of ID is ddlCategories and AutoPostBack is True //Remember

DropDownList2 Property of ID is ddlSubCategories

Step 5: Write below code in Default.aspx.cs . Write Connection String code above page_load event then it can apply every function in Default.aspx.cs

Sample code:

//Data Source = Your server name or local  sqlserver name

//Data Source=./SQLEXPRESS (for local Sqlserver)

//Data Source = santhu or 192.168.1.34 (for remote servers)

string ConnectionString = “Data Source=santhu;Initial Catalog=DataBaseName;User Id=sa;Password=abc123″;

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
GetCategories();
}

}

//Filling Categories code

public void GetCategories()
{
SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select CategoryID,CategoryName from tblCategory”;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

objConn.Open();
objDa.Fill(objDs);
objCmd.ExecuteNonQuery();

ddlCategories.DataTextField = “CategoryName”;
ddlCategories.DataValueField = “CategoryID”;

ddlCategories.DataSource = objDs;
ddlCategories.DataBind();

objConn.Close();
}

//Filling SubCategories Code in SelectedIndexChanged

protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
{

//getting CategoryID
long catID = long.Parse(ddlCategories.SelectedItem.Value);

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select *from tblsubCategory where CategoryId=”+catID;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

objConn.Open();
objDa.Fill(objDs);
objCmd.ExecuteNonQuery();

ddlSubCategories.DataTextField = “SubCategory”;
ddlSubCategories.DataValueField = “SubCategoryID”;

ddlSubCategories.DataSource = objDs;
ddlSubCategories.DataBind();

objConn.Close();
}

Happy Coding….

Alibi3col theme by Themocracy