Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Web Control Events Problem

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
Hi, i'm trying to create my own pager control. I've built very basic web controls in the past but this one is the first where i have had to handle events. So far i have:

Code:
public class PagerCommandEventArgs : CommandEventArgs
{
    public int Page { get; set; }
    
    public PagerCommandEventArgs(int page, CommandEventArgs originalArgs) : base(originalArgs)
    {
        this.Page = page;
    }
}

public class Pager : WebControl
{
    public new int Page
    {
        get { return (int)(ViewState["Page"] ?? 1); }
        set { this.ViewState["Page"] = value; }
    }

    public int PageSize
    {
        get { return (int)(ViewState["PageSize"] ?? 15); }
        set { this.ViewState["PageSize"] = value; }
    }

    public int NumRows
    {
        get { return (int)(ViewState["NumRows"] ?? 0); }
        set { this.ViewState["NumRows"] = value; }
    }

    private static readonly object EventPagerCommand = new object();

    public event EventHandler<PagerCommandEventArgs> PagerCommand
    {
        add
        {
            this.Events.AddHandler(EventPagerCommand, value);
        }
        remove
        {
            this.Events.RemoveHandler(EventPagerCommand, value);
        }
    }

    protected virtual void OnPagerCommand(PagerCommandEventArgs e)
    {
        EventHandler<PagerCommandEventArgs> handler = (EventHandler<PagerCommandEventArgs>)base.Events[EventPagerCommand];

        if (handler != null)
            handler(this, e);
    }

    private LinkButton CreateCommand(string commandName, string buttonText, int fieldIndex)
    {
        LinkButton control = new LinkButton();
        control.Text = buttonText;
        control.CommandName = commandName;
        control.CommandArgument = fieldIndex.ToString();
        control.Command += new CommandEventHandler(HandleEvent);
        return control;
    }

    private void HandleEvent(object sender, CommandEventArgs e)
    {
        this.OnPagerCommand(new PagerCommandEventArgs(this.Page, e));
    }

    protected override void CreateChildControls()
    {
        this.Controls.Add(this.CreateCommand("First", "First", 0));
    }
}

As you can see it's pretty basic so far. I just want to test handling an event when the First button is clicked. I have created a web form with the following inside:

Code:
<cc1:Pager ID="pageNavigation" runat="server" OnPagerCommand="pageNavigation_PagerCommand"></cc1:Pager>

Then in my code behind i have:

Code:
protected void pageNavigation_PagerCommand(object sender, PagerCommandEventArgs e)
{
    // Do Nothing
}

I have added a break point in my PagerCommand event handler but it is never reached when i click the button.

Really appreciate if someone could help. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top