如何使用jQuery绑定asp.net gridview控件?

时间:2012-05-09 09:24:31

标签: jquery asp.net gridview

我必须在jqModal弹出窗口中将asp.net gridview与jquery绑定 我做了一个ajax post调用并获取一个列表。 我需要将此列表绑定到asp.net网格,并成功回调ajax调用。 请在下面找到我正在使用的代码,在bindGrid方法中我需要将列表绑定到asp.net gridview。 请帮忙!

         function pageLoad(sender,args){

         var param = null;
         $(function () {
             var myBlock = $('#myBlock'); // cache jq obj
             $().ajaxStart(function () {
                 $.blockUI(myBlock);
             }).ajaxStop($.unblockUI);
         });

         var showModalPopup = function (container, path, leftMargin, topMargin, triggerElement, postData) {
             // alert('in popup');
             //(container, path, leftMargin, topMargin, triggerElement, postData) {
             ///<summary>This method is used for displaying remote pages in modal popup.</summary>
             ///<param name="container">ID of the div which is displayed as popup.</param>
             ///<param name="path">URL of the page which will be displayed as popup.</param>
             ///<param name="leftMargin">Width of left margin in "%". Used for alignment of popup.</param>
             ///<param name="topMargin">Width of top margin in "%". Used for alignment of popup</param>
             ///<param name="triggerElement">Element which triggered the popup. Used for contexts.</param>
             ///<returns></returns>
             var divBlock = '#' + container;
             if (typeof leftMargin === 'undefined') {
                 leftMargin = '20%';
             }

             if (typeof topMargin === 'undefined') {
                 topMargin = '10%';
             }
             //if the 3rd parameter type is object,we would treat it as postdata  
             if (typeof leftMargin === 'object') {
                 postData = leftMargin;
                 leftMargin = '10%';
             }
             if (typeof triggerElement !== 'undefined') {
                 //Select the id of immediate fieldset of the element which opened popup.
                 //e.g., if "NameSearchButton" is the trigger element, find the fieldset containing this button.
                 var parentFieldsetId = triggerElement.parents('fieldset')[0].id;
                 //Store the fieldset by adding it as custom attribute to divPopup.
                 $(divBlock).attr('parentContext', parentFieldsetId);
             }


             $.blockUI({ message: $(divBlock),
                 css: {
                     padding: 10,
                     margin: 0,
                     left: leftMargin,
                     top: topMargin,
                     width: '',
                     height: '',
                     centerX: true,
                     centerY: true,
                     textAlign: 'center',
                     color: '#000',
                     opacity: '80',
                     border: '3px solid #aaa'
                 },
                 overlayCSS: {
                     backgroundColor: '#000',
                     opacity: 0.7
                 },
                 fadeIn: 0,
                 fadeOut: 0
             });

             if (path !== 'local') {
                 $(document).data('formInstance', false);
                 if (postData) {
                     $.post(path, postData, function (response) {

                         $(divBlock).html(response);
                     });
                 }
                 else {

                     $(divBlock).load(path);
                 }
             }


             $(divBlock).attr('IsPopupDiv', 1);


         };


         var bind = function (success) {
             if (success !== null || success !== undefined) {
                 //Bind to the grid #ContentPlaceHolder1_grdPopUpGrid
             }

         };
         var bindGrid = function () {
             var objBind = '06'; //$('#ContentPlaceHolder1_txtBeginsWithText').val();
             var conStrBind = $('#ContentPlaceHolder1_ConnStrHidden').val();

             if (param == null || param == '' || param == undefined) {

                 drpParamBind = $('#ContentPlaceHolder1_drpSearchBy').val();
             }
             else {
                 drpParamBind = param;
             }
             var webMethodURL = 'testPage.aspx/HitMethod';
             var newobj = { 'obj': objBind, 'conStr': conStrBind, 'drpParam': drpParamBind };
             $.ajax({
                 type: "POST",
                 url: webMethodURL,
                 contentType: "application/json; charset=utf-8",
                 data: JSON.stringify(newobj),
                 dataType: "json",
                 success: function (response) {

                     var success = '';
                     if (response !== undefined) {
                         if (response.d !== null || response.d !== '') {
                             success = response.d;
                             bind(success);



                         }
                     }
                 }
                 //            ,error: function(XHR, errStatus, errorThrown) {
                 //            }
             });
         };
         var con = $('#imgSearchSpouse').parent();
         var test = $('#txtPartnerNo').val();

         $("#ContentPlaceHolder1_imgSearchSpouse").click(function () {
             // alert('hello');

             showModalPopup('ContentPlaceHolder1_Spousepopupdiv', 'local', '13%', '15%');


         });
         $('#ContentPlaceHolder1_btnSearchpopup').click(function () {

             bindGrid();
         });



     }
</script>

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:-1)

我正在使用Jquery Ajax方法处理相同的事情

函数GetDetails(pk){

        var paramList = '{"Foreignkey":"' + pk + '"}';
        alert(paramList);
        $.ajax({
            type: "POST",
            url: "GetData.aspx/getDetails",
            dataType: "json",
            contentType: "application/json",
            processData: false,
            data: paramList,
            success: function (msg) {
                alert('success');
                for (var i = 0; i < msg.d.length; i++)
                {
                    $("#NamesGridView").append("<tr><td>"+ msg.d.id+"</td><td>"+msg.d.GraduatedYear+"</td><td>"+msg.d.SchoolName+"</td></tr>");
                }
                //Show the Dialog
                OpenDialog_Details();
                //closeDialog_Details();
            },
            error: function (msg) {
                alert("error " + msg.responseText);
            }
        });
        return false;
    }

这是webmethod

    [WebMethod]
    public static Details[] getDetails(string Foreignkey)
    {

        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RepeaterDBConnection"].ConnectionString);
        SqlCommand comm = new SqlCommand();
        DataTable dt = new DataTable();
        List<Details> list = new List<Details>();
        try
        {

            comm.Connection = con;
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = "[dbo].[GetDetail]";
            comm.Parameters.Add(new SqlParameter("@PIFK", SqlDbType.Int)).Value = Convert.ToInt32( Foreignkey);
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(dt);

            foreach (DataRow row in dt.Rows)
            {
                Details detobj = new Details();
                detobj.id = Convert.ToInt32( row["id"].ToString());
                detobj.GraduatedYear = row["GraduatedYear"].ToString();
                detobj.SchoolName = row["SchoolName"].ToString();
                list.Add(detobj);
            }
        }
        catch (SqlException Ex)
        {
            //lblResultMessage.Text = Ex.Message;
        }
        return list.ToArray();
    }

这是我的班级 公共课详情     {         private int _id;

    public int id
    {
        get { return _id; }
        set { _id = value; }
    }
    private string _GraduatedYear;

    public string GraduatedYear
    {
        get { return _GraduatedYear; }
        set { _GraduatedYear = value; }
    }
    private string _SchoolName;

    public string SchoolName
    {
        get { return _SchoolName; }
        set { _SchoolName = value; }
    }


}

我希望它能为您提供解决问题的想法

答案 2 :(得分:-1)

public int id     {         得到{return _id; }         设置{_id = value; }     }     私人字符串_GraduatedYear;

public string GraduatedYear
{
    get { return _GraduatedYear; }
    set { _GraduatedYear = value; }
}
private string _SchoolName;

public string SchoolName
{
    get { return _SchoolName; }
    set { _SchoolName = value; }
}

}