Solved

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

Posted on 2011-02-16
10
410 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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