简体   繁体   English

如何使添加的按钮转到页面上的不同部分?

[英]How to make an added button go to a different section on the page?

I have this simple page that is meant to create a button and a new section, when both are created the button should take you to that new section in the page, but the page refreshes and never takes you there, here's what i have:我有一个简单的页面,用于创建一个按钮和一个新部分,当两者都被创建时,按钮应该带你到页面中的新部分,但页面刷新并且永远不会带你到那里,这是我所拥有的:

.aspx .aspx

    <br />

    <div runat="server" id="panel1" class="panel panel-success">

        <div class="panel-heading">
            <h2>Add a panel</h2>
        </div>

        <div class="panel-body">
            <div class="row">
                <div class="col-md-5">
                    <label>Name: </label>
                    <input runat="server" id="name" class="form-control" type="text" />
                    <br />
                    <label>Some text: </label>
                    <textarea runat="server" id="textarea" class="form-control" rows="5"></textarea>
                    <br />
                    <asp:Button runat="server" class="btn btn-success" id="Add" Text="Add Section + Button"/>
                </div>

                <div class="col-md-5">
                    <h3>Buttons will be here:</h3>
                    <div runat="server" id="BtnDiv">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <br />
    <h3>Panels Will be here: </h3>
    <hr />
    <div runat="server" id="div1" class="panel">

    </div>

c# C#

 public partial class TESTgo : System.Web.UI.Page
    {
        static int count = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            Add.Click += Add_Click;
        }

        private void Add_Click(object sender, EventArgs e)
        {
            string NameBtn = name.Value;
            string text = textarea.Value;

            BtnDiv.InnerHtml += "<button class='btn btn-default' href='#Section" + count + "'>Go to Section " + count + "</button>";
            div1.InnerHtml += "<div id='Section" + count + "' class='panel panel-warning'>";
            div1.InnerHtml += "<div class='panel-heading'> " + NameBtn + "</div>";
            div1.InnerHtml += "<div class='panel-body'><h3>" + NameBtn + "</h3>";
            div1.InnerHtml += "<p>" + text + "</p>";
            div1.InnerHtml += "</div></div>";

            count += 1;
        }
    }

Any Help, suggestions and edits are welcome, Thanks in advance.欢迎任何帮助、建议和编辑,提前致谢。

You can either:您可以:

Use attribute使用属性

<button type="button"></button> 

to prevent the default behavior of submission.以防止提交的默认行为。 You can use javascript too and add an onclick event listener with event.preventDefault() at the beggining of the function.您也可以使用 javascript 并在函数开始时使用 event.preventDefault() 添加一个 onclick 事件侦听器。

OR或者

Thinking of the classnames you might be using bootstrap, so instead of using button you might wanna use anchor tags for referencing ID elements on your page without submission like this:考虑到您可能正在使用引导程序的类名,因此您可能想要使用锚标记来引用页面上的 ID 元素而不像这样提交,而不是使用按钮:

BtnDiv.InnerHtml += "<a class='btn btn-default' href='#Section" + count + "'>Go to Section " + count + "</a>";

HTML buttons do not have href attributes. HTML 按钮没有 href 属性。 Change it to an anchor tag, or use the onclick event handler of the button.将其更改为锚标记,或使用按钮的 onclick 事件处理程序。

BtnDiv.InnerHtml += "<button class='btn btn-default' href='#Section" + count + "'>Go to Section " + count + clicking "</button>";

becomes... .变成…… BtnDiv.InnerHtml += "<a class='btn btn-default' href='#Section" + count + "'>Go to Section " + count + "</a>";

or...或者...

BtnDiv.InnerHtml += "<button class='btn btn-default' onclick='goto(\\"Section" + count + "\\");'>Go to Section " + count + "</button>";

(you would still have to implement goto(segment) in this example, likely setting location.href = *${url}${segment}* <- replace *s with backticks here, markdown restriction) (你仍然需要在这个例子中实现goto(segment) ,可能设置location.href = *${url}${segment}* <- 在这里用反引号替换 *s,降价限制)


Side note, and this is only a preference, but I would recommend a different quote scheme...旁注,这只是一个偏好,但我会推荐一个不同的报价方案......

BtnDiv.InnerHtml += $"<button class=\\"btn btn-default\\" onclick=\\"('Section{count}');\\">Go to Section {count}</button>";

It is more common to wrap HTML attributes in double quotes, and JS strings in single quotes..更常见的是用双引号包裹 HTML 属性,用单引号包裹 JS 字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM