2011. 7. 13. 18:02

데이터 바인딩

데이터를 담고 있는 객체와 서버 컨트롤을 연결하는 것을 말합니다. 데이터 바인딩은 주로 리스트 컨트롤에서 사용되지만, 하나의 값에 대해서도 데이터 바인딩을 할 수 있습니다.  데이터 바인딩을 리스트 컨트롤을 이용한 예를 알아보겠습니다.

데이터 바인딩(Data Binding)이란 데이터 값과 컨트롤을 연결하는 것을 말합니다. 예를 들면, Array 객체와 리스트 컨트롤을 열결하는 것입니다. 리스트 컨트롤을 Array 객체에 있는 값을 보여줍니다.



예제 1)   

     <table>
            <tr>
                <td style="width: 100px">
                    <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox></td>
                <td style="width: 100px">
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    </asp:DropDownList></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    <asp:RadioButtonList ID="RadioButtonList1" runat="server" Width="176px">
                    </asp:RadioButtonList></td>
                <td style="width: 100px">
                    <asp:CheckBoxList ID="CheckBoxList1" runat="server" Width="156px">
                    </asp:CheckBoxList></td>
            </tr>
        </table>

예제 2)

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] arr = { "1. ASP", "2. C/C++/C#", "3. VB.NET" };

            ListBox1.DataSource = arr;
            ListBox1.DataBind();

            DropDownList1.DataSource = arr;
            DropDownList1.DataBind();

            RadioButtonList1.DataSource = arr;
            RadioButtonList1.DataBind();

            CheckBoxList1.DataSource = arr;
            CheckBoxList1.DataBind();
        }
    }



* 예외처리

예제3)
string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db.mdb";

OleDbConnection conn = new OleDbConnection(Source);

string sql = "Select * from Member";
OleDbCommand = new OleDbCommand(sql.conn);

OleDbDataReader readre = null;

try
{
conn.Open();
reader.cmd.ExecuteReader();  //SQL 실행
}
catch
{
Response.Write("데이터베이스 실행과정에서 오류 발생.");
}
if (reader !=null) reader.Close();
conn.Close();

     예제4
ArrayList list = new ArrayList();

list.Add("A");
list.Add("B");
list.Add("C");

ListBox1.DataSource = list;
ListBox1.DataBind();