1

我有页面 Webform1.aspx 和一个 GrvData.ascx 用户控件。我在 webfom1.aspx 页面中动态显示用户控件...我想访问位于 GrvData.ascx 中的 webform1.aspx.cs 中的 grvitem。完整的设计和代码如下..

webform1.aspx

<form runat="server">
<input type="button" value="Load" runat="server" />
<input type="button" value="Submit" runat="server" />
<div id="result"></div>
</form>
<script>
$(document).ready(function () {  
// on load button click jquery      
        $.ajax({
            type: "POST",
            url: "Webform1.aspx/Result",
            data: "{ ClientIDD : '" + clientidd + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {               
                $('#result').html(response.d);
            },
            error: function (msg) {               
                alert(msg);               
            }       
    });
}); 
</script>

后面的代码

Webform1.aspx.cs

  [WebMethod]
    public static string Result(string ClientIDD)
    {
        return Results(ClientIDD);
    }

    public static string Results(string ClientIDD)
    {
        try
        {
            Page page = new Page();
            System.Web.UI.UserControl userControl = (System.Web.UI.UserControl)page.LoadControl("GrvData.ascx");

            Type t = userControl.GetType();
            MethodInfo addtext = t.GetMethod("BindMyGrid");
            addtext.Invoke(userControl, new object[] { ClientIDD });
           // ControlHub.Controls.Add(userControl);

            userControl.EnableViewState = true;
            HtmlForm form = new HtmlForm();
            form.Controls.Add(userControl);
            page.Controls.Add(form);

            StringWriter textWriter = new StringWriter();                
            HttpContext.Current.Server.Execute(page, textWriter,false);              
            return textWriter.ToString();

        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

GrvData.ascx

<asp:gridview id="grvitem" runat="server" 
        width="100%" autogeneratecolumns="False"
        datakeynames="ID" EmptyDataText="No records found" >          
            <Columns>               
                <asp:TemplateField >
                <ItemTemplate >
                    <input type="checkbox" id="chk" value="getIndex" onclick="chng(<%# Eval("Amount") %>,this);" />                
                </ItemTemplate>
            </asp:TemplateField>               
                <asp:BoundField DataField="ServiceName" HeaderText="Description" />                               
                <asp:BoundField HeaderText="Rate" DataField="Rate" />
                <asp:BoundField HeaderText="QTY" DataField="QTY" />               
                 <asp:TemplateField HeaderText="Price">
    <ItemTemplate>
        <asp:Label ID="lblListPrice" runat="server" Text='<%#Eval("Amount")%>'/>
    </ItemTemplate>
    </asp:TemplateField> 
            </Columns>             
        </asp:gridview>

代码背后

public void BindMyGrid(string clientid)
        {      
            // bind the data to the grid
            grvitem.DataSource = data;
            grvitem.DataBind();
        }

请告诉我如何访问 webform1.aspx.cs 中的 grvitem...

4

1 回答 1

0

如果您从服务器返回 HTML 内容,请从您的 jQuery ajax 调用中删除以下内容

contentType: "application/json; charset=utf-8",
dataType: "json",

因为它请求 JSON 作为返回类型

于 2012-10-23T11:47:22.003 回答