Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use Asp.Net/VB.Net for my website and on a particular page I am trying to use some Javascript to set a horizontal scroll bar position. I don't know javascript but I have managed to get the following to work. When you press a button, it alters the scroll position. What I now need though, is to get rid of the buttons and just have the VB.Net code pass the 'posNo' across to the function when the page loads so that the scroll bar is automatically set..
ASP.NET
<asp:ImageButton ID="Button1" runat="server" ImageUrl="files/images/icons/Button1.gif" OnClientClick="gotoPage(1);return false;" ValidationGroup="Client" />
<asp:ImageButton ID="Button2" runat="server" ImageUrl="files/images/icons/button2.gif"  OnClientClick="gotoPage(2);return false;" ValidationGroup="Client" />
<asp:ImageButton ID="Button3" runat="server" ImageUrl="files/images/icons/button3.gif" OnClientClick="gotoPage(3);return false;" ValidationGroup="Client" />

<!-- javascript function -->
<script>
function gotoPage(posNo) {
     var scroll = 260 * posNo;
     document.getElementById('myScrollbox').scrollLeft = scroll;
}
</script>


What I have tried:

So this is what I tried (I'm probably way off).. I deleted the buttons and tried this as the VB code..
VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
     Dim posNo As Integer = "2"
     Dim cstype As Type = Me.GetType()
     Page.ClientScript.RegisterStartupScript(cstype, "goToPage('" & posNo & "');", True)
End Sub
Posted
Updated 5-May-20 1:31am
v2
Comments
Richard Deeming 7-May-20 12:37pm    
Other than assigning a string literal to an integer variable, what's wrong with what you've tried?

Dim posNo As Integer = "2"
should be
Dim posNo As Integer = 2


And you don't need the single quotes around the goToPage argument:
Page.ClientScript.RegisterStartupScript(cstype, "goToPage(" & posNo & ");", True)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900