Solved

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

Posted on 2011-02-16
10
406 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

14 Experts available now in Live!

Get 1:1 Help Now