2015-08-13 98 views
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) 
     { 

     } 
    } 
} 

但是當我選擇下拉列表項。事件沒有開火。它走錯了路。

如何解決這個問題。

有什麼想法?

回答

0

請確保dropdownlist的id是唯一的,因爲它的重複會導致錯誤並且不會觸發事件。在你的UserControl中搜索ddlTest Id。您可以在瀏覽器中使用firbug或inspect元素來檢查。這可以幫助你一點,更確保你的下拉列表是表格標籤

<form> 
<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> 
</form> 
相關問題