Thursday, 27 February 2014

Checkboxlist, ListBox ,RadioButtonlist, Radiobutton in Asp.net

CheckBoxList 

<asp:CheckBoxList ID="Edu_ChkBxLst" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Value="0" Text="Diploma"></asp:ListItem>
                        <asp:ListItem Value="1" Text="UG" >  </asp:ListItem>
                        <asp:ListItem Value="2" Text="PG"></asp:ListItem>
                        <asp:ListItem Value="3" Text="MPhil/PhD"></asp:ListItem>

                    </asp:CheckBoxList>

In CodeBehind ,
 To Insert

foreach (ListItem li in Edu_ChkBxLst.Items)
        {
            if (li.Selected)
            {
                if (_pro_Reg.Education == null)
                {
                    _pro_Reg.Education = li.Text.ToString();

                }
                else
                {
                    _pro_Reg.Education = _pro_Reg.Education + "," + li.Text.ToString();
                }
            }

        }

To Update in GridView
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {

            int index = Convert.ToInt32(e.CommandArgument);
           string Edu = GridView1.Rows[index].Cells[6].Text.Trim();

            string[] qual = Edu.Split(' ');

            for (int i = 0; i < qual.Length; i++)
            {
                for (int j = 0; j < Edu_ChkBxLst. Items.Count; j++)
                {
                    if (qual[i].Contains(Edu_ChkBxLst.Items[j].Text.Trim()))
                    {
                        Edu_ChkBxLst.Items[j].Selected = true;
                    }
                }

            }
ListBox

<asp:ListBox ID="LB_Tech_Knowledge" runat="server" TabIndex="7"
                        SelectionMode="Multiple">
                        <asp:ListItem Value="0">Dot Net</asp:ListItem>
                        <asp:ListItem Value="1">SQL Server</asp:ListItem>
                        <asp:ListItem Value="2">AJAX</asp:ListItem>
                        <asp:ListItem Value="3">WebService</asp:ListItem>
                        <asp:ListItem Value="4">JSON</asp:ListItem>
                        <asp:ListItem Value="5">LINQ</asp:ListItem>
                        <asp:ListItem Value="6">JQuery</asp:ListItem>
                        <asp:ListItem Value="7">MVC</asp:ListItem>

                    </asp:ListBox>

In CodeBehind ,
 To Insert
foreach (ListItem li in LB_Tech_Knowledge.Items)
        {
            if (li.Selected)
            {
                if (_pro_Reg.Tech == null)
                {
                    _pro_Reg.Tech = li.Text.ToString();
                }

                else
                {
                    _pro_Reg.Tech = _pro_Reg.Tech + "," + li.Text.ToString();

                }
            }
        }

To Update in GridView

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {

            int index = Convert.ToInt32(e.CommandArgument);
           string LB_Tech = GridView1.Rows[index].Cells[7].Text.Trim();

            string[] tec = LB_Tech.Split(',');

            for (int i = 0; i < tec.Length; i++)
            {
                for (int j = 0; j < LB_Tech_Knowledge.Items.Count;j++)
                {
                    if (tec[i].Contains(LB_Tech_Knowledge.Items[j].Text.Trim()))
                    {
                        LB_Tech_Knowledge.Items[j].Selected = true;
                    }
                }

            }

RadioButtonList

<asp:RadioButtonList ID="RBList_Color" runat="server" RepeatColumns="2"
                        RepeatDirection="Horizontal" TabIndex="9">
                        <asp:ListItem Value="0" Text="Green"></asp:ListItem>
                        <asp:ListItem Value="1" Text="Yellow"></asp:ListItem>
                        <asp:ListItem Value="2" Text="Orange"></asp:ListItem>
                        <asp:ListItem Value="3" Text="Blue"></asp:ListItem>

                    </asp:RadioButtonList>
In CodeBehind ,
 To Insert
_pro_Reg.FavColor = RBList_Color.SelectedItem.Text.ToString();

To Update in GridView

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {

            int index = Convert.ToInt32(e.CommandArgument);
          string fav = GridView1.Rows[index].Cells[9].Text.Trim();
           
            for (int j = 0; j < RBList_Color.Items.Count; j++)
            {
                if (fav.Contains(RBList_Color.Items[j].Text.Trim()))
                {
                    RBList_Color.Items[j].Selected = true;
                }
            }


RadioButton
<asp:RadioButton ID="RBtn_Male" GroupName="Gender"  Text="Male" TabIndex="5" runat="server" />
                    <asp:RadioButton ID="RBtn_Female" GroupName="Gender"   Text="Female" runat="server" />
In CodeBehind ,
 To Insert
if (RBtn_Female.Checked)
        {
            _pro_Reg.Gender = RBtn_Female.Text;
        }
        else
        {
            _pro_Reg.Gender = RBtn_Male.Text;

        }
To Update in GridView

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {

            int index = Convert.ToInt32(e.CommandArgument);
            string gender = GridView1.Rows[index].Cells[5].Text.Trim();
            if (gender == "Male")
            {
                RBtn_Male.Checked = true;
            }
            if (gender == "Female")
            {
                RBtn_Female.Checked = true;
            }


                

Cascading DropDownlist

We have three tables  1)tblcontinents 2)tblCountry  3)tblState
Stored Procedure for the above named tables for doing operation in Cascading DropDownlist

create proc [dbo].[SP_GetContinent]
as
begin
select Continentid,Continentname from dbo.tblcontinents
end

create proc [dbo].[SP_GetCountriesByContinentID]
@Continent_id int
as
begin
select Countryid,Countryname from dbo.tblCountry where Continent_id =@Continent_id
end

create proc [dbo].[SP_GetStatesByCountry]
@Country_id int
as
begin
select Stateid,Statename from dbo.tblState where Country_id =@Country_id
end
We have to design as like below 

<asp:DropDownList ID="DDL_Continent" runat="server" Width="150px"  CausesValidation="true"
                        DataTextField="Continentname" DataValueField="Continentid"  AutoPostBack="true" 
                        onselectedindexchanged="DDL_Continent_SelectedIndexChanged" TabIndex="10">
                    </asp:DropDownList>

<asp:DropDownList ID="DDL_Country" runat="server" Width="150px" 
                        DataTextField="Countryname" DataValueField="Countryid" 
                        onselectedindexchanged="DDL_Country_SelectedIndexChanged" TabIndex="11" AutoPostBack="true">
                    </asp:DropDownList>

<asp:DropDownList ID="DDL_City" runat="server" Width="150px" 
                        DataTextField="Statename" DataValueField="Stateid"  TabIndex="12" AutoPostBack="true">
                        
                    </asp:DropDownList>
 This  is  reusable stored procedure function
private DataSet GetData(string spname, SqlParameter Parameter)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter(spname, con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        if (Parameter != null)
        {
            da.SelectCommand.Parameters.Add(Parameter);
        }
        ds = new DataSet();
        da.Fill(ds);
        return ds;
    }

protected void DDL_Continent_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (DDL_Continent.SelectedIndex == 0)
        {
            DDL_Country.SelectedIndex = 0;
            DDL_Country.Enabled = false;

            DDL_City.SelectedIndex = 0;
            DDL_City.Enabled = false;
        }

        else
        {
            DDL_Country.Enabled = true;
            SqlParameter par = new SqlParameter("@Continent_id", DDL_Continent.SelectedValue);
            ds = new DataSet();
            ds = GetData("SP_GetCountriesByContinentID", par);


            DDL_Country.DataSource = ds;
            DDL_Country.DataBind();


            ListItem li = new ListItem("Select Country", "-1");
            DDL_Country.Items.Insert(0, li);


            DDL_City.SelectedIndex = 0;
            DDL_City.Enabled = false;
        }
    }
    protected void DDL_Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDL_Country.SelectedIndex == 0)
        {
            DDL_City.SelectedIndex = 0;
            DDL_City.Enabled = false;

        }
        else
        {
            DDL_City.Enabled = true;
            SqlParameter par = new SqlParameter("@Country_id", DDL_Country.SelectedValue);
            ds = new DataSet();
            ds = GetData("SP_GetStatesByCountry", par);

            DDL_City.DataSource = ds;
            DDL_City.DataBind();

            ListItem li = new ListItem("Select City", "-1");
            DDL_City.Items.Insert(0, li);

            DDL_City.SelectedIndex = 0;
            DDL_City.Enabled = true;


        }


    }
We have to call this below method in Page_Load event

    private void Initial_PageLoad()
    {
        DDL_Continent.DataSource = GetData("SP_GetContinent", null);
        DDL_Continent.DataBind();

        ListItem licont = new ListItem("Select Continent", "-1");
        DDL_Continent.Items.Insert(0, licont);

        ListItem liCountry = new ListItem("Select Country", "-1");
        DDL_Country.Items.Insert(0, liCountry);


        ListItem liCity = new ListItem("Select City", "-1");
        DDL_City.Items.Insert(0, liCity);

    }