简体   繁体   中英

Usercontrol asp:dropdownlist selected change event not firing?

I tried to bind user control dynamically without placeholder. In my parent page called Main.aspx & usercontrol called UC_1.ascx.

I tried to bind usercontrol via ajax call below is my aspx page codes.:

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();
            }
        }
    }
}

In My Usercontrol I have one 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)
        {

        }
    }
}

but when i am choose dropdownlist items. event not firing. It goes some wrong path.

How to solve this.

Any idea?

Please make sure that the id for dropdownlist is unique because when its duplicate it causes error and not firing event. Search for ddlTest Id in your UserControl. You can check this out by using firbug or inspect element in browser. May this helps you a little and one thing more make sure your dropdownlist is Form tag

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM