Tuesday, February 9, 2010

Tab Control Using MultiView in ASP.net C#

Here I have used Menu for Tab Buttons and MultiView for Tab contents.


Add the fallowing code in the page load event and menu item click event.



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            MultiView1.ActiveViewIndex = 0;
        
    }
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = Int32.Parse(Menu1.SelectedValue);
    }







<div>
        <asp:Menu ID="Menu1" runat="server" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal"
        style="left: 1px; position: relative; top: 30px" Height="38px" Width="127px" >
            <StaticMenuStyle HorizontalPadding="0px" VerticalPadding="0px" />
            <StaticSelectedStyle BackColor="#C0C0FF" BorderColor="#C0C0FF" />
           
            <Items>
                <asp:MenuItem Text="tab1" Value="0" Selected="True"></asp:MenuItem>
                <asp:MenuItem Text="tab2" Value="1"></asp:MenuItem>
                <asp:MenuItem Text="tab3" Value="2"></asp:MenuItem>
            </Items>
            <StaticHoverStyle BackColor="Silver" />
            <StaticMenuItemStyle BorderColor="#404040" BorderStyle="Solid" BorderWidth="1px" />
        </asp:Menu>
         </div>
        <div style="width: 429px; height: 191px; background-color: #C0C0FF;">
            <asp:MultiView ID="MultiView1" runat="server">
                <asp:View ID="View1" runat="server">
                   <br /> CONTENTS OF TAB1 (view1).</asp:View>
                <asp:View ID="View2" runat="server">
                    <br />
                    CONTENTS OF TAB2(view2)<br />
                </asp:View>
                <asp:View ID="View3" runat="server">
                    <br />
                    CONTENTS OF TAB3(view3)</asp:View>
            </asp:MultiView>
        </div>




Friday, February 5, 2010

Display the Cell Data of a Row in Textboxes using DropdownList for Identifying the Row

Here the dropdownlist is used to select a row through the ID column.
The selected row's cell data will be displayed on textboxes.
The database table I have used is as shown: (Table name: details1).

Bind the Dropdownlist with the ID column of the table either by using the fallowing code or by the smart task menu >> ConfigureDataSource wizard.








protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con1 = new SqlConnection("your connectionstring");
            con1.Open();
            SqlDataAdapter da1 = new SqlDataAdapter("select id from details1", con1);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            Dropdownlist1.DataSource = ds1;
            Dropdownlist1.DataTextField = "id";
            Dropdownlist1.DataBind();
            con1.Close();
        }
protected void  DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
    {

        SqlConnection con1 = new SqlConnection("your connectionstring");
        con1.Open();
        SqlDataAdapter da1 = new SqlDataAdapter("select name,designation,place from details1 where   id=@id", con1);
        da1.SelectCommand.Parameters.AddWithValue("@id", DropDownList1.Text);
        DataTable dt1 = new DataTable();
        da1.Fill(dt1);
        con1.Close();
        DataRow row1 = dt1.Rows[0];
        txtname.Text = row1["name"].ToString();
        txtdsgn.Text = row1["designation"].ToString();
        txtplace.Text = row1["place"].ToString();

    }

Monday, February 1, 2010

Nesting 3 DropdownLists in ASP.net C#

  •   Add 3 DropdownList controls to .aspx page.
  • Enable AutoPostback for the 1st and 2nd Dropdownlists.
public partial class Default : System.Web.UI.Page
{
    protected string[] d1 = { "select","eng", "mba"};

    protected string[] eng = { "select","ec","cs" };
    protected string []mba = { "select","fin", "hr" };
   

    protected string []ec = { "ec1", "ec2"};
    protected string []cs = { "cs1", "cs2"};

   
    protected string []fin = { "fin1", "fin2"};
    protected string []hr = { "hr1", "hr2"};

  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = d1;
            DropDownList1.DataBind();
        }
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedIndex == 1)
        {
            DropDownList2.DataSource = eng;
            DropDownList2.DataBind();
        }
        else if (DropDownList1.SelectedIndex == 2)
        {
            DropDownList2.DataSource = mba;
            DropDownList2.DataBind();
        }
       
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        switch (DropDownList2.SelectedIndex)
        {
            case 1:
                {

                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = ec;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = fin;
                        DropDownList3.DataBind();
                       
                    }
                    break;
                }
            case 2:
                {
                    if (DropDownList1.SelectedIndex == 1)
                    {
                        DropDownList3.DataSource = cs;
                        DropDownList3.DataBind();
                       
                    }
                    else if (DropDownList1.SelectedIndex == 2)
                    {
                        DropDownList3.DataSource = hr;
                        DropDownList3.DataBind();
                       
                    }
                }
                break;
         }

    }
}