Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c# .net custom control can add click in the control codebehind but not when using the control

Posted on 2011-02-16
10
Medium Priority
?
413 Views
Last Modified: 2012-05-11
hi all, ive created my own custom control, now in the code behind of the custom control file i can add

Name.Click += new EventHandler(name);

but on the codebehind of the page where ive used the custom control i cannot, is there anyway i can regsiter it as an available attribute to add?

my code below
and on the image the test codebehind click is not available, how can i get it?
################# Button.ascx.cs #################
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class LargeButton : System.Web.UI.UserControl
{
    public string JSFunc { get; set; }
    public string SSFunc { get; set; }
    public string ImgName { get; set; }
    public string LinkText { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Link.Attributes.Add("onclick", "javascript:"+ JSFunc + ";");
        Link.HRef = "javascript:;";
        Link.Title = LinkText;
        Img.Src = "~/App_Themes/" + Page.Theme + "/Images/Icons32/" + ImgName;
        Img.Alt = LinkText;
        //Link.ServerClick += new EventHandler(asd);
    }
}
################# LargeButton.ascx #################
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LargeButton.ascx.cs" Inherits="LargeButton" %>
 
<a ID="Link"  runat="server" class="MenuItemLarge" clientidmode="Static"  >
    <img ID="Img" align="center" runat="server"/>
    <br />
    <%= LinkText %>
</a>
################# test.aspx #################
<MB:LgButton runat="server" ID="Save"   
            JSFunc="Save(this)" 
            ImgName="save_32.png"
            LinkText="Save"  />

Open in new window

noclick.png
0
Comment
Question by:awilderbeast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 5

Expert Comment

by:Siyamala
ID: 34905514
http://www.15seconds.com/issue/020319.htm


above url give some information
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34905541
to what part am i looking at for my problem?

do i need to create an abstract class instead of a usercontrol?

then add
protected System.Web.UI.WebControls.Link click?

that will add a click?

could you explain?
Thanks
0
 
LVL 10

Expert Comment

by:John Claes
ID: 34905741
I woudl suggest that you create a handle for the Click event and then register to that handle upon the page.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:awilderbeast
ID: 34905780
how do i do that?

Thanks
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34907812
ok i added
    public event EventHandler Click;
    public event EventHandler ServerClick;

to the button control code behind and now in the page code behind im using, serverclick is available as is click
but when i assign Name.serverclick+= new EventHandler(name);

i get no errors in the code but it just doesnt work, am i missing something else out?

Thanks
0
 
LVL 1

Accepted Solution

by:
awilderbeast earned 2000 total points
ID: 34908060
ive done it!

see below, then in your page ID.MyServerclick += new EvetnHandler(function);
public event EventHandler MyServerClick;

    protected void Page_Load(object sender, EventArgs e)
    {
        Link.ServerClick += MyServerClick;
}

Open in new window

0
 
LVL 10

Expert Comment

by:John Claes
ID: 34914343
I'm glad you found the way to create the EventHandler and subscribe to it.

regards

poor beggar
0
 
LVL 10

Expert Comment

by:John Claes
ID: 34941076
I object : We don't need to write his Code: we have to point him the way
0
 
LVL 1

Author Comment

by:awilderbeast
ID: 34941104
your advice did not help me though, i am new to c# and this is the first custom control ive made, i just ended up googling for another 4 hours as you can see in the time scale of my posts before coming across what i was trying to do.

i asked on here as i needed the answer there and then, not a hint to google for something else when i didnt even know what your hint meant

if i had that time to sit and google i wouldnt of asked on here! :S
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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