LinkButton Handles OnClick Equivelent in C#

To handle a Linkbutton onclick in VB.NET the LinkButton is declared and a handler sub declared:

<asp:linkbutton id="cmdAdd" runat="server" ResourceKey="Add" BorderStyle="none" CausesValidation="true" />

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
:::
:::
End Sub

C# doesn’t support the VB.net handles on click, the function is simply declared.

The LinkButton declaration is given below. Unlike vb.net there is no WithEvents part of the declaration.

protected global::System.Web.UI.WebControls.LinButton cmdAdd

Explanations of the above:

  • protected – Used to restrict access to the code to the same class.
  • global – Used to clarify that the object is taken from the global namespace as opposed to the current one.
  • :: – This operator, positioned between two indentifiers, is used to look up an identifier.
  • cmdAdd – name of the LinkButton

Looking in the .ascx presentation file the link button is declared as:

<asp:linkbutton id=”cmdAdd” runat=”server” ResourceKey=”cmdAdd” CausesValidation=”true” />

Begin the OnLoad, and include reference for the LinkButton

protected override void OnLoad(EventArgs e) {
base.OnLoad(e)
try {
cmdAdd.click += new System.EventHandler(cmdAdd_Click);

Then later in the code there is the cmdAdd_Click function:

private void cmdAdd_Click(System.Object sender, System.EventArgs e) {try {
// actions here for the function

 

References

MSDN: global

MSDN: The :: Operator

MSDN: How to: Use the Global Namespace Alias