[ASP.NET]Repeater 컨트롤 사용하기 IT/Programming

DB로 부터 데이터를 받아서 Repeater 컨트롤을 통해 뿌려주는 방법이다. 
Repeater 컨트롤은 반복되는 형식의 DB를 뿌려줄 때 유용하다. 
사용방법은 다음과 같다. aspx문서의 html부분 내 원하는 곳(ex. div)에
리피터 컨트롤을 넣고 데이터 소스를 지정해 준 다음 템플릿 안에 원하는 내용을 입력하면 된다. 


[main.aspx]

---앞부분 생략---

        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <table>
                        <tr>
                            <td>
                                <h2><a href="#" onClick="markAddress('대학동 241-81')">
                                    <%# Eval("address_dong")%>
                                    <%# Eval("address_zip")%>번지
                                    <%# Eval("address_name")%>
                                </a></h2>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <h3><%# Eval("bldname") %></h3>
                            </td>
                        </tr>
                        <td>
                            <h3>
                            <%# Eval("roomtype")%>
                            <%# Eval("bath")%>
                            <%# Eval("kitchen")%>
                            <%# Eval("laundrymachine")%>
                            </h3>
                        </td>
                        </tr>
                        <tr>
                            <td>
                                보증금: <%# Eval("roomtype")%>만원 / 월세: <%# Eval("roomtype")%>만원
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
        </div>

---뒷부분 생략---

[main.aspx.cs]

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;

public partial class main : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReadData();
        CheckBoxList1.DataSource = GetData();
        CheckBoxList1.DataBind();
    }

    private void ReadData()
    {
        //변수(쿼리문 작성)
        String strSql = "SELECT roominfo.roomid, roominfo.bdid, roominfo.roomspecid, roominfo.roomnum, bldinfo.bdid AS Expr1, bldinfo.address_gu, bldinfo.address_dong, bldinfo.address_zip, bldinfo.address_name, bldinfo.newadd_ro, bldinfo.newadd_zip, bldinfo.bldname, roomspec.roomspecid AS Expr2, roomspec.roomtype, roomspec.area, roomspec.bath, roomspec.kitchen, roomspec.laundrymachine, roomspec.laundryservice, roomspec.floor, roomspec.meal, roomspec.cook, roomspec.window, roomspec.elevator, roomspec.heating, roomspec.cooling, roomspec.bed, roomspec.closet, roomspec.desk, roomspec.internet, roomspec.noise, roomspec.rooftop, roomspec.terrace, roomspec.park FROM roominfo INNER JOIN bldinfo ON roominfo.bdid = bldinfo.bdid INNER JOIN roomspec ON roominfo.roomspecid = roomspec.roomspecid";

        //커넥션 객체
        SqlConnection objCon = new SqlConnection();
        objCon.ConnectionString = "Data Source=plaza3.snu.ac.kr;Initial Catalog=liberta;Persist Security Info=True;User ID=xxxxxx;Password=xxxxxxx";

        objCon.Open();

        //커맨드 객체
        SqlCommand objCmd = new SqlCommand();
        objCmd.Connection = objCon;
        objCmd.CommandText = strSql;
        //”Insert GuestBook Values(‘홍길동’;’g@g.com’,’방명록입니다.’,’1234’,GetDate(),’127.0.0.1’);
        objCmd.CommandType = CommandType.Text;

        SqlDataReader objDr = objCmd.ExecuteReader();

        this.Repeater1.DataSource = objDr;
        this.Repeater1.DataBind();

        //종료
        //objCon.Close();
    }

}

덧글

댓글 입력 영역
◀ 다음덧글    이전덧글 ▶


방문자위치