==> [FrmDataRow.aspx] 소스 및 디자인

  

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmDataRow.aspx.cs" Inherits="FrmDataRow" %>

 

<!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></title>

</head>

<body>

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

    <div>

   

        카테고리 리스트 출력<br />

        <br />

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

        </asp:DropDownList>

   

    </div>

    </form>

</body>

</html>

 

 

 

 


-------------------------------------------------------------------------------------

 


==> [FrmDataRow.aspx.cs]

 


using System;

using System.Data.SqlClient;

using System.Configuration;

using System.Data;

using System.Web.UI.WebControls;

 

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection(

            ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        con.Open();

 

        SqlCommand cmd = new SqlCommand("Select * From Categories", con);

        cmd.CommandType = CommandType.Text;

 

        SqlDataAdapter da = new SqlDataAdapter(cmd);

 

        DataSet ds = new DataSet();

 

        da.Fill(ds, "Categories");

 

        #region 직접바인딩

        ////[!] 드롭다운리스트에 바인딩

        //this.lstCategoryList.DataSource = ds;

        //this.lstCategoryList.DataTextField = "CategoryName";

        //this.lstCategoryList.DataValueField = "CategoryID";

        //this.lstCategoryList.DataBind();

        #endregion

 

        // List<T>와 같은 방법으로 출력

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

        {           

            DataRow dr = ds.Tables[0].Rows[i]; // 한개 레코드를 DataRow에 담기

            // Text : 카테고리명, Value : 카테고리ID로 출력

            lstCategoryList.Items.Add(

                new ListItem(dr["CategoryName"].ToString(), dr["CategoryID"].ToString()));

        }

 

        con.Close();

    }

}

 

 

 

 

// DataRow 클래스 - DataTable의 데이터 행을 나타냅니다.

 

 


 

-------------------------------------------------------------------------------------


 

[실행결과]


--> "DataRow"클래스와 "드롭다운리스트"를 사용하여 웹페이지에 출력하였다.


 


Posted by holland14
: