Solved

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

Posted on 2011-02-16
10
409 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Need help with a query 14 55
CSS for Popup in ASP.NET 4 49
Best book for Internet security 4 55
Binding a GridView in ASP.NET(VB) using Using statements 6 19
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
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 antispam), the admini…

752 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