<script type="text/javascript">
            //master: id of div element that contains the information about master data
            //details: id of div element wrapping the details grid
            //customerId: id of the customer to be send as parameter to web method
            function showhide(master,detail,customerId)
            { 
                //First child of master div is the image
                var src = $(master).children()[0.src;
                //Switch image from (+) to (-) or vice versa.
                if(src.endsWith("plus.png"))
                    src = src.replace('plus.png','minus.png');
                else
                    src = src.replace('minus.png','plus.png');
                //if the detail DIV is empty Initiate AJAX Call, if not that means DIV already populated with data             
                if($(detail).html() == "")
                {
                    //Prepare Progress Image
                    var $offset = $(master).offset();
                    $('#progress').css('visibility','visible');
                    $('#progress').css('top',$offset.top);
                    $('#progress').css('left',$offset.left+$(master).width());                    
                    //Prepare Parameters
                    var params = '{customerId:"'+ customerId +'"}';                    
                    //Issue AJAX Call
                    $.ajax({
                            type: "POST", //POST
                            url: "GridViewDrillDownjQueryAjax.aspx/GetOrders", //Set call to Page Method
                            data: params, // Set Method Params
                            beforeSend: function(xhr) {
                                xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");},
                            contentType: "application/json; charset=utf-8", //Set Content-Type
                            dataType: "json", // Set return Data Type
                            success: function(msg, status) {
                                $('#progress').css('visibility','hidden');
                                $(master).children()[0].src = src;
                                $(detail).html(msg);
                                $(detail).slideToggle("normal"); // Succes Callback
                                },
                            error: function(xhr,msg,e){
                                alert(msg);//Error Callback
                                }
                            });
                }
                else
                {
                    //Toggle expand/collapse                   
                    $(detail).slideToggle("normal");
                    $(master).children()[0].src = src;
                }
            }
        </script>]上面是js代码。
下面是aspx的后台代码
public static string GetOrders(string customerId)
    {
        System.Threading.Thread.Sleep(500);
        Page page = new Page();
        CustomerOrders ctl = (CustomerOrders)page.LoadControl("CustomerOrders.ascx");
        ctl.CustomerId = customerId;
        page.Controls.Add(ctl);
        System.IO.StringWriter writer = new System.IO.StringWriter();
        HttpContext.Current.Server.Execute(page, writer, false);
        string output = writer.ToString();
        writer.Close();
        return output;
  
public class CustomerOrders : UserControl
{    public string CustomerId
    {
        get { return (string)ViewState["CustomerId"]; }
        set {ViewState["CustomerId"] = value;}
    }
    }为什么我在ASCX里面加入textbox或者button这类的控件的时候,ajax那里运行就会提示出错。是因为JSON格式不能转换,导致不能输出数据吗? 该如何修改才能在ascx中也加入textbox。 现在ascx中就只有一个repeater。困扰了好多天了,哪位大神来打救下我这个超级菜鸟吧!!!!

解决方案 »

  1.   

    本帖最后由 net_lover 于 2011-03-02 14:49:50 编辑
      

  2.   

    格式不对啊,ascx需要注意文件路径
      

  3.   


    还不够详细吗?客户端引用服务器控件,需要使用$('#<%=TextBox1.ClientID%>')
      

  4.   

    就是说服务器控件传送到客户端的浏览器后生成html代码后的id不一定是你在aspx里面的id.需要使用$('#<%=TextBox1.ClientID%>')去获取它的真ID。这个你可以通过查看生成后页面的源代码就知道了。