1. 데이터가 들어갈 WebService 프로젝트를 만듭니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.ComponentModel;
using System.Collections;

namespace WebService
{   
[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]    public class WebService1 : System.Web.Services.WebService
    {
        [WebMethod]
        public List<Student> GetCompany()
        {
            SqlConnection conn = new SqlConnection("server=[서버];database=[DB명];uid=[ID];pwd=[PWD]");

            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * from Student";

            SqlDataAdapter ad = new SqlDataAdapter();
            ad.SelectCommand = cmd;
            DataSet ds = new DataSet();
            ad.Fill(ds);

            List<Student> studentlist;
            studentlist = new List<Student>();

            foreach (DataRowView rw in ds.Tables[0].Rows)
            {
                Student std = new Student();
                std.name = rw["Name"].ToString();
                std.age=rw["Age"].ToString();

                studentlist.Add(std);
            }
            return studentlist;
        }
    }

        public class Student
        {
            public string name {get; set;}
            public string age {get; set;}
        }
}



2. Silverlight 에 바인딩


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Collections.ObjectModel;

namespace Demo
{
    public MainPage()
        {
            InitializeComponent();
            studentList = new List<Student>();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {

            StudentListService.CompanyListSoapClient com = new StudentListService.StudentListSoapClient();

            com.GetStudentCompleted += new EventHandler<StudentListService.GetStudentCompletedEventArgs>(com_GeStudentCompleted);
            com.GetStudentAsync();
        }

        void com_GetStudentCompleted(object sender, StudentListService.GetStudentCompletedEventArgs e)
        {
               // 바인딩
        }
}

 

'Silverlight > Tip' 카테고리의 다른 글

DataGrid 해당 Column Sorting 기능 넣기  (0) 2010.10.12
[Silverlight 4.0] Print 기능 구현하기  (0) 2010.08.07
Silverlight Tip Site  (0) 2010.07.28
네이버 MS Silverlight 카페 Shorty 님이 쓰신 자료

#1. .NET RIA Service 의 정의 / 선수조건 - http://shorty.tistory.com/33

#2. 디폴트 프로젝트의 설명 / 기본 듀토리얼 - http://shorty.tistory.com/34

#3. 클라이언트 코드의 이해 - http://shorty.tistory.com/35

#4. 엔티티 쿼리 - http://shorty.tistory.com/36

#5. 엔티티 수정법 - http://shorty.tistory.com/39

#6. 커스텀 메서드와 서비스 오퍼레이션 - http://shorty.tistory.com/40

#7. 구성과 전체 구조의 이해 - http://shorty.tistory.com/entry/43

#8 메타데이터 클래스를 쓰자 - http://shorty.tistory.com/entry/45

Silverlight 4 개발환경 구축 순서

  1. Silverlight 4 Runtime 설치
    http://go.microsoft.com/fwlink/?LinkID=149156
  2. Silverlight 4 Tools for Visual Studio 2010 설치
    http://go.microsoft.com/fwlink/?LinkID=177428
  3. Silverlgiht 4 Toolkit 설치
    http://silverlight.codeplex.com/


// Set parent window’s hidden field value from child window
window.opener.document.getElementById(Client ID of Hidden Field).value = Selected IDs;

// raise button click event of parent window
window.opener.document.getElementById(Client ID Of Button).click();

// Close the child window
close();

Fig – (1) Javascript on child window to raise button click event on parent window

I had written this script on child page. Tis will raise button click event and in post back I wrote the logic of retrieving data from database for IDs set in hidden field and displayed them in grid.

Now the second way, call javascript function of parent window from child window.

<script language="Javascript" type="text/javascript">
    function CallAlert()
    {
        alert("This is parent window's alert function.");
    }
</script>

Fig – (2) Javascript on parent window.

<script language="Javascript" type="text/javascript">

    function SetParentWindowsHiddenFieldValue()
    {
        window.opener.document.getElementById("HiddenField1").value =
                            document.getElementById("TextBox1").value;
        return false;
    } 

    function CallParentWindowFunction()
    {
        window.opener.CallAlert();
        return false;
    }
</script>

Fig – (3) Javascript on child window


 Web.Config

<connectionStrings>
 <add name="MyDB" connectionString="Data Source=[서버];Initial Catalog=[DB명];
UserID=[아이디];Password=[비밀번호]" providerName="System.Data.SqlClient"/>
</connectionStrings>

Code
SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString);

SqlDataReader reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);

<!-- 새로고침방지 -->

<script language="JavaScript">
document.onkeydown = function() {
if (
( event.ctrlKey == true && ( event.keyCode == 82 ) ) ||
( event.keyCode == 116 )) {
event.keyCode = 505;
}
if (event.keyCode == 505) {
    location.reload();
return false;
}
}
</script>
<!-- 새로고침방지 -->

+ Recent posts