Solved

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

Posted on 2011-02-16
10
405 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
  • 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
 
LVL 1

Author Comment

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

Thanks
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now