Monday, October 5, 2009

AJAX Accordion Control DataBinding and Gridview with Code Behind C#

AJAX Accordion Control DataBinding and Gridview with Code Behind C#


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">.acc-header, .acc-selected {width: 300px;background-color: #c0c0c0;margin-bottom:2px;padding:2px;color:#444444;font-weight:bold;cursor:pointer;}.acc-content {width:300px;margin-bottom:2px;padding:2px;}.acc-selected, .acc-content {border:solid 1px #666666;}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<ajaxToolkit:Accordion ID="Accordion1" runat="server" TransitionDuration="100" FramesPerSecond="200"
FadeTransitions="true" RequireOpenedPane="false" OnItemDataBound="Accordion1_ItemDataBound"
    ContentCssClass="acc-content" HeaderCssClass="acc-header" HeaderSelectedCssClass="acc-selected">
    <HeaderTemplate>
        <%#DataBinder.Eval(Container.DataItem,"categoryName") %>
    </HeaderTemplate>
    <ContentTemplate>
        <asp:HiddenField ID="txt_categoryID" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"categoryID") %>' />
        <asp:GridView ID="GridView1" runat="server" RowStyle-BackColor="#ededed" RowStyle-HorizontalAlign="Left"
            AutoGenerateColumns="false" GridLines="None" CellPadding="2" CellSpacing="2"
            Width="300px">
            <Columns>
                <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Product Name"
                HeaderStyle-BackColor="#d1d1d1"  HeaderStyle-ForeColor="#777777">
                    <ItemTemplate>
                        <%#DataBinder.Eval(Container.DataItem,"productName") %>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </ContentTemplate>
</ajaxToolkit:Accordion>

    </div>
    </form>
</body>
</html>

 

using System;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getCategories();
        }
    }

    public void getCategories()
    {
        SqlConnection sqlConn = new SqlConnection(conString);
        sqlConn.Open();
        SqlCommand sqlSelect = new SqlCommand("SELECT * FROM Categories", sqlConn);
        sqlSelect.CommandType = System.Data.CommandType.Text;
        SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
        DataSet myDataset = new DataSet();
        sqlAdapter.Fill(myDataset);
        sqlConn.Close();
        Accordion1.DataSource = myDataset.Tables[0].DefaultView;
        Accordion1.DataBind();

    }

    protected void Accordion1_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
    {
        if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
        {
            SqlConnection sqlConn = new SqlConnection(conString);
            sqlConn.Open();
            SqlCommand sqlSelect = new SqlCommand("SELECT productName FROM Products where categoryID = '" + ((HiddenField)e.AccordionItem.FindControl("txt_categoryID")).Value + "'", sqlConn);
            sqlSelect.CommandType = System.Data.CommandType.Text;
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
            DataSet myDataset = new DataSet();
            sqlAdapter.Fill(myDataset);
            sqlConn.Close();
            GridView grd = new GridView();
            grd = (GridView)e.AccordionItem.FindControl("GridView1");
            grd.DataSource = myDataset;
            grd.DataBind();
        }
    }


    private string conString
    {
        get
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
            return connectionString;
        }
    }
}

Reference:: http://programming.top54u.co

1 comment:

Unknown said...

dapfor provide a net grid tutorial which is very helpful and informative visit dapfor. com