tags:

views:

212

answers:

2

I have a LinkButton server control which have some attached method in OnClick Event in somepage.aspx.cs file throw it's Id (control), I want to add another Server side Event also to that LinkButton.

How to achieve that?

Edit:

on default.aspx.cs this is a line of code which open a new window

lnlBtnProfile.Attributes.Add("onClick", "newwindow('" + acc.ProfileUrl + "','MyProfile');return false");

but I want another function to execute at same time.

+1  A: 

Here is a LinkButton with client side and server side event :

<asp:LinkButton runat="server" ID="myLink" 
   OnClientClick="alert('this is clientside event');" 
   OnClick="myLink_Click">
</asp:LinkButton>

Here is the server side event :

private void myLink_Click(object sender, EventArgs e)
{
    // Do some stuff
}

Use OnClientClick for client side events and OnClick for server side events. But if you write an return false at OnClientClick, your control won't postback and your server side event won't be called.

EDIT : As I said, just remove the 'return false;' line, and your attached server side event will be called. Because the 'return false;' blocks the rest of your code including postback to server.

lnlBtnProfile.Attributes
  .Add("onClick", "newwindow('" + acc.ProfileUrl + "','MyProfile');");

lnlBtnProfile.Click += new EventHandler(lnlBtnProfile_Click);
Canavar
+3  A: 

You can add multiple handlers for the server side click event the following way:

protected void Page_Load(object sender, EventArgs e)
{
    linkBtn.Click += new EventHandler(handler1);
    linkBtn.Click += new EventHandler(handler2);
}

protected void handler1(object sender, EventArgs e)
{
    lbl.Text += "33";
}
protected void handler2(object sender, EventArgs e)
{
    lbl.Text += "66";
}

This will result in firing both the handler1 and handler2 on a click of the given link button.

Genady Sergeev
Seach for the term 'Multicast Delegate'. For example: http://www.netsplore.com/PublicPortal/blog.aspx?EntryID=9
rohancragg