我有页面Webform1.aspx和一个GrvData.ascx用户控件。我在webfom1.aspx页面中动态显示usercontrol ...我想访问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 ...
答案 0 :(得分:0)
如果从服务器返回HTML内容,请从jQuery ajax调用中删除以下内容
contentType: "application/json; charset=utf-8",
dataType: "json",
因为它请求JSON作为返回类型