用户控制asp:下拉列表选择更改事件不触发

本文关键字:事件 选择 控制 asp 下拉列表 用户 | 更新日期: 2023-09-27 18:07:47

我尝试在没有占位符的情况下动态绑定用户控件。在我的主页面中。aspx和usercontrol call 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();
            }
        }
    }
}

在我的用户控制我有一个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)
        {
        }
    }
}

,但当我选择下拉列表项。事件未触发。它走错了路。

如何解决这个问题

任何想法?

用户控制asp:下拉列表选择更改事件不触发

请确保下拉列表的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>