开发者

Tablecell eventhandler

I am creating a small game of 4 in a row in C#.

My first thought was to use an existing Table control, give it 6 rows and 5 columns, and give all of the tablecell objects a unique id. I would then continue to add eventhandlers to each of th开发者_如何学编程e cells in the first row that I define myself, so that when a user clicks on one of them, my code would check the cells beneath the cell that was clicked and change the backgroundcolor of the cell that represents the piece that you inserted in real life.

My question is, how do I add a eventhandler to the tablecell object, since the only predefined events are DataBinding,Init,Load,PreRender and Unload, and since they dont fit my needs, I need to create my own. Any ideas?


A normal table cell doesn't have an OnClick event. The easiest alternative is to place a <asp:LinkButton>, <asp:Button> or <asp:ImageButton> inside the table cell. Of course, you can also create a ClickableTableCell control yourself. This is a very simple implementation:

public class ClickableTableCell : TableCell, IPostBackEventHandler
{
    public event EventHandler Click;

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(this, null));
        base.RenderBeginTag(writer);
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        OnClick(new EventArgs());
    }

    protected void OnClick(EventArgs e)
    {
        if (Click != null)
            Click(this, e);
    }
}

If you want to use this control, you have to register the control first, either on top of the aspx page, or in web.config.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜