socialMatrix
socialMatrix

Reputation: 1443

asp LinkButton to call a C# method

According to MSDN I can have an OnClick for a LinkButton --> http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.onclick(v=VS.90).aspx

However, When I try to do following

 foeach(var item in items)
 {
 var link = new LinkButton
           {
               Text = item.Policy.PolNumber,
               OnClick = 
           };
 }

I don't have access to OnClick :-( where I can specify a C# function to be called. I do however have OnClientClick but that is for calling javascript function :-( Right? I also tried

var link = new LinkButton {Text = item.Policy.PolNumber};
link.Click +=new EventHandler(link_Click);

protected void link_Click(object sender, EventArgs e)
{
   //Do my stuff
}

This is never being executed :-(

I am trying to achieve this with a ASP table. In the foreach loop I am also doing.

var tRow = new TableRow();
var link = new LinkButton {Text = holding.Policy.PolNumber};
link.Click +=new EventHandler(link_Click);
var tCell = new TableCell ();
tCell.Controls.Add(link);
tRow.Cells.Add(tCell);

Upvotes: 0

Views: 2306

Answers (1)

coolcake
coolcake

Reputation: 2967

You need to add the link button to controls container or some other container on your page. That should be having the property runat="server". Looks like your linkbutton is not having the property runat="server" set. You can check in one of the replies in the following link for a sample dynamic link button creation.

Upvotes: 1

Related Questions