نحوه اتصال یک DropDownList با WebService توسط #C در ASP.Net

یکشنبه 22 شهریور 1394

در این پست خواهیم دید که چگونه در یک سایت ASP یک DropDownList را به یک Web Service متصل کنیم.

نحوه اتصال یک DropDownList با WebService توسط #C در ASP.Net

Web Services: 

در web services ما از WebMethod برای فراخوانی با پارامتر و انجام عملیات استفاده می کنیم. یک web Services از کلاس Sysytem.Web.Services.WebService ارث بری می کند.

شروع با یک Web Services:

برای شروع فرآیند با یک web services یک پروژه جدید ایجاد کرده و یک WebForm اضافه می کنیم. برای اضافه کردن یک web service به قسمت Add NewItem رفته و سپس یک web service با پسوند (asmx.) اضافه می کنیم.

اکنون در web Form یک Html Drop Down List اضافه می کنیم. ما از web service برای اتصال HTML Drop Down List استفاده می کنیم.

<select id="ddlFrom">
</select>

اکنون کد jQuery json  آمده در زیر را باهم بررسی می کنیم.

<script type="text/javascript">
            $(document).ready(function () {
                load_ddlFrom();
            });

            function load_ddlFrom() {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "WebService1.asmx/LoadddlForm",
                    data: "{}",
                    dataType: "json",
                    success: function (Result) {
                        Result = Result.d;
                        $.each(Result, function (key, value) {
                            $("#ddlFrom").append($('<option></option>').val
                            (value.Id).html(value.Stopage));
                        });
                    },
                    error: function (Result) {
                        alert("Error");
                    }
                });
            }
    </script>

url: WebService1.asmx/LoadddlForm :

WebService1.asmx نام web services است که به آن دسترسی دارید. و در آخر LoadddlForm، نام متدی است فراخوانی کرده ایم. بنابراین باید ببینیم که چه چیزی در متد LoadddlForm نوشته شده است.

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

namespace DropDownWebService
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        public class CountryInfo
        {
            public int Id { get; set; }
            public string Stopage { get; set; }
        }
        public List<CountryInfo> CountryInformation { get; set; }

        [WebMethod]
        public List<CountryInfo> LoadddlForm()
        {
            CountryInfo ci = new CountryInfo();
            var CountryInformation = new List<CountryInfo>();
            DataSet ds;
            using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["booking"].ToString()))
            {
                using (SqlCommand cmd = new SqlCommand("select Id,Stopage from tblStopageMeta ", con))
                {
                    con.Open();
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {

                        ds = new DataSet();
                        da.Fill(ds);
                    }
                }
            }
            try
            {
                if (ds != null)
                {
                    if (ds.Tables.Count > 0)
                    {
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                                CountryInformation.Add(new CountryInfo()
                                {
                                    Id = Convert.ToInt32(dr["Id"]),
                                    Stopage = dr["Stopage"].ToString()
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return CountryInformation;
        }
    }
}

اکنون برنامه را اجرا کنید و بررسی کنید که آیا به HTML DropDown List متصل شده است یا نه.

برای استفاده از جزئیات برنامه می توانید آن را از قسمت ضمیمه دانلود کنید.

 

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید