Usercontrol asp:dropdownlist选择的更改事件没有触发?

时间:2015-08-13 07:14:13

标签: c# jquery asp.net user-controls

我试图在没有占位符的情况下动态绑定用户控件。在我的母页名为Main.aspx& usercontrol名为UC_1.ascx。

我尝试通过下面的ajax调用绑定usercontrol是我的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:下拉列表。 的 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)
        {

        }
    }
}

但是当我选择下拉列表项时。事件没有解雇。它走错了道路。

如何解决这个问题。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

请确保下拉列表的ID是唯一的,因为当它重复时会导致错误而不会触发事件。在UserControl中搜索ddlTest Id。你可以在浏览器中使用firbug或inspect元素来检查它。这可以帮助你做一点事情,确保你的下拉列表是Form标签

 <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>