Calling a function from an asp:LinkButon

Mako1985

Member
Joined
Jan 29, 2008
Messages
9
Programming Experience
3-5
Hi,
I want to make a link to my "deleteBlogBlog(ByVal blogID as String)" function from each asp:linkbutton in my repeater. Unfortunately this doesn't seem to work. I can make it call sub procedures but not functions with parameters.
This is my code:
MyBlogs.aspx
VB.NET:
        <asp:Repeater ID="rptMyBlogs" runat="server">
            <ItemTemplate>
                        <b>DataBinder.Eval(Container.DataItem, "title")</b>
                           [B]<asp:LinkButton ID="deleteBlog" runat="server" Text="delete" 
                               OnClientClick="return confirm('Are you sure you want to delete record?');" 
                               OnClick='<%# deleteBlog(Cstr(DataBinder.Eval(Container.DataItem, "blogID")) %>' />[/B]
                        <br /> 
            </ItemTemplate>
        </asp:Repeater>
MyBlogs.aspx.vb
VB.NET:
Partial Class MyBlogs 
   Protected Sub deleteBlog(ByVal blogID As String)
        Dim dbConn As New DbConnection
        Dim parameters As IList(Of DbParameter) = New List(Of DbParameter)
        parameters.Add(New DbParameter("BlogID", blogID.ToString))
        dbConn.ExecuteNonQueryOnStoredProcudure("sp_DeleteBlog", parameters)
    End Sub
End Class

At this point i can't even call deleteBlog(2) with a set parameter. If anyone could help me on this one it would be greatly appreciated.
-Mark
 
First, the deleteBlog method is not a function as it is defined as a Sub and doesn't return a value.
Second, the click event handler of a link button has a signature defined by the framework and must be used if you want to assign a procedure to the click event handler of a linkbutton. This signature is shown in the code below.
One way to pass the blogID to the event handler would be to use the CommandArgument property of the linkbutton.
HTML:
<asp:Repeater ID="rptMyBlogs" runat="server">
    <ItemTemplate>
        <b><%#DataBinder.Eval(Container.DataItem, "title")%></b>
        <asp:LinkButton ID="deleteBlog" runat="server" Text="delete"
            OnClientClick="return confirm('Are you sure you want to delete record?');"
            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "blogID") %>'
            OnClick="deleteBlog_Click" />
        <br />
    </ItemTemplate>
</asp:Repeater>
VB.NET:
Protected Sub deleteBlog_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim dbConn As New DbConnection
    Dim parameters As List(Of DbParameter) = New List(Of DbParameter)
    parameters.Add(New DbParameter("blogID", CType(sender, LinkButton).CommandArgument))
    dbConn.ExecuteNonQueryOnStoredProcudure("sp_DeleteBlog", parameters)
End Sub
 
Back
Top