Wednesday, July 28, 2010

How to bind or populate Dropdownlist from XML file




Please visit my new Web Site WWW.Codedisplay.com



Few days ago i got an email from one of my reader. He wants more article on XML specially on Dropdownlist. Here in this article i will explain how one can bind or populate XML data into a Dropdownlist control. Asp.net DataSet provide us a method named ReadXml where we can initially load XML file. After that we can populate Dropdownlist DataTextField & DataValueField by DataSet default view table. To do the example first add an aspx page in your project then add a Dropdownlist control. After that add an XML file like below:









<?xml version="1.0" encoding="utf-8" ?>
<Products>
  <Product>
    <ID>1</ID>
    <Name>Lux</Name>
  </Product>
  <Product>
    <ID>2</ID>
    <Name>Harpic</Name>
  </Product>
  <Product>
      <ID>3</ID>
      <Name>Dove</Name>
  </Product>
  <Product>
      <ID>4</ID>
      <Name>Sunsilk</Name>
  </Product>
  <Product>
      <ID>5</ID>
      <Name>Pentine</Name>
  </Product>
</Products>

And then under page_load event write the below code:
using System;
using System.Data;

public partial class Dropdownlist_XML : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet RS = new DataSet();
            RS.ReadXml(Server.MapPath("~/ProductList.xml"));
            
            DataView dv = RS.Tables[0].DefaultView;

            //Sorting by column name "Name" defined in XML file 
            dv.Sort = "Name";

            // Set the DataTextField and DataValueField
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "ID";

            DropDownList1.DataSource = dv;
            DropDownList1.DataBind();
        }
    }
}

Run the page to see that the Dropdownlist bind data as per XML file data.

Monday, July 26, 2010

Apply CSS to create Image Border




Please visit my new Web Site WWW.Codedisplay.com



Sometimes Asp.net developers need to enhance the UI by using image border. Though it's a bit easy in Asp.net image controls by using SKIN to manage image borders but it will be a bit difficult to manage HTML image control without CSS. Here in this post i will show you how one can apply CSS in both Asp.net image control and HTML image control. To do that first write the below CSS:











<style type="text/css">
    .imageCSS
 {
    padding: 3px;
    background-color: #FF0000;
 }
    </style>

Asp.Net Image Control:
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" CssClass="imageCSS" ImageUrl="images/blue_flower.jpg" />
    </div>
    </form>
</body>

HTML Image Control:
<body>
    <form id="form1" runat="server">
    <div>
        <img src="~/Images/blue_flower.jpg" id="Image2" class="imageCSS" runat="server" />
    </div>
    </form>
</body>

Hope it will help.

Sunday, July 25, 2010

How to convert ArrayList to Array in Asp.net C# VB.Net




Please visit my new Web Site WWW.Codedisplay.com



In many cases developers have to create ArrayList to achieve dyanamic array functionalities. But sometimes you may need to convert the ArrayList to an array. For example if you are wnat to trnsfer csv data to ORACLE database then if you use bindarray then the conversion is required. Otherwise you won't to insert data into oracle databse. This is only one scenario but for lot of reasons you may need such type of conversion. Here in this article i will show you how you can convert ArrayList to Array.


CLICK HERE to read how to Create Dynamic Array.







C# Code Example:
using System;
using System.Collections;

public partial class Dynamic_Array : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList Dynamic_List = new ArrayList();
       
        for(int i=0; i<5; i++)
            Dynamic_List.Add(i);

        //Convert Here
 int[] array = (int[])Dynamic_List.ToArray(typeof(int));

        for(int i=0;i<=array.GetUpperBound(0);i++)
            Response.Write(array[i].ToString()+"</BR>");
    }
}

VB.NET Code Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Dynamic_List As New ArrayList()
        Dim i As Integer

        For i = 0 To 5
            Dynamic_List.Add(i)
        Next i

        // Convert here
        Dim array() As Integer
        array = DirectCast(Dynamic_List.ToArray(GetType(Integer)), Integer())

        For i = 0 To 5
            Response.Write(array(i).ToString() + "</BR>")
        Next i

    End Sub

Creating Using Dynamic Array's in Asp.Net C# VB.Net




Please visit my new Web Site WWW.Codedisplay.com



In some cases developers need to use dynamic arrays in Asp.net. But it’s not straight forward as like VB. If you were a VB developer then you knew that for dynamic array's VB uses REDIM keyword to generate dynamic array's. Here in this post i will discuss how developer can use dynamic arrays in asp.net pages. Basically there is no straight forward way to use but you can achieve the dynamic size by using a collection named ArrayList. The ArrayList collection provide us to add any type of object (int, float, string, object, collection, user defined datatype) in its index by using ADD() method and by using count method we will know how many object the ArrayList currently hold. So let’s go for an example on dynamic arrays in both C# and VB.Net.


CLICK HERE to read How to convert ArrayList  to Array.




C# Dynamic Array Example:
using System;
using System.Collections;

public partial class Dynamic_Array : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList Dynamic_List = new ArrayList();
       
        for(int i=0; i<5; i++)
            Dynamic_List.Add(i);

        Dynamic_List.Add("You can put Object/any datatype here !!");

        for (int i = 0; i < Dynamic_List.Count; i++)
            Response.Write("Index " + i.ToString() + ": " + Dynamic_List[i].ToString() + "</br>");
    }
}

VB.Net Dynamic Array Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Dynamic_List As New ArrayList()
        Dim i As Integer

        For i = 0 To 5
            Dynamic_List.Add(i)
        Next i

        Dynamic_List.Add("You can put Object/any datatype here !!")

        For i = 0 To Dynamic_List.Count - 1
            Response.Write("Index " + i.ToString() + ": " + Dynamic_List(i).ToString() + "</br>")
        Next i

    End Sub
End Class

Hope now you can use Dynamic array in asp.net C# VB.Net.
Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash