c# – Event in User Control – Education Career Blog

I have a problem with button event in web user control

First i have an aspx like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddTest.aspx.cs" Inherits="AddTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div id="divContent" runat="server">
    </div>
    </form>    
</body>
</html>

As you see i have a ScriptManager to do with ASP.NET AJAX, a div with ID divContent to load controls, then in code behind:

protected void Page_Load(object sender, EventArgs e)
{
    divContent.Controls.Add(LoadControl("~/thitai/addtest.ascx"));
}

I add addtest.ascx to divContent as you see, and this is the code of addtest.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="addtest.ascx.cs" Inherits="thitai.addtest" %>
<asp:UpdatePanel ID="updatePanel" runat="server" ClientIDMode="Static">
    <ContentTemplate>
        <input type="button" value="Load text" onclick="updPanel('text');return false;" />
        <input type="button" value="Load image" onclick="updPanel('image');return false;" />
        <asp:HiddenField ID="hftype" runat="server" ClientIDMode="Static" />
        <div>
            <asp:PlaceHolder ID="plhControl" runat="server"></asp:PlaceHolder>
        </div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
    function updPanel(type) {
        $("#hftype").val(type);
        __doPostBack("updatePanel", "");
    }
</script>

Each time i click on Load text button, i change the value of hfType (hidden field) then do postback to server using UpdatePanel, then this is code behind

protected override void OnPreRender(EventArgs e)
{
     string type = hftype.Value;
     switch (type)
     {
                case "text":
                    plhControl.Controls.Add(LoadControl("~/thitai/test/text.ascx"));
                    break;
                case "image":
                    plhControl.Controls.Add(LoadControl("~/thitai/test/image.ascx"));
                    break;
                default:
                    plhControl.Controls.Add(LoadControl("~/thitai/test/text.ascx"));
                    break;
     }
     base.OnPreRender(e);
}

This is the code of text.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="text.ascx.cs" Inherits="thitai.test.text" %>
<%@ Register Assembly="Support" Namespace="Support.CustomEditor" TagPrefix="Editor" %>
<Editor:CustomEditor ID="edNoidung" runat="server" NoScript="True" Content=""
    Height="500" ActiveMode="Design" NoUnicode="True" InitialCleanUp="False" />
<div>
    <asp:Button ID="Button1" runat="server" Text="Show HTML" 
        onclick="Button1_Click" />
</div>
<div id="showHtml" runat="server"></div>

And code behind of text.ascx

protected void Button1_Click(object sender, EventArgs e)
{
    showHtml.InnerHtml = edNoidung.Content;
}

The problem is: when i click to button1, the page do a postback using AJAX but event Button1_Click DOES NOT FIRE!

Any idea for this?

,

Control events would always take place before Page/Control PreRender. However, Button1 does not exists till Page Prerender happens (because you are loading the control at that time).
To solve the issue, you can move loading of the control (text.ascx) in Page Load instead of prerender.

EDIT: alternately, you can sniff request data in text.ascx page_load to check if button has been clicked. For example,

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.FromButton1.UniqueID != null)
    {
       // button1 click, handle it
       ...
    }
}

Leave a Comment