tags:

views:

953

answers:

3

I need to add linked buttons in the code-behind based on a DataSet that is populated. How do I do that? Do I create a placeholder? I could be adding none, one, or many different linked buttons. I lalso need to have each linked button call a method in the code-behind. How would I wire that up when I add the button?

Thank you for any help.

+1  A: 

Since you are dynamically created the link buttons you should create a panel or placeholder on your client side so you can append your links to the panel so you would know where they will appear like:

    <asp:PlaceHolder ID="Linkholder" Runat="server"></asp:PlaceHolder>

and in your code behind

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

     For i = 0 To 2 //this is for you iterate how many links you need to add base on your dataset you referred to
       Dim ctrl As New LinkButton
  //and since it is based on the Database you need to give them unique ids if you want to know which one was clicked based on the data info like so
       ctrl.ID = ""+DB.id  //id is suppose to be a "column"field from 
       your database
       ctrl.Text = DB.id+"create"
       ctrl.CommandArgument = "myargument"
       ctrl.CommandName = "mycommandname"
       AddHandler ctrl.Click, AddressOf link_button_click
       Linkholder.Controls.Add(ctrl)
     Next

  End Sub

   Sub link_button_click(ByVal sender As Object, ByVal e As System.EventArgs)
   Dim lb As Linkbutton = CType(sender, LinkButton)

   displayObjects(lb.CommandArgument)­ ­ 
   End Sub

But if not dynamically How about you add the link buttons on the client side but set visible to false so you can also have the attributes onclick in the controls to what method you want them to go to and in your code behind based on what happens in your postback that you want to make the links appear.just set the attribute to true when that happens like so:

 <asp:linkbutton id="link1" runat="server" onclick="link1method" visible="false"/>

in code behind

 public Page_Load(object sender, EventArgs e)
{


   if(//is dataset whatever it is suppose to be for link to show)
   {
       link1.visible=true;
   }

}

public link1method(object sender, EventArgs e)
{


  //for link1 method onclick

}
TStamper
I don't know how many I will have at design time. I can't create the buttons ahead of time because I could have none, one, or maybe 25. I don't what to set it up with those links in the html.
Yes, then this approach won't work..give me a sec to think of another solution.
TStamper
well u do want all the links to go to the same method when clicked correct..since you don't know how many links will be?
TStamper
yes, they can all go to the same method as long as I can find out what link was clicked on
are you trying to do something similar to this: http://www.velocityreviews.com/forums/t114057-how-to-create-linkbutton-in-repeater-control-from-dataset.html
TStamper
In the code sample you provided, why did you add the controls within a !Page.IsPostBack? I thought you had to add the controls with every postback? If there is a postback then that set of code would not run and the links would not be in the rendered html.
sorry that was a mistake that I overlooked in my opinion it should not be within a postback or not withing one. it should just be in page load and within a if statement,based on the criteria that you were trying to satisfy..being in a postback just increases the speed
TStamper
for if it doesn't need to be on the page on first load
TStamper
+3  A: 

Here's one way:

on the front end, you'd have some type of data-repeater control, for example a GridView.

In the ItemTemplate for said databound control, you'd declare your link button:

<asp:linkButton CommandName='Foo' CommandArgument='<%# Eval("Bar") %>'>
<%# Eval("Text") %>
</asp:linkButton>

Take note that I'm assigning it a Command Name, and binding a value into the CommandArugment for the button. That's important.

Then, on the server side, you'd wire up an event for the data-repeater's Click event.

Check the Event Args class for the Command Name & Argument to determine which function you need to access based on the LinkButton clicked.

Stephen Wrighton
The linked button will not have a command but something similar to:<asp:LinkButton runat="server" ID="lnkDocumentOne" OnClick="lnkDocumentOne_Click" Text="Document One" />
+1  A: 

You'll need to write some code in your RowDataBound event for the given GridView you're binding to. In that method, you can add as many controls as you'd like dynamically with some simple code. In addition, you can leverage the CommandName and CommandArgument properties for easy click handling.

Your RowDataBound event will have the following code in it.

If e.Row.RowType = DataControlRowType.DataRow Then

   Dim SpecialLink As New LinkButton()

   SpecialLink.CommandName = "FancyCommand"
   SpecialLink.CommandArgument = e.Row.RowIndex.ToString ''//Or your Unique Data Id too.
   SpecialLink.Text = "Click me to do custom work."
   e.Row.Cells(0).Controls.Add(SpecialLink) ''//Put it in whatever grid cell you'd like

End If

Then you can use the RowCommand event to process your command easily enough.

Private Sub ProcessGridCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
           Handles gvComments.RowCommand

   Select Case e.CommandName

         Case "FancyCommand"

            DoSpecialProcessing(e.CommandArgument)

   End Select

End Sub

I like to use the Select only because it makes it easy to add more custom commands later. You can easily redo this code for C# if needs be.

In addition, if you need to add more than 1 link button, you can do it really fast in the code without having a bunch of placeholder links that may be set to visible or hidden.

EDIT: So after a quick dig, if you want to call your JavaScript code, then ditch the CommandName/Command Arguments above and you can do the following:

SpecialLink.Attributes("OnClick") = "lnkDocumentOne_Click();"

You may need to create a "catch all" type JavaScript method to handle the various links that would be calling it, or you can also use the Page.RegisterClientScript method to register all the JavaScript methods and create those dynamically as well.

Dillie-O
I am not using the button with the Command information. Here is an example of a linked button I am needing:<asp:LinkButton runat="server" ID="lnkDocumentOne" OnClick="lnkDocumentOne_Click" Text="Document One" />
Gotcha. You can easily append your "OnClick" type functionality as well with the LinkButton in code. I don't have any source for that off the top of my head, but it is easy to find.
Dillie-O
The OnClick in the html asp:LinkButton is creating a code-behind event, not calling a javascript function. Would what you are talking about still be able to work? Thank you.
Is there anything that you'll need to access via code behind when the link is created? If not, I'm thinking instead of creating a LinkButton object you can create an HTML Literal object and script in your own link with all the appropriate data. Create multiple literals for each link needed.
Dillie-O
Yes, I need to run code in the code-behind when the link is clicked so the HTML Literal would not work.
Okay, so now you have me slightly confused. The original idea I had posted would use code behind on a post back, but you said you needed the JavaScript. What if you went back to the original idea with Commands and then had them register JavaScript to occur when done processing?
Dillie-O