Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

LinkButton

  Asked By: Conrad    Date: Apr 19    Category: Asp.net    Views: 591
  

I'm creating LinkButtons dynamically ...


Dim LinkButton as New LinkButton()
LinkButton.Text = "Link Text"
LinkButton.ID = "LinkID"
FormName.Controls.Add(LinkButton)

How do I specify the event name for OnClick like I would if I put it
in the form:

<asp:LinkButton
id="LinkID"
Text="Link Text"
OnClick="LinkButton_Click"
runat="server" />

Or, how is the event name for OnClick specified when being generated
dynamically? I'm having some trouble with this. I've read the docs,
some articles and relevant message saved from this list; I am not
getting the big picture.

Can someone please enlighten me? I think I have OnCommand under
control, but I might ask about that next.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Isaac Evans     Answered On: Apr 19

I don't have a lot of experience with dynamically-created controls  yet, so
I'm not sure if it's efficient (or correct) but I was able to get this
control to call this server-side event...


< %@Import <mailto:%@Import> Namespace="System"%>
<%@ Page Language="vb" AutoEventWireup="false" %>

<HTML>
<HEAD>
<SCRIPT runat=server>

Protected WithEvents LinkButton as new LinkButton()

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

LinkButton.Text = "Link Text"
LinkButton.ID = "LinkID"
PlaceHolder1.Controls.Add(LinkButton)

End Sub

Public Sub X(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles LinkButton.Click
Response.Write("running function X<br>")
End Sub
</SCRIPT>
</HEAD>
<body>
<form id=frmX runat=server>
<asp:PlaceHolder id=PlaceHolder1 runat="server"></asp:PlaceHolder>
</form>
</body>
</HTML>


the line --- Protected WithEvents LinkButton as new LinkButton()
--- is important because it creates a "holding variable" for the server-side
code to use and bind the event  to. If it's not possible to pre-define your
dynamic controls w/ pre-known variables - then this example won't be of much
help...

 
Answer #2    Answered By: Erin Dunn     Answered On: Apr 19

Here is some C# code which dynamically  generates ImageButtons
and uses values from an OLeDbDatareader:


imageItem = new System.Web.UI.WebControls.ImageButton();


imageItem.ImageUrl = "CM/Links/LinkImages/" + dr.GetString(3);

imageItem.ID = dr.GetInt32(0).ToString();

imageItem.Command += new CommandEventHandler(ImageButton_Command);

imageItem.CommandName = dr.GetInt32(0).ToString();

private void ImageButton_Command(object sender, CommandEventArgs e)

{

string Sql = "INSERT INTO LinkManagementItems " +

"(ClickDate, ClickThroughs, LinkManagementID) " +

"VALUES ('" + DateTime.Now + "', " + 1 + ", " +

Convert.ToInt32(e.CommandName) + ")";


try

{

DBUtility.ExecuteSQLCommand(Sql);

}

catch (Exception exc)

{

Response.Write(exc.Message.ToString());

}

}

If I had more time I'd flesh it out for you, but this should give you a bit of
help.

 
Answer #3    Answered By: Ana Bradley     Answered On: Apr 19

Try doing this:


Dim LinkButton as New LinkButton()
LinkButton.Text = "Link Text"
LinkButton.ID = "LinkID"
FormName.Controls.Add(LinkButton)

AddHandler LinkButton.Click, AddressOf MyClickHandler

...
Private Sub MyClickHandler(sender as Object, e As eventArgs)
DoSomething()
End Sub

The AddHandler "wires" the MyClickHandler to the Click event, so that
when the linkbutton is clicked, MyClickHandler is called.

 
Didn't find what you were looking for? Find more on LinkButton Or get search suggestion and latest updates.

Related Topics:



Tagged:  

 

Related Post