Reputation: 13685
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.
Upvotes: 0
Views: 7447
Reputation: 30354
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
}
Upvotes: 2
Reputation: 29725
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.
Upvotes: 1
Reputation: 37819
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.
Upvotes: 3