0
我試圖無約束地動態綁定用戶控件。在我的父頁面調用Main.aspx & usercontrol調用UC_1.ascx。Usercontrol asp:dropdownlist選擇的更改事件不會觸發?
我試圖通過AJAX調用來綁定用戶控件下面是我的aspx頁面代碼:
Main.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function callUC() {
//alert("call loadUsercontrol method....");
var userControl = "CrossTab.ascx";
var selectimg = $('#divCrossTab');
$.ajax({
type: "POST",
url: "Main.aspx/LoadUserControl",
data: "{message: '" + userControl + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
alert("success");
$(selectimg).append(r.d);
},
error: function (r) {
var ss = r;
alert("fail");
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="divCrossTab" runat="server"></div>
<div>
<a onclick="callUC();">click to call usercontrol</a>
<asp:PlaceHolder ID="placeHolderTest" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Main.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Services;
namespace div_PopUp_issue.UserControls
{
public partial class Main : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Page.Controls.Add(Page.LoadControl(@"~/UserControls/CrossTab.ascx"));
}
[WebMethod]
public static string LoadUserControl(string message)
{
using (Page page = new Page())
{
System.Web.UI.UserControl userControl = null;
string path = @"~/UserControls/CrossTab.ascx";
userControl = (System.Web.UI.UserControl)page.LoadControl(path);
// dynamically adding a script manager
var sm = new System.Web.UI.ScriptManager();
//var sm1 = new Telerik.Web.UI.RadScriptManager();
HtmlForm form = new HtmlForm();
form.Controls.Add(sm);
//form.Controls.Add(sm1);
form.Controls.Clear();
form.Controls.Add(userControl);
page.Controls.Add(form);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
}
}
}
在我的UserControl中我有一個asp:dropdownlist。 UC_1.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CrossTab.ascx.cs" Inherits="div_PopUp_issue.UserControls.CrossTab" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
<asp:ListItem Selected="True" Text="a" Value="a"></asp:ListItem>
<asp:ListItem Text="b" Value="b"></asp:ListItem>
<asp:ListItem Text="c" Value="c"></asp:ListItem>
</asp:DropDownList>
UC_1.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace div_PopUp_issue.UserControls
{
public partial class CrossTab : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
但是當我選擇下拉列表項。事件沒有開火。它走錯了路。
如何解決這個問題。
有什麼想法?