locked
Go to <A Name> RRS feed

  • Question

  • User-1653812950 posted

    Normally, to drop down on a web page to a specific spot, I could use

    <a href="#CH1" runat=server />

    <br />

    <br />

    <a name="CH1" runat=server />

    OR in the URL do: www.mysite.com/thispage.aspx?#CH1

    However, I want to write Code Behind that allows me to click a Button that would drop down to the <a name="CH1"> and can't figure out how to do this in VB.

    I've tried using FINDCONTROL but can't get this to work. It doesn't find the <a name> control.

    For Example:

    Dim lbl As Hyperlink
    lbl = Me.FindControl("CH1")
    If lbl IsNot Nothing Then
      Response.Write("Found it!")
      lbl.Focus()
    Else
      Response.Write("Did Not Find it!")
    End If

    Suggestions on how to write Code Behind that would allow me to drop down to <a name="CH1"> in VB?

    Thanks in advance for the help.

    Thursday, January 17, 2019 1:52 AM

Answers

  • User475983607 posted

    Not sure why the button does not work but the Hyperlink does.

    A bookmark link does not refresh the page.  A submit button refreshes the page.

    Set the MaintainScrollPositionOnPostback to true in the page directive if you want to maintain the scroll position on an ASP.NET Web Forms page when an ASP button is clicked.   

    <%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs" %>

    You can also format a link to look like a button.  This is a bootstrap example.

    <a href="#CH1" class="btn btn-primary">Go to CH1</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2019 6:49 PM

All replies

  • User283571144 posted

    Hi qwsoftdraw,

    As far as I know, we couldn't use FINDCONTROL to find the html control according to the control name. You should use control ID.

    I suggest you could add a control ID for the a tag and use it in the code-behind.

    Besides, I suggest you could htmlcontrol insteade of Hyperlink, since the html a tag isn't the Hyperlink in the vb.net.

    More details, you could refer to below codes:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DropDownWIthSpeicalValue.aspx.vb" Inherits="VBWebform.DropDownWIthSpeicalValue" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <a href="#CH1" runat="server"> sss</a>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
     
    
    <br />
    
    <br />
    <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br /><br />
                <br />
                <br />
                <br />
                <br />
                <br />
                <br />
    
                <br />
                <br />
           <div>aaaaaa</div>
    <a name="CH1" runat="server" id="CH1"  />
                     
    
            </div>
        </form>
    </body>
    </html>
    

    Code-behind:

    Public Class DropDownWIthSpeicalValue
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Dim lbl As HtmlControl = Me.FindControl("CH1")
            lbl.Focus()
        End Sub
    End Class

    Result:

    Best Regards,

    Brando

    Thursday, January 17, 2019 3:16 AM
  • User-1653812950 posted

    Thank you for taking the time to respond.

    I put your code into a separate HTML document than the one I'm working on and put all the code into one page:


    <%@ Page Language="vb" AutoEventWireup="false" %>

    <script language="vbscript" runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
    Dim lbl As HtmlControl = Me.FindControl("CH1")
    lbl.Focus()
    End Sub

    </script>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <a id="A1" href="#CH1" runat="server"> sss</a>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    <br />

    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />

    <br />
    <br />
    <div>aaaaaa</div>
    <a name="CH1" runat="server" id="CH1" />

    </div>

    <br />

    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />

    <br />
    <br />
    </form>
    </body>
    </html>

    1.) On a large computer, screen I had to add a bunch of <br /> after the <a name="CH1"> for the page to drop down. The hyperlink works on my end (as expected), but the button does not.

    2.) When I put similar code into my ASPX page, I get an error of "Object reference not set to an instance of an object.". The <a name="CH1" id="CH1" runat="server"> is in a ASCX page that gets loaded. The ASCX text comes into a Table, inside of a DIV, inside of another Table inside of a <ASP:Content>. Not sure if this makes a difference as to how the FINDCONTROL works or not.

    Not sure why the button does not work but the Hyperlink does.

    Thursday, January 17, 2019 5:35 PM
  • User475983607 posted

    Not sure why the button does not work but the Hyperlink does.

    A bookmark link does not refresh the page.  A submit button refreshes the page.

    Set the MaintainScrollPositionOnPostback to true in the page directive if you want to maintain the scroll position on an ASP.NET Web Forms page when an ASP button is clicked.   

    <%@ Page Language="C#" MaintainScrollPositionOnPostback="true" AutoEventWireup="true" CodeBehind="Default.aspx.cs" %>

    You can also format a link to look like a button.  This is a bootstrap example.

    <a href="#CH1" class="btn btn-primary">Go to CH1</a>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 17, 2019 6:49 PM
  • User-1653812950 posted

    The following is a really condensed version of the HTML, strictly to show the outline of my structure (yes, there are <TD>'s, <TR>'s missing just for simplicity of showing the outline):

    <asp:Content ContentPlaceHolderID="mainContent" Runat="Server">
      <table id="tblOne" runat="server">
        <div id="divMain" runat="server">
          <table id="tblTwo" runat="server">
            <asp:Label id="lblText" runat="server">

              ASCX File gets loaded here.

            </asp:Label>
          </table>
        </div>
      </table>
    </asp:Content>

    Here is the button1_click code that works and doesn't work:

    Dim cph As ContentPlaceHolder = Master.FindControl("mainContent") ' Finds Contentplaceholder: YES
    Dim tbl1 As Control = cph.FindControl("tblOne") ' Finds Table #1: YES
    Dim div1 As Control = tbl1.FindControl("divMain") ' Finds DIV: YES
    Dim tbl2 As Control = div1.FindControl("tblTwo") ' Finds Table #2: YES
    Dim lbl1 As Control = tbl2.FindControl("lblText") ' Finds Label: YES

    Dim lbl2 As HtmlControl = lbl1.FindControl("CH1") ' Finds HTMLControl within Label (from ASCX file): Does Not Work

    Thursday, January 17, 2019 9:01 PM
  • User-1653812950 posted

    Thank you for responding.

    I will try making a link look like a button but will have to do server side code. I am not looking for just one HTMLControl. The user will select an option from a Dropdownlistbox, then based on their selection, the final HTMLControl will be determined...CH1, CH2, CH3, etc. The page will then drop down to that selections. The ASCX file loads a book that the user has selected, then the user selects a chapter in the book.

    Thursday, January 17, 2019 9:12 PM
  • User475983607 posted

    qwsoftdraw

    Thank you for responding.

    I will try making a link look like a button but will have to do server side code. I am not looking for just one HTMLControl. The user will select an option from a Dropdownlistbox, then based on their selection, the final HTMLControl will be determined...CH1, CH2, CH3, etc. The page will then drop down to that selections. The ASCX file loads a book that the user has selected, then the user selects a chapter in the book.

    Your question is a bit confusing. An ASCX only runs on the server and has no meaning in the browser (DOM).  A bookmark is all browser side.  

    I would continue to render the bookmarks.  Add a bit of jQuery/JavaScript to the response that invokes a link click when the page loads in the browser.  This will cause the browser to scroll to the bookmark.

    Thursday, January 17, 2019 10:14 PM
  • User-1653812950 posted

    <quote>

    You can also format a link to look like a button.
    </quote>

    This is what I ended up doing that worked. Thank you.

    Thursday, January 17, 2019 10:33 PM
  • User-1653812950 posted

    Your question is a bit confusing. An ASCX only runs on the server and has no meaning in the browser (DOM).  A bookmark is all browser side.  

    You're right. An ASCX file is only on the server side.

    I use this to load the ASCX file:

    <%@ Register TagPrefix="HSB" TagName="BOOK1" Src="Book-01.ascx" %>
    <%@ Register TagPrefix="HSB" TagName="BOOK2" Src="Book-02.ascx" %>
    etc.

    I place this in the HTML section:

    <HSB:Book1 id="OTBook1" Visible="false" Runat="Server" />
    <HSB:Book2 id="OTBook2" Visible="false" Runat="Server" />
    etc.

    Then from Code Behind, I select which book becomes visible based on the User selection. The ASCX file has the <a name="CH1">, <a name="CH2">, etc. tags.

    Monday, January 21, 2019 10:40 PM