Howto add eventhandler to web custom usercontrol?

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

  1. jimmy.liang

    jimmy.liang 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>
     
    jimmy.liang, May 16, 2006
    #1
    1. Advertisements

  2. jimmy.liang

    jimmy.liang 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; }
    }
    ....
     
    jimmy.liang, May 16, 2006
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.