Howto add eventhandler to web custom usercontrol?

Discussion in 'ASP .Net Building Controls' started by jimmy.liang@gmail.com, May 16, 2006.

  1. Guest

    hi,

    Is there any way to add eventhandler to custom usercontrol and could
    set it at webForm?

    i can add eventhandler on code, such as at Page_Load, add:
    MyControl1.SelectedIndexChanged = this.OnSelectedIndexChange;

    but, if i want to add eventhandler at webForm, like:
    <uc1:MyControl ID="MyControl1" runat="server"
    SelectedIndexChanged="OnSelectedIndexChange" />

    it will genarel a compile error : cannot create an object of type
    'System.EventHandler' from string. How can I do that?

    thanks,


    Detail as blow:
    -----------------------
    MyControl.ascx
    -----------------------
    <%@ Control Language="C#" ClassName="MyControl" %>
    <script runat="server">
    public EventHandler SelectedIndexChanged;
    public string SelectedValue
    {
    get
    {
    if (DropDownList1.SelectedIndex < 0)
    return "";
    else
    return DropDownList1.SelectedValue;
    }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender,
    EventArgs e)
    {
    if (SelectedIndexChanged != null)
    SelectedIndexChanged(this, null);
    }
    </script>

    <asp:DropDownList ID="DropDownList1" runat="server"
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
    AutoPostBack=true>
    <asp:ListItem Value=1 Text="item 01"></asp:ListItem>
    <asp:ListItem Value=2 Text="item 02"></asp:ListItem>
    <asp:ListItem Value=3 Text="item 03"></asp:ListItem>
    </asp:DropDownList>


    -----------------------------
    MyTestControl.aspx
    -----------------------------
    <%@ Page Language="C#" %>
    <%@ Register Src="Controls/MyControl.ascx" TagName="MyControl"
    TagPrefix="uc1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
    //MyControl1.SelectedIndexChanged = this.OnSelectedIndexChange;
    }

    void OnSelectedIndexChange(object sender, EventArgs e)
    {
    Label1.Text = "My selection is: " + MyControl1.SelectedValue;
    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>My Control</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <uc1:MyControl ID="MyControl1" runat="server"
    SelectedIndexChanged="OnSelectedIndexChange"
    /> <hr />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
    </body>
    </html>
    , May 16, 2006
    #1
    1. Advertising

  2. Guest

    OK, i make it like blow and it just work.
    ---------

    code in MyControl.ascx
    ....
    void Page_Load(object sender, EventArgs e)
    {
    if (strEvent != null && strEvent != "")
    {
    Delegate d = Delegate.CreateDelegate(typeof(EventHandler),
    Page, strEvent);
    SelectedIndexChanged = (EventHandler)d;
    }

    }

    private string strEvent;
    private EventHandler SelectedIndexChanged;

    public string OnSelectedIndexChanged
    {
    set { strEvent = value; }
    }
    ....
    , May 16, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QWxlc3NhbmRybyBSb3NzaQ==?=

    Add an eventhandler on a button_click

    =?Utf-8?B?QWxlc3NhbmRybyBSb3NzaQ==?=, Feb 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    462
    Chris Jackson
    Feb 17, 2004
  2. Karl
    Replies:
    0
    Views:
    519
  3. Oscar
    Replies:
    6
    Views:
    21,352
    Eirik
    Jul 31, 2005
  4. SimonSimon

    Add EventHandler to AudioClip?

    SimonSimon, May 8, 2006, in forum: Java
    Replies:
    1
    Views:
    358
    Oliver Wong
    May 8, 2006
  5. Simon Maltow

    Adding eventhandler dynamically to usercontrol

    Simon Maltow, Aug 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    123
Loading...

Share This Page