tags:

views:

63

answers:

3

I have a class that inherits from the LinkButton ASP.NET control to include an int "Value" property for remembering a database ID when creating LinkButtons dynamically. This is so I can load some database driven content based on that ID on postback.

My question is: Is there a simple way to achieve this in C#? I have had a look around the internet and got a lot of info on delegates & custom event handling but it seems overly complex for what I need to do. Should I be reading up on delegates, generics & events or can I just override the LinkButtons Click event to capture the ID and query the DB?

EDIT

I'm creating my controls dynamically so I'm not sure how to link up an event to the click event of the dynamically added control. I take the points below about the CommandArgument property but does anyone have an example of using CommandArgument with a dynamically created control?

var lnkbtnTitle = new LinkButton();

            lnkbtnTitle.CssClass = "ABC";
            lnkbtnTitle.ToolTip = Description;
            lnkbtnTitle.Text = Name + ' ' + Version;
            lnkbtnTitle.CommandArgument = ID.ToString();
            pnlHelpGuidesView.Controls.Add(lnkbtnTitle);
+4  A: 

I don't think you need inherit from the linkButton to just attach an "ID" property, linkbutton has a property called "CommandArgument" which you can utilize to store this ID, and sent it on Postback.

J.W.
+1  A: 

The LinkButton control already has a CommandArgument property for this purpose. All you need to do is handle the Click or Command events, cast the CommandArgument to int, and you've captured the ID.

Jamie Ide
A: 

OK, I solved my own problem here and it turns out to be a rookie problem I have encountered before! I figured it was worth any potential embarrassment to post the resolution here for anyone else's benefit...

WHEN YOU CREATE DYNAMIC CONTROLS YOU MUST RECREATE THEM ON POSTBACK

This always seemed counter-intuitive to me but I had a if(!IsPostBack) directive on my control creation code, which meant that any solutions I tried the events did not fire. This was because the control no longer existed because I did not recreate it on postback.

In answer to my own question and taking on-board the points mentioned here, the following code works...

private void DrawHelpGuide(int ID, int Type, string Name, string Description, string Version, string URL)
    {
            var lnkbtnTitle = new LinkButton();

            lnkbtnTitle.CssClass = "HelpGuideTitle";
            lnkbtnTitle.ToolTip = Description;
            lnkbtnTitle.Text = Name + ' ' + Version;
            lnkbtnTitle.CommandArgument = ID.ToString();
            lnkbtnTitle.Click += new EventHandler(lnkbtnTitle_Click);
            pnlHelpGuidesView.Controls.Add(lnkbtnTitle);
        }

    void lnkbtnTitle_Click(object sender, EventArgs e)
    {
        var ClickedButton = (LinkButton)sender;
        var id = int.Parse(ClickedButton.CommandArgument);
    } 

I now also realise that the various versions I wrote previously, while more complex, would also have worked! In my specific case the CommandArgument method is perfectly adequate but I'm very pleased I now "get" the concept of delegates & events a lot more than I did previously :)

Rich Andrews