Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Bind Checkboxlist to xml file in asp.net

How to bind asp.net checkboxlist to xml file using dataset

This asp.net C# example is about checkboxlist binding to xml file using dataset
Now we create an xml file. We have named the xml file as states.xml. This xml file contains some states name from india. This  states will be populated to dropdownlist using asp.net c#
To create xml file open website menu ,select add new item, select xml file name it as states.xml,save it in root folder.After creating the xml file add below markup to states.xml



In the .aspx page add the checkboxlist control. In the designer page we have named the checkboxlist control as cblStatesThe name of the state will be assigned to text part and id will be assigned to value part of asp.net check box list control

Complete Aspx Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

     <asp:CheckBoxList ID="cblStates" runat="server">
     </asp:CheckBoxList>
  
    </form>
</body>
</html>


To bind the dropdownlist to xml file we have created a method in page load method and named it as BindXmlToCheckBoxList().

Complete c# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

        private void BindXmlToCheckBoxList()
        {
            string filePath = Server.MapPath("~/States.xml");
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(filePath);

                cblStates.DataSource = ds;
                cblStates.DataTextField = "name";
                cblStates.DataValueField = "id";
                cblStates.DataBind();
                
            }
        }
    }
}

Bind asp.net Gridview to xml file using dataset

Bind gridview to xml file or read xml data and populate gridview in asp.net c# using dataset

This asp.net example is about binding xml file to asp.net gridview using c# with dataset lets create an example to implement asp.net gridview binding to xml file using dataset
Step 1: First let’s create a website. So start visual studio – File Menu—New – Website –Name the website and click ok. Then Add an xml by right clicking on solution--AddNewItem--select XML FIle--Name it as UserDetails.xml Inside the UserDetails.xml file add some user details tags and markups as shown below 


20fingers2brains Free Videos


 In the aspx designer page add the below code to add a gridview
   

            
                
                
                
                
            
        
    
Now lets create the code behind programming to bind gridview to an xml file This namespaces needs to be added to code behind .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

        
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
      {
        BindGridToXML();
      }
 }
private void BindGridToXML()
{
        using (DataSet ds = new DataSet())
        {
                ds.ReadXml(Server.MapPath("~/UserDetails.xml"));
                gvUserDetails.DataSource = ds;
                gvUserDetails.DataBind();
        }
}

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
     gvUserDetails.PageIndex = e.NewPageIndex;
      this.BindGridToXML();
}

Complete C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

        private void BindGridToXML()
        {
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(Server.MapPath("~/UserDetails.xml"));
                gvUserDetails.DataSource = ds;
                gvUserDetails.DataBind();
            }
        }

       protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUserDetails.PageIndex = e.NewPageIndex;
            this.BindGridToXML();
        }
    }
}
In this asp.net example we have learned how to bind xml file to a gridview

Bind Dropdownlist to xml File aspnet

Bind Populate Asp.Net C# Dropdown List from XML File using Dataset with Example

In this Asp.Net Tutorials i will explain how to bind or populate  xml file data to dropdownlist using dataset in asp.net c#.Let’s create an example to implement dropdownlist binding with xml file.

First let’s create a website. So start visual studio – File Menu—New – Website –Name the website and click ok.

Initially we create an xml file. We have named the xml file as states.xml. This xml file contains some states name from india. This  states will be populated to dropdownlist using asp.net c#.To create xml file open website menu ,select add new item, select xml file name it as states.xml,save it in root folder.After creating the xml file add below markup to states.xml


In the .aspx page add the dropdownlist control. In the designer page we have named the dropdownlist control as ddlstates

In the .aspx page add the dropdownlist control. In the designer page we have named the dropdownlist control as ddlstates The name of the state will be assigned to text part and id will be assigned to value part of asp.net drop down list control

    
  
   
This below namespaces needs to be added in code behind in order to bind xml to dropdownlist
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

To bind the dropdownlist to xml file we have created a method in page load method and named it as BindXmlToDropdown().
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
     {
        BindXmlToDropDown();
     }
            
}
private void BindXmlToDropDown()
{
            string filePath = Server.MapPath("~/States.xml");
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(filePath);
                ddlStates.DataSource = ds;
                ddlStates.DataTextField = "name";
                ddlStates.DataValueField = "id";
                ddlStates.DataBind();
            }
}

Complete C# code to populate dropdownlist from xml file 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

        private void BindXmlToDropDown()
        {
            string filePath = Server.MapPath("~/States.xml");
            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(filePath);
                ddlStates.DataSource = ds;
                ddlStates.DataTextField = "name";
                ddlStates.DataValueField = "id";
                ddlStates.DataBind();
            }
        }
    }
}


In this asp.net tutorial we have learned how to bind asp.net dropdownlist to xml file using dataset