Treeview with CheckBox in Asp.Net


Description:-

In this example we explain that how to create Dynamic Treeview with checkbox in asp.net or create Dynamic treeview with checkbox in hierarchy format in asp.net. Or treeview check unchecks all checkboxes using JavaScript without Postback in asp.net. In this example we include checkbox in treeview to select particular tree node or all tree nodes by selection of checkbox.

Here we used JavaScript to avoid the Postback of the checkbox when checkbox is checked. You can also fetch all child tree node value by just selecting parent Treenode using checkbox selection. Or also you can fetch the selected checkbox tree node value for performing insert, update, and delete operation. Dynamically Bind Data in Ajax Accordion Panel Bind Data to Accordion from database Restrict the size of File when Uploaded How to Restrict the size of File when uploaded by user.

Create Table:-

      Country:
CREATE TABLE [dbo].[Country](
      [Country_ID] [int] IDENTITY(1,1)NOTNULL,
      [CountryName] [varchar](50)NOTNULL,
CONSTRAINT [PK_Country] PRIMARYKEYCLUSTERED
(
      [Country_ID] ASC
)WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
)ON [PRIMARY]

GO

      State_Mst:

CREATE TABLE [dbo].[State_Mst](
      [State_ID] [int] IDENTITY(1,1)NOTNULL,
      [StateName] [varchar](50)NOTNULL,
      [Country_ID] [int] NOTNULL,
CONSTRAINT [PK_State_Mst] PRIMARY KEY CLUSTERED
(
      [State_ID] ASC
)WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
)ON [PRIMARY] 
GO 
SETANSI_PADDINGOFF
GO 
ALTER TABLE [dbo].[State_Mst]  WITH CHECK ADD CONSTRAINT [FK_State_Mst_Country] FOREIGN KEY([Country_ID])
REFERENCES [dbo].[Country]([Country_ID])
GO 
ALTER TABLE [dbo].[State_Mst] CHECK CONSTRAINT [FK_State_Mst_Country]
GO

Default.aspx:-

<!DOCTYPEhtmlPUBLIC"-//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></title>
    </head>
    <body>
      <form id="form1" runat="server">
       <div>
         <asp:TreeView ID="trv_country_state_hierchy"runat="server">
         </asp:TreeView>
       </div>
      </form>
    </body>
  </html>

Default.aspx.cs:-

SqlConnection conn = new SqlConnection("your connection string");
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    PopulateTreeview();
  }
}

private void PopulateTreeview()
{
  try
  {
    DataSet ds = newDataSet();
    DataTable dtparent = new DataTable();
    DataTable dtchild = newDataTable();
    dtparent = FillParentTable();
    dtparent.TableName = "A";
    dtchild = FillChildTable();
    dtchild.TableName = "B";
    ds.Tables.Add(dtparent);
    ds.Tables.Add(dtchild);
    ds.Relations.Add("children", dtparent.Columns["Country_ID"], dtchild.Columns["Country_ID"]);
    if (ds.Tables[0].Rows.Count> 0)
    {
      trv_country_state_hierchy.Nodes.Clear();
      foreach (DataRow masterRow in ds.Tables[0].Rows)
      {
        TreeNodemasterNode = newTreeNode((string)masterRow["CountryName"],Convert.ToString(masterRow["Country_ID"]));
        trv_country_state_hierchy.Nodes.Add(masterNode);
        trv_country_state_hierchy.CollapseAll();
        foreach (DataRow childRow in masterRow.GetChildRows("Children"))
        {
          TreeNode childNode = new TreeNode((string)childRow["StateName"],Convert.ToString(childRow["Country_ID"]));
          masterNode.ChildNodes.Add(childNode);
          childNode.Value = Convert.ToString(childRow["State_ID"]);
        }
      }
    }
  }
  catch (Exception ex)
  {
    thrownewException("Unable to populate treeview" + ex.Message);
  }
}

private DataTable FillParentTable()
{
  DataTable dt = newDataTable();
  conn.Open();
  string cmdstr = "Select * from Country";
  SqlCommand cmd = newSqlCommand(cmdstr, conn);
  SqlDataAdapter adp = newSqlDataAdapter(cmd);
  adp.Fill(dt);
  conn.Close();
  return dt;
}

private DataTable FillChildTable()
{
  DataTable dt = newDataTable();
  conn.Open();
  string cmdstr = "Select * from State_Mst";
  SqlCommand cmd = newSqlCommand(cmdstr, conn);
  SqlDataAdapter adp = newSqlDataAdapter(cmd);
  adp.Fill(dt);
  conn.Close();
  return dt;
}

Related Posts

Previous
Next Post »

1 comments:

comments
Anonymous
October 13, 2017 at 7:02:00 PM GMT+5:30 delete

Thanks on your marvelous posting! I certainly enjoyed reading it,
you may be a great author.I will be sure to bookmark your blog and will often come back in the future.

I want to encourage you to definitely continue your great work, have a
nice afternoon!

Reply
avatar

Thanks for comments.....