We help IT Professionals succeed at work.

I have a custom control with a button. The button click event fires if the page is without master. But, after the master is added, the event does not fire

tnova1
tnova1 asked
on
603 Views
Last Modified: 2013-11-07
To all,

Have anyone run into the problem that custom event won't fire:

I have a custom control with a button. The button click event fires if the page is without master. But, after the master is added, the event does not fire.

The custom tag is defined in caller.aspx as:

<cc1:popupcontrol id="ccPopTEC" runat="server" ButtonText="Add TEC" ButtonWidth="60px"             PopupHeightPx="340" PopupWidthPx="320" TextBoxWidth="0px" TargetURL="ECPTecAdd.aspx"
AutoPostBack="True"></cc1:popupcontrol>

The event in the caller.aspx.cs is:
      void ccPopTEC_Changed(object sender, EventArgs e)
      {
         if (ccPopTEC.Text.Length > 0)
         {
                  //process the custom textbox value
         }
      }

The callee(TargetURL)  ECPTecAdd.aspx just bring up a list defined with <select></select> html tag, and
set the value for the custom textbox as
         PopupControl.ReturnValue(str.ToString(), "");   // a public function of the custom control.

I worked perfect w/o the master page.

I put a break point insid the function eccPopTEC_Changed(object sender, EventArgs e), but the debugger won't kick in after I added the master page.

Please help!
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What is 'popupcontrol'?  How does the event normally get fired (without the master)?

Bob

Author

Commented:
The popupcontrol has a text box and a submit button, once click on the button it will bring up the webpage specified in TargetURL="ECPTecAdd.aspx" .  The page ECPTecAdd.aspx ran fine and place the value in the return value PopupControl.ReturnValue(str.ToString(), "");

However the caller page which contains the popupcontrol autopostback event handler as:
      // Custom TEC popup change event handler.  Add Tec into database and refresh list box lbECPTec of TECs.
      void ccPopTEC_Changed(object sender, EventArgs e)
      {
         if (ccPopTEC.Text.Length > 0)
         {
            ECPData.AddTec(ccPopTEC.Text, Session["sesEcpID"].ToString(), Response.Cookies["username"].Value, this.oDB);
         }
      }
It didn't fire at all and the selected TEC cann't be saved in the SQL database.  The caller has a master page.  Take off the master page, autopostback event worked fine.

I has search on Google, I saw one guy has the same problem like me, however he never mention how did he solve it.

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Does the control get rendered with __doPostBack?  How is the event handler declared?

Bob

Author

Commented:
I did not make it clear, the return value is posted in the popupcontrol's text box, then inside the event handler ccPopTEC_Changed will process the content value if (ccPopTEC.Text.Length > 0).  But I had a break point on this line, the debugger never kick in on the first line of the procedure.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you have a Changed event, was posts the page back?

Bob

Author

Commented:
Here is the html source (view source from IE)
<table style="TABLE-LAYOUT: fixed; DISPLAY: inline; VERTICAL-ALIGN: top; width: 60px; height: 20px"cellpadding="0" cellspacing="0">
           <tr><td><input type="text" id="_ctl0_NatecContentPlaceHolder_ccPopTEC-field" name="_ctl0_NatecContentPlaceHolder_ccPopTEC"  style="width: 0px; height: 20px" value="" onchange="__doPostBack('_ctl0$NatecContentPlaceHolder$ccPopTEC','_ctl0_NatecContentPlaceHolder_ccPopTEC-field')" readonly="readonly"></input></td>

The _doPostBack was defined in the render(HtmlTextWriter output) as:
          string Onchange = "onchange=\"" + Page.ClientScript.GetPostBackEventReference(this, FieldID()) + "\"";
         output.Write(String.Format(
             "<table style=\"TABLE-LAYOUT: fixed; DISPLAY: inline; VERTICAL-ALIGN: top{0}\"" +
             "cellpadding=\"0\" cellspacing=\"0\">\n" +
             "<tr><td><input type=\"text\" id=\"{1}\" name=\"{2}\" {9} style=\"width: {14}{10}\" value=\"{3}\" {11}{12}{13}></input></td>\n" +
             "<td width=\"{4}\">{5}</td></tr></table>\n" +
             "<input type=\"hidden\" id =\"{6}\" name=\"{7}\" value=\"{8}\"></input>",
             SizeString, FieldID(), ClientID, EscapeString(Text), m_ButtonWidth, ButtonString,
             FieldID() + "-value", ClientID + "-value", EscapeString(Value), classStr, HeightString, Onchange, EnableStr, ReadOnlyStr, m_TextBoxWidth));

Event handler is declared as:
      public delegate void ChangedEventHandler(object sender, EventArgs e);

      public event ChangedEventHandler Changed;

      protected virtual void OnChange(EventArgs e)
      {
         Changed(this, e);
      }

      public bool LoadPostData(string PostDataKey, System.Collections.Specialized.NameValueCollection Values)
      {
                     ....
      }

      public void RaisePostDataChangedEvent()
      {
         OnChange(EventArgs.Empty);
      }


CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I don't see anything wrong there.  It is starting to sound like a bug.  Did you install 2005 SP1?

Bob

Author

Commented:
Yes, I have the changed event, the target page get called and brought up for user to choose data, and post back data but the postback event ccPopTEC_Changed(object sender, EventArgs e) didnot fired with master page on.

Author

Commented:
Yes, I installed 2005 SP1.  Could you tell me the link to  SP1, may be I have different version?

View the html source code from browser
<table style="TABLE-LAYOUT: fixed; DISPLAY: inline; VERTICAL-ALIGN: top; width: 60px; height: 20px"cellpadding="0" cellspacing="0">
<tr><td><input type="text" id="_ctl0_NatecContentPlaceHolder_ccPopTEC-field" name="_ctl0_NatecContentPlaceHolder_ccPopTEC"  style="width: 0px; height: 20px" value="" onchange="__doPostBack('_ctl0$NatecContentPlaceHolder$ccPopTEC','_ctl0_NatecContentPlaceHolder_ccPopTEC-field')" readonly="readonly"></input></td>
<td width="60px"><input type="button"  class = 'buttons' style="vertical-align: top; width: 60px; height: 20px" value="Add TEC" onclick="showPopup('_ctl0_NatecContentPlaceHolder_ccPopTEC-field', '_ctl0_NatecContentPlaceHolder_ccPopTEC-field-value', 'ECPTecAdd.aspx', 340, 320, '2px solid #d0d0f0','2px solid #7070a0')"/></td></tr></table>

I wonder that the prefix _ctl0$NatecContentPlaceHolder$ vs the '_ctl0_NatecContentPlaceHolder_ causing problem?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
The custom control is defined as:
<cc1:popupcontrol id="ccPopTEC" runat="server" Width="60px" Height=20px ButtonText="Add TEC" ButtonWidth="60px"  PopupHeightPx="340" PopupWidthPx="320" TextBoxWidth="0px" TargetURL="ECPTecAdd.aspx" ButtonCssClass="buttons"                             AutoPostBack="True"></cc1:popupcontrol>

Merging the master page and the page together .NET framework appending these prefixed values.  
W/o masterpage there is no prefixes and no problem.

Author

Commented:
Change the line:
       string Onchange = "onchange=\"" + Page.ClientScript.GetPostBackEventReference(this, FieldID()) + "\"";
where as FieldID() = ClientID + "-field"

to:
        string Onchange = "onchange=\"__doPostBack('" + this.ID + "','" + FieldID() + "')\"";
whereas FieldID()=this.ID + "-field"

still not work.

I created 2 test pages to test this:

1. default.aspx  has master page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="test._Default" MasterPageFile="~/Site1.Master" %>

<%@ Register Assembly="PopupControl" Namespace="PopupControl" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID=ContentPlaceHolder1 runat="server">

    <div>
    <table class=srch_tbl>
      <TBODY>
         <tr>
            <td>
               <cc1:PopupControl id="PopupControl1" runat="server"  ButtonText="Add Publication" ButtonWidth="120px"
                  PopupHeightPx="125" PopupWidthPx="470" TextBoxWidth="10px" TargetURL="test.aspx" AutoPostBack="True" ForeColor="White">
               </cc1:PopupControl>
            </td>
         </tr>
         <tr>
            <td>        
               <asp:Literal ID=literal1 runat=server></asp:Literal>
            </td>
         </tr>
      </TBODY>
    </table>
    </div>
</asp:Content>

The code behind page is:

   public partial class _Default : System.Web.UI.Page
   {

      #region Web Form Designer generated code
      override protected void OnInit(EventArgs e)
      {
         //
         // CODEGEN: This call is required by the ASP.NET Web Form Designer.
         //
         InitializeComponent();
         base.OnInit(e);
      }

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.PopupControl1.Changed += new PopupControl.PopupControl.ChangedEventHandler(this.PopUpChange);
      }

      #endregion

      protected void Page_Load(object sender, EventArgs e)
      {
      }

      protected void PopUpChange(object sender, EventArgs e)
      {
         literal1.Text = "U have selected " + this.PopupControl1.Text;
      }
   }

The PopUpChange never get fired.

2.  The NoMasterPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NoMasterPage.aspx.cs" Inherits="test.NoMasterPage" %>
<%@ Register Assembly="PopupControl" Namespace="PopupControl" TagPrefix="cc1" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table class=srch_tbl>
      <TBODY>
         <tr>
            <td>
               <cc1:PopupControl id="PopupControl1" runat="server"  ButtonText="Add Publication" ButtonWidth="120px"
                  PopupHeightPx="125" PopupWidthPx="470" TextBoxWidth="0px" TargetURL="test.aspx" AutoPostBack="True" ForeColor="White">
               </cc1:PopupControl>
            </td>
         </tr>
         <tr>
            <td>        
               <asp:Literal ID=literal1 runat=server></asp:Literal>
            </td>
         </tr>
      </TBODY>
    </table>
    </div>
    </form>
</body>
</html>


The code behind page NoMasterPage.aspx.cs

   public partial class NoMasterPage : System.Web.UI.Page
   {
      #region Web Form Designer generated code
      override protected void OnInit(EventArgs e)
      {
         //
         // CODEGEN: This call is required by the ASP.NET Web Form Designer.
         //
         InitializeComponent();
         base.OnInit(e);
      }

      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         this.PopupControl1.Changed += new PopupControl.PopupControl.ChangedEventHandler(this.PopUpChange);
      }

      #endregion

      protected void Page_Load(object sender, EventArgs e)
      {

      }

      //Work ok w/o master page.
      protected void PopUpChange(object sender, EventArgs e)
      {
         literal1.Text = "U have selected " + this.PopupControl1.Text;
      }
   }

the litteral1.Text display the text U have selected D

Both default.aspx and NoMasterPage.aspx use the same targetURL test.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="test.Test" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      You see me as a dummy text
      <table>
         <tr>
            <td>Please select this
            </td>
            <td>
               <select class="pulldown" id="selAction"  runat="server">
                  <option value="" selected></option>
                  <option value="R">READ</option>
                  <option value="I">INSERT</option>
                  <option value="U">UPDATE</option>
                  <option value="D">DELETE</option>
               </select>            
            </td>
         </tr>
         <tr>
            <td>
               <asp:Button ID=btnSubmit Text=Ok runat=server OnClick="btnSubmit_Click" />
            </td>
         </tr>
      </table>

    </div>
    </form>
</body>
</html>

The code behind test.aspx.cs:
   public partial class Test : System.Web.UI.Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {

      }
      protected void btnSubmit_Click(object sender, EventArgs e)
      {
         PopupControl.PopupControl.ReturnValue(selAction.Value, "");

      }
   }


Author

Commented:
I put back the line

  string Onchange = "onchange=\"" + Page.ClientScript.GetPostBackEventReference(this, FieldID()) + "\"";
It should always use the GetPostBackEventReference.

And after that I changed all occurence ClientID to UniqueID. Rebuild and it worked.

Last time I rebuild but somehow the test application still using the old PopupControl.dll, that explained why it not work.

We all know that the __doPostBack event cann't fire because a wrong control id was using.
Hi, I have the same pblm. I have a custom button control and a custom event for that button. The custom event fires for simple pages. But for Master page - Content Page th event is not firing.

I did the following line at the content page load :
Page.RegisterRequiresRaiseEvent(btnOk);

Surprisingly in this case it is working. But pblm again when I have more than 1 buton. Then I have to register like:
Page.RegisterRequiresRaiseEvent(btnOk);
Page.RegisterRequiresRaiseEvent(btnCancel);
Always the second event is firing.

Did your pblm solved? Can anybody help on this?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.