Home DropDownList
10.03.2010
Main Menu
Programming
.NET Controls
Operating System
Free Software Downloads
Databases
Articles
Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Yahoo Add to: Technorati Add to: Google
DropDownList


The easiest way to populate drop down list with XML in C# PDF Print E-mail

Populating the drop down list is fairly straigth forward and you can accomplish this task in a few minutes. I have included a sample project which you can download and try it on your computer.

Basically there are 3 steps to populate the drop drop down list:

1. Create a new dataset.
2. Use the dataset to read the XML file
3. Bind the dataset to the drop down list and you're done.

 XML FILE:

<?
xml version="1.0" encoding="utf-8" ?>
<Students>
  <student>
   <StudentID>1</StudentID>
    <StudentName>Mike</StudentName>

  </student>

  <student>

    <StudentID>2</StudentID>

    <StudentName>Bob</StudentName>

  </student>

  <student>

    <StudentID>3</StudentID>

    <StudentName>Ben</StudentName>

  </student>

  <student>

    <StudentID>4</StudentID>

    <StudentName>Julie</StudentName>

  </student>

  <student>

    <StudentID>5</StudentID>

    <StudentName>Oliver</StudentName>

  </student

</Students>

 

  

ASPX FILE:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="LoadDropDownListFromXML._Default" %>

 

<!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>Easist way to load drop down list from XML file</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:DropDownList ID="DropDownList1" runat="server">

        </asp:DropDownList>

    </div>

    </form>

</body>

</html>

 

 

CODE BEHIND FILE:

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

namespace LoadDropDownListFromXML

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            LoadXML();

        }

 

        public void LoadXML()

        {

            string myXMLfile = Server.MapPath("~/StudentXML.xml");

            DataSet dsStudent = new DataSet();

            try

            {

                dsStudent.ReadXml(myXMLfile);

                DropDownList1.DataSource = dsStudent;

                DropDownList1.DataValueField = "StudentID";

                DropDownList1.DataTextField = "StudentName";

                DropDownList1.DataBind();            

            }

            catch (Exception ex)

            {

                Response.Write(ex.ToString());

            }

        }

    }

}

 

Download the sample project


 
Dropdownlist OnSelectedIndexChanged PDF Print E-mail

The OnSelectedIndexChanged occurs and gets fired when the user is selecting a different item in the dropdownlist. You can registered the event on the ASPX page or behind code.

If you are experiencing that the event does not get fire then make sure the AutoPostBack is set to true.

To register it on the ASPX page:

OnSelectedIndexChanged="ddlCourse_SelectedIndexChanged"

To register it from the code behind page, place this code inside the  Page_Load event block:

ddlCourse.SelectedIndexChanged += new EventHandler(ddlCourse_SelectedIndexChanged);

 
HTML File:

<asp:Panel ID="Panel1" runat="server" Visible="false">       

        <asp:DropDownList ID="ddlCourse" runat="server" AutoPostBack="true"

            OnSelectedIndexChanged="ddlCourse_SelectedIndexChanged">

            <asp:ListItem Text="Course 1" Value="Course 1"></asp:ListItem>

            <asp:ListItem Text="Course 2" Value="Course 2"></asp:ListItem>

            <asp:ListItem Text="Course 3" Value="Course 3"></asp:ListItem>

        </asp:DropDownList>       

</asp:Panel>

 

Code File:

 

void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)

{

       throw new NotImplementedException();

}



 
RadComboBox OnSelectedIndexChanged PDF Print E-mail

The OnSelectedIndexChanged occurs and gets fired when the user is selecting a different item in the drop down list. You can registered the event on the ASPX page or behind code.

To register it on the ASPX page:

OnSelectedIndexChanged="ddlSites_SelectedIndexChanged"

To register it from the code behind page, place this code inside the  Page_Load event block:

ddlSites.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(ddlSites_SelectedIndexChanged);


HTML File:

<asp:Panel ID="paSelectSite" runat="server" Visible="false" CssClass="SelectSiteBox" >

        <div class="date"><% =DateTime.Now.ToString("dd MMMM yyyy") %></div>

            <br />

        <h3>Visitor Site Sign In</h3>

        <telerik:RadCodeBlock runat="server">

            <telerik:RadComboBox ID="ddlSites" runat="server"  Width="250px" AutoPostBack="true"

                OnSelectedIndexChanged="ddlSites_SelectedIndexChanged">

            </telerik:RadComboBox>

        </telerik:RadCodeBlock>

    </asp:Panel>

 

Code File:

 

public void ddlSites_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)

 {

            Response.Redirect("defaut.aspx?siteid=" + ddlSites.SelectedValue);

 }



 
How to use FCKeditor in ASP.NET 2.0 PDF Print E-mail

FCKeditor is an open source WYSIWYG text editor that is often use with CMS applications. There are two versions of FCKeditor one is in Java and the other is for the .NET framework. In this tutorial I will show you how to use FCKeditor .Net in ASP.NET 2.0.

The first thing you need to is to download the control from source forge. Make sure you download the latest one.

 

 There are two files that you need to download:

1. FCKEditor
2. FCKeditor.Net

After you have downloaded the two files now unzip the files and have it handy somewhere. You should have a folder fckeditor and a dll file FredCK.FCKeditorV2.dll. The DLL file can be found in the bin -> Release -> 2.0 folder. Obviously the 2.0 folder is for asp.net 2.0 and 1.1 is for asp.net 1.1.

Now create a new web application solution and add the FredCK.FCKeditorV2.dll to your reference folder. Copy the fckeditor folder that you have unzipped previously to your website folder.

fckeditor-folder

After you have done all that yo can begin to use the FCKeditor control. Just place this line of code in your aspx page.

    <FCKeditorV2:FCKeditor  ID="FCKeditor1" BasePath="/fckeditor/" runat="server" Height="400px">

    </FCKeditorV2:FCKeditor>

To get the value from the FCKeditor in the code behind file use FCKeditor1.Value property.

Example Code:

ASPX Page:

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test._Default" %>

    2 <%@ Register assembly="FredCK.FCKeditorV2" namespace="FredCK.FCKeditorV2" tagprefix="FCKeditorV2" %>

    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    4 

    5 <html xmlns="http://www.w3.org/1999/xhtml" >

    6 <head runat="server">

    7     <title>Untitled Page</title>

    8 </head>

    9 <body>

   10     <form id="form1" runat="server">

   11     <div>       

   12         <asp:Label ID="Label1" runat="server" Text="Page Title"></asp:Label>       

   13     </div>

   14     <br />

   15     <FCKeditorV2:FCKeditor  ID="FCKeditor1" BasePath="/fckeditor/" runat="server" Height="400px">

   16     </FCKeditorV2:FCKeditor>

   17     <br />

   18     <asp:Button ID="btnSave" runat="server" onclick="Button1_Click" Text="Save"

   19         Width="71px" />

   20     </form>

   21 </body>

   22 </html>

 

 

Class File:


 

    4 using System.Data;

    5 using System.Linq;

    6 using System.Web;

    7 using System.Web.Security;

    8 using System.Web.UI;

    9 using System.Web.UI.HtmlControls;

   10 using System.Web.UI.WebControls;

   11 using System.Web.UI.WebControls.WebParts;

   12 using System.Xml.Linq;

   13 using System.Data.SqlClient;

   14 using System.Data.Sql;

   15 

   16 namespace Test

   17 {

   18     public partial class _Default : System.Web.UI.Page

   19     {

   20         protected void Page_Load(object sender, EventArgs e)

   21         {

   22 

   23         }

   24 

   25         protected void Button1_Click(object sender, EventArgs e)

   26         {

   27             string connStr = "Data Source=compserver;Initial Catalog=sample;User Id=sample;Password=sample";

   28             SqlConnection conn = null;

   29 

   30             try

   31             {               

   32                 string strInsert = "INSERT INTO Content(Page_Content) VALUES('{0}')";

   33 

   34                 conn = new SqlConnection(connStr);

   35                 conn.Open();

   36 

   37                 strInsert = string.Format(strInsert, FCKeditor1.Value);

   38 

   39                 SqlCommand cmd = new SqlCommand(strInsert, conn);

   40                 cmd.CommandType = CommandType.Text;

   41                 cmd.ExecuteNonQuery();               

   42             }

   43             catch (Exception ex)

   44             {

   45             }

   46             finally

   47             {

   48                 conn.Close();

   49             }

   50         }      

   51     }

   52 }

Now try to execute your code and see if it works.


 


 

 


 



 
OnDataBound Event PDF Print E-mail

The OnDataBound event is raised when the databinding logic has been completed, at this point in time you can then perform any addtional tasks before the page finishes loading.

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebSample.Default" %>

    2 

    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    4 <html xmlns="http://www.w3.org/1999/xhtml">

    5 <head runat="server">

    6     <title>Untitled Page</title>

    7 </head>

    8 <body>

    9     <form id="form1" runat="server">

   10     <div>

   11 

   12         <script runat="server">

   13             protected void ddlStudent_DataBound(object sender, EventArgs e)

   14             {

   15                 //this.ddd

   16                 ddlStudent.Items.Add("Last Item");

   17                 ddlStudent.SelectedIndex = 0;

   18             }

   19         </script>

   20 

   21         <h2>

   22             Events</h2>

   23         <div>

   24             DataBound Event</div>

   25         <div>

   26             <asp:DropDownList ID="ddlStudent" runat="server" OnDataBound="ddlStudent_DataBound"

   27                 DataSourceID="SqlDataSourceTI" DataTextField="TI" DataValueField="TI" AutoPostBack="true">

   28                 <asp:ListItem>List Item 1</asp:ListItem>

   29                 <asp:ListItem>List Item 2</asp:ListItem>

   30                 <asp:ListItem>List Item 4</asp:ListItem>

   31             </asp:DropDownList>

   32             <asp:SqlDataSource ID="SqlDataSourceTI" runat="server" ConnectionString="<%$ ConnectionStrings:PI_RTCF_ReportingConn %>"

   33                 SelectCommand="usp_SelectTIAll" SelectCommandType="StoredProcedure"></asp:SqlDataSource>

   34         </div>

   35     </div>

   36     </form>

   37 </body>

   38 </html>



 



Online Paid Survey