Question

I have a linkbutton displaying the path of an uploaded document..The tag structure is like so

            <tr>
                <td>
                    <asp:Label ID="lblDoc" runat="server" Text="Document:"></asp:Label>
                </td>
                <td colspan="3">
                    <asp:LinkButton ID="lnkDoc" runat="server" PostBackUrl="~/Transfer.aspx"></asp:LinkButton>
                </td>
            </tr>

I am handling the onclick event on the server side:

Private Sub lnkDoc_Click(sender As Object, e As System.EventArgs) Handles lnkDoc.Click
    ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>window.open('OpenDocument.aspx?DocumentPath=" & System.Web.HttpUtility.UrlEncode(lnkDoc.Text) & "', 'OpenUploadedDoc','left=0px,top=0px,width='+screen.availWidth+',height='+screen.availHeight+',menubar=yes,resizable=yes,scrollbars=1')</script>")
End Sub

But I am not able to click the document path. When I hover over the linkbutton, nothing happens. What could be the issue?

EDIT: When I try to add onclick on the client side, I get the following error.

Transfer.Private Sub lnkDoc_Click(sender As Object, e As System.EventArgs)' is not accessible in this context because it is 'Private'.

Was it helpful?

Solution 3

ASPX

<tr>
                <td>
                    <asp:Label ID="lblDoc" runat="server" Text="Document:"></asp:Label>
                </td>
                <td colspan="3">
                    <asp:LinkButton ID="lnkDoc" CausesValidation="false" runat="server" Text="Test"></asp:LinkButton>
                </td>
            </tr>

Code behind

    Private Sub lnkDoc_Click(sender As Object, e As System.EventArgs) Handles lnkDoc.Click
         ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>alert('hi')</script>")
End Sub

OTHER TIPS

I think there are some validation on your page so click event is not fire so please set causesvalidation="false" of LinkButton as below code and check again:

<asp:LinkButton ID="lnkDoc" causesvalidation="false" runat="server" PostBackUrl="~/Transfer.aspx"></asp:LinkButton

Thanks, Hitesh

I have fixed the issue. The problem was with rendering the linkbutton in HTML(which was inside a panel). So I placed it in a outside the Panel.

    </asp:Panel>
    <div>
            <asp:Label ID="lblDoc" style="margin-left: 3px" Text="Document: " runat="server"></asp:Label>
            <asp:LinkButton ID="lnkDoc" style="margin-left:100px" runat="server" PostBackUrl="~/Transfer.aspx"></asp:LinkButton>
        </div>

Thanks all for your suggestions..!

try to give onclick event on .aspx page, and check it..

i hope that way it will work

Protected Sub lnkDoc_Click(sender As Object, e As System.EventArgs) Handles lnkDoc.Click
    ClientScript.RegisterStartupScript(Me.GetType(), "onclick", "<script language=javascript>window.open('OpenDocument.aspx?DocumentPath=" & System.Web.HttpUtility.UrlEncode(lnkDoc.Text) & "', 'OpenUploadedDoc','left=0px,top=0px,width='+screen.availWidth+',height='+screen.availHeight+',menubar=yes,resizable=yes,scrollbars=1')</script>")
End Sub

Note: make your event Protected not Private

And also You need to add the click event to link button.

  <asp:LinkButton ID="lnkDoc" runat="server" PostBackUrl="~/Transfer.aspx" onclick="lnkDoc_Click"></asp:linkbutton>

Add text to the button and make the handler public.

You need to add the click event to link button.

<asp:LinkButton ID="lnkDoc" runat="server"  onclick="lnkDoc_Click"></asp:linkbutton>

Page behind file you have to write

 Protected Sub lnkDoc_Click((ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDoc.Click
        // code you want to run
    End Sub

Maybe you want to set LinkButton.OnClientClick property? It let's you specify client-side handler for rendered link button control. Take a look:

<asp:linkbutton id="LinkButton1" text="Open Web site" onclientclick="Navigate()" onclick="LinkButton1_Click" runat=Server /> 

Here Navigate() is a JavaScript function called upon click, and LinkButton1_Click is a server side event handler.

You want to open a new browser window to display a document when user clicks a link, right? So a better approach will be to create JavaScript function with documentPath parameter (set on the server-side). No need to for messy RegisterStartupScript. You seem to run in circles now. Take a step back and rethink what you really want your code to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top