Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Click Event won't fire in custom control

Posted on 2009-07-09
10
Medium Priority
?
1,482 Views
Last Modified: 2013-12-17
I've created a custom control that dynamically creates form fields. Though a button I've created will not fire when I click it.

I'm using Visual Studio 2008 and the AJAX ModalPopupExtender.
When I click the Refer button, the popup disappears, and nothing else happens.
This is the first time I've created a custom control like this, so I'm wondering if I need to reload the controls somehow.

Thanks!
Mike
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using skmValidators;
using AjaxControlToolkit;
 
namespace ReferralPopupControl
{
    [ToolboxData("<{0}:ReferralControl runat=server></{0}:ReferralControl>")]
    public class rpc : WebControl
    {
        private string targetControlID = "";
        public TextBox txtNotes;
        public DropDownList ddlEmployee;
        public CheckBoxList cblApplications;
        private Button btnRefer;
 
        public string TargetControlID
        {
            get { return targetControlID; }
            set { targetControlID = value; }
        }
 
        protected override void CreateChildControls()
        {
            CreateControls();
        }
 
        private void CreateControls()
        {
            Panel pnlPopup = new Panel();
            pnlPopup.ID = "pnlPopupForReferral";
            pnlPopup.Style.Add("display", "none");
 
            Table t = new Table();
            t.Width = Unit.Pixel(600);
            t.Style.Add("border", "1px solid black");
            t.Style.Add("background-color", "white");
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.Text = "Refer Customer";
            tc.Font.Bold = true;
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Style.Add("font-size", "18px");
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.Text = "Pick Applicaple Applications";
 
            /*CheckBoxListValidator cblv = new CheckBoxListValidator();
            cblv.ControlToValidate = "cblApplications";
            cblv.Text = "Please select atleast one application.";
            cblv.MinimumNumberOfSelectedCheckBoxes = 1;
            tc.Controls.Add(cblv);*/
 
            tc.Font.Bold = true;
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            cblApplications = new CheckBoxList();
            cblApplications.ID = "cblApplications";
            cblApplications.Width = Unit.Percentage(100);
            cblApplications.RepeatColumns = 3;
            cblApplications.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
            cblApplications.RepeatLayout = RepeatLayout.Table;
 
            DataAccess da = new DataAccess();
            cblApplications.DataSource = da.getApplicationTypes();
            cblApplications.DataTextField = "applicationType";
            cblApplications.DataValueField = "id";
            cblApplications.DataBind();
 
            tc.Controls.Add(cblApplications);
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.Text = "Employee to Refer Customer to";
            tc.Font.Bold = true;
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            ddlEmployee = new DropDownList();
            ddlEmployee.ID = "ddlEmployee";
 
            ddlEmployee.DataSource = da.getBankers();
            ddlEmployee.DataTextField = "fullName";
            ddlEmployee.DataValueField = "id";
            ddlEmployee.DataBind();
 
            tc.Controls.Add(ddlEmployee);
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            tc.Text = "Reason & Relevant Notes";
            tc.Font.Bold = true;
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            tr = new TableRow();
            tc = new TableCell();
            tc.ColumnSpan = 2;
            txtNotes = new TextBox();
            txtNotes.TextMode = TextBoxMode.MultiLine;
            txtNotes.Rows = 5;
            txtNotes.Width = Unit.Percentage(99);
 
            tc.Controls.Add(txtNotes);
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            btnRefer = new Button();
            btnRefer.ID = "btnRefer";
            btnRefer.Text = "Refer Customer";
            btnRefer.Click += new EventHandler(this.btnRefer_Click);
 
            // btnRefer.Attributes.Add("onclick", "alert('hi');");
 
            Button btnCancel = new Button();
            btnCancel.ID = "btnCancel";
            btnCancel.Text = "Cancel";
 
            tr = new TableRow();
            tc = new TableCell();
            tc.Width = Unit.Percentage(50);
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Controls.Add(btnRefer);
            tr.Cells.Add(tc);
            tc = new TableCell();
            tc.Width = Unit.Percentage(50);
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.Controls.Add(btnCancel);
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
 
            pnlPopup.Controls.Add(t);
 
            AjaxControlToolkit.ModalPopupExtender mpe = new ModalPopupExtender();
            mpe.ID = "mpeReferralPopup";
            mpe.TargetControlID = targetControlID;
            mpe.PopupControlID = "pnlPopupForReferral";
 
            mpe.DropShadow = true;
 
            this.Controls.Add(pnlPopup);
            this.Controls.Add(mpe);
        }
 
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }
 
        private void btnRefer_Click(object sender, EventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "clientscript", "document.getElementByID('pnlPopupForReferral').style.display = \"none\";", true);
 
            Page.ClientScript.RegisterStartupScript(this.GetType(), "clientscript", "alert('hi');", true);
            DataAccess da = new DataAccess();
 
            string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            username = username.Substring(username.LastIndexOf("\\") + 1);
            int employeeID = da.getUserEmployeeID(username);
 
            int count = 0;
            foreach (ListItem li in cblApplications.Items)
            {
                if(li.Selected)
                    count++;
            }
 
            int[] chkApplications = new int[count];
            int i = 0;
            foreach (ListItem li in cblApplications.Items)
            {
                if (li.Selected)
                {
                    chkApplications[i] = Convert.ToInt32(li.Value);
                    i++;
                }
            }
            int emplID = Convert.ToInt32(ddlEmployee.SelectedValue);
            string notes = txtNotes.Text;
 
            da.addReferral(employeeID, chkApplications, emplID, notes);
        }
    }
}

Open in new window

0
Comment
Question by:jouwstrm
  • 5
  • 5
10 Comments
 
LVL 12

Accepted Solution

by:
williamcampbell earned 2000 total points
ID: 24816443
As it has child controls dealing with postback, your control is a composite
control and may need to also implement  INamingContainer interface so that the
postback data is routed correctly.

This solves duplicate names. Give it a try.

0
 
LVL 3

Author Comment

by:jouwstrm
ID: 24816544
like this?
public class rpc : WebControl, INamingContainer
{
...
}

That didn't seem to work for me, same result.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24816632
Did you implement the functions for INamingContainer?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:jouwstrm
ID: 24816725
I guess I don't understand what functions I would need to implement, or how I would do it.
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24816825
My Bad

It is a "marker" interface, meaning that some piece of
code, e.g., the ASP.NET runtime, will use its presence to take some action.

.but what it actually does it ensure that controls are given unique
ids...this is important if you are creating composite controls or something
where you are specifying an ID for the dynamically created
child-controls.

Anyways it didn't seem to make a difference.


0
 
LVL 3

Author Comment

by:jouwstrm
ID: 24816909
hold on actually, looks like its getting into the event handler now... I didn't think it was because its not registering the javascript to the page. I modified a field on the event click and its showing up on my page. Any idea why Page.ClientScript.RegisterStartupScript is not working for me?
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24816915
Two more things to Try

Do you have EnsureChildControls() in Page_Init and CreateChildControls() in OnPreRender?

Try Inheriting from CompositeControl instead of WebControl
0
 
LVL 12

Expert Comment

by:williamcampbell
ID: 24816933
If you remove INamingContainer  does it still hit the event?
0
 
LVL 3

Author Comment

by:jouwstrm
ID: 24816964
actually, without the INamingContainer, my field values disappear on me and I'm unable to tell if the field is updated from the event handler.
0
 
LVL 3

Author Comment

by:jouwstrm
ID: 24816970
with no INamingContainer, After I hit the Refer button, my modalpopup disappears, and the values are cleared
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question