c# - ASP :button onclick doesn't fire and page only refreshes

标签 c# html asp.net webforms

这是我的按钮的 aspx 代码:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

下面是代码隐藏文件中的 onclick 事件函数:

public void Button1_Click(object sender, EventArgs e)
{
    lblOutput.Text = txtFirstName.Text;
}

或者,我还在 onclick 函数中使用了它,以查看问题是否出在函数中的脚本而不是按钮本身:

public void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("http://www.google.com");
}

不幸的是,这也不起作用。所以问题可能是点击按钮时出了问题,对吧?

后面的代码已正确链接,因为 page_load 中的其他函数工作正常。

单击我的按钮时发生的所有事情是页面刷新。老实说,我不确定我是否可以称之为刷新,from 中的值仍然存在,页面只是跳回顶部,没有其他任何事情发生。

我还尝试创建第二个按钮,其 ID 和 onclick 名称略有不同,但页面的行为仍然相同。我在这里完全不知所措。我没有在谷歌上找到任何有帮助的东西。

编辑:这是完整的 asp 文件。我们将 ektron 用作 CMS,因此也有一些奇怪的代码。在任何人询问之前,是的,它在表单标签中。

    <%@ Page Language="C#" MasterPageFile="~/InteriorPage.master" AutoEventWireup="true" CodeFile="urmintakeform.cs" Inherits="urmintakeform" Debug="true" %>
<%@ Register Assembly="Ektron.Cms.Controls" Namespace="Ektron.Cms.Controls" TagPrefix="CMS" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<asp:Literal ID="Literal1" runat="server" />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PageBody" Runat="Server">

    <div id="two-column-pagenav" class="section-divider-small">
        <div class="column-one"><h2>University Relations and Marketing</h2></div>
            <div class="column-two">
                <p class="breadcrumb">
                    <CMS:FolderBreadcrumb ID="FolderBreadcrumb1" runat="server" DynamicParameter="id" SuppressWrapperTags="true" SuppressAddEdit="true" />
                </p>
            </div>
    </div>
    <div>
        <img alt="URM Banner" src="/images/department_banners/urm-banner.jpg" />
        <asp:Literal ID="litDeptImage" runat="server" />
    </div>
    <div id="" class="container section-divider-small">
        <div class="column-one">
            <div class="nav-aside widget">
                <div class="opener-block">
                    <a href="#" class="opener"><span>Menu</span></a>
                    <span class="title">University Relations and Marketing</span>
                </div>
                <div class="nav-page-box nav-slide">
                    <!--Replace this code with the Ektron Flex Menu Control-->
                    <CMS:FlexMenu ID="FlexMenu1" runat="server" SuppressAddEdit="true" DefaultMenuID="15032386736" DisplayXslt="xml/departments.xsl" SuppressWrapperTags="true" CacheInterval="0"  AllowClose="true" EnableSmartOpen="True" AutoCollapseBranches="True" StartCollapsed="True" />
                </div>
            </div>
            <div class="widget-holder">
                <div class="dept-contact-box widget">
                    <h4>How to Reach Us</h4>
                    <p><span style="font-weight: bold">University Relations and Marketing</span><br />
                        xxxx<br />
                        xxxx<br />
                        xxxxx<br />
                        xxxx<a href="tel:xxxxxxxxxxxxxxxxxxxxxxx</a><br />
                        Email: <a href="xxxxxxxxxxxxxxxxxxxxx">xxxxxxxxxxxxxxxxxxx</a><br />
                    </p>
                    <div class="dept-social-media">
                        <a title="Twitter" href="xxxxxxxxxxxxxxx" target="_blank">
                            <img src="/images/socmed_icons/twitter-32x432.png" alt="Twitter" title="Twitter" />
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div id="body-content" class="column-two">
            <div class="content-body">
                <div id="ctl00_PageBody_ContentBlock1" class="content-container">
                    <div class="content-holder">
                        <asp:Literal ID="litInPageTopNav" runat="server" />
                        <asp:Literal ID="ltrContent" runat="server" />
                        <CMS:FormBlock ID="FormBlock1" runat="server" DynamicParameter="ekfrm" />
                    </div>
                </div>

                <div class="intake">

                    <h2>Tell us about yourself</h2>
                    <div class="userinfo">
                        <label id="firstname" runat="server" text="First Name:" >First Name:</label>
                        <asp:TextBox ID="txtFirstName" runat="server" MaxLength="50" placeholder="First Name"></asp:TextBox><p></p>

                        <label id="lblLastName" runat="server" text="">Last Name:</label>
                        <asp:TextBox ID="txtLastName" runat="server" MaxLength="50" placeholder="Last Name"></asp:TextBox><p></p>

                        <label id="lblDepartment" runat="server" text="">Department:</label>
                        <asp:TextBox ID="txtDepartment" runat="server" MaxLength="50" placeholder="Department"></asp:TextBox><p></p>
                    </div>
                    <!--  Phone and Email  --------->
                    <div class="job">
                        <label id="lblPhone" runat="server" text="">Phone:</label>
                        <asp:TextBox ID="txtPhone" runat="server" MaxLength="50" placeholder="Phone"></asp:TextBox><p></p>

                        <label id="lblEmail" runat="server" text="Email">Email:</label>
                        <asp:TextBox ID="txtEmail" runat="server" MaxLength="50" placeholder="Email"></asp:TextBox><p></p>
                    </div>
                    <div style="clear:both;"></div>
                    <div class="help">
                        <h2>How can we help you?</h2>

                        <label id="lblCategory" runat="server" text="Help">Get Help With: </label>

                        <select name="areaselector" id="areaselector">
                            <option value="select">Select an Option</option>
                            <option value="design">Design</option>
                            <option value="imprinted">Imprinted</option>
                            <option value="photography">Photography</option>
                            <option value="social">Social Media</option>
                            <option value="written">Written Content</option>
                        </select>

                        <div id="design" class="areas" style="display:none" onchange="">
                            <asp:CheckBoxList ID="designASP" runat="server" OnSelectedIndexChanged="appendToSelectedList">
                                <asp:ListItem Value="brochures">Brochures</asp:ListItem>
                                <asp:ListItem Value="posters">Posters</asp:ListItem>
                                <asp:ListItem Value="postcards">Postcards</asp:ListItem>
                                <asp:ListItem Value="booklets">Booklets</asp:ListItem>
                                <asp:ListItem Value="advertisement">Advertisement Design</asp:ListItem>
                                <asp:ListItem Value="digitalMarketing">Digital Marketing Material</asp:ListItem>
                                <asp:ListItem Value="other">Other Promotional Material</asp:ListItem>
                            </asp:CheckBoxList>
                        </div>

                        <div id="imprinted" class="areas" style="display:none">
                            <asp:CheckBoxList ID="imprintedASP" runat="server" OnSelectedIndexChanged="appendToSelectedList">
                                <asp:ListItem Value="licensed vendors">Licensed Vendors</asp:ListItem>
                                <asp:ListItem Value="Imprinted product search/sample products">Imprinted product search/sample products</asp:ListItem>
                                <asp:ListItem Value="billboard leasing">Billboard Leasing</asp:ListItem>
                                <asp:ListItem Value="Imprinted product art/design">Imprinted product art/design</asp:ListItem>
                                <asp:ListItem Value="table covers">Table covers</asp:ListItem>
                                <asp:ListItem Value="retractable banners">Retractable banners</asp:ListItem>
                                <asp:ListItem Value="temporary signage">Temporary signage</asp:ListItem>
                                <asp:ListItem Value="tigerizing">"Tigerizing"</asp:ListItem>
                            </asp:CheckBoxList>
                        </div>

                        <div id="photography" class="areas" style="display:none">
                            <asp:CheckBoxList ID="photographyASP" runat="server" OnSelectedIndexChanged="appendToSelectedList">
                                <asp:ListItem Value="Official faculty and staff portraits">Official facutly and staff portraits</asp:ListItem>
                                <asp:ListItem Value="Groups and/or organizatoin portraits">Groups and/or organization Portraits</asp:ListItem>
                                <asp:ListItem Value="Organization Event photos">Organization Event photos</asp:ListItem>
                                <asp:ListItem Value="Student Event photos">Student Event photos</asp:ListItem>
                                <asp:ListItem Value="Departmental brochure and web photos">Departmental brochure and web photos</asp:ListItem>
                                <asp:ListItem Value="Sporting events">Sporting events</asp:ListItem>
                                <asp:ListItem Value="Guest speakers">Guest speakers</asp:ListItem>
                                <asp:ListItem Value="ID and/or nametags">ID and/or nametags</asp:ListItem>
                                <asp:ListItem Value="Visa\passports photographs">Visa\passports photographs</asp:ListItem>
                                <asp:ListItem Value="Scanning, retouching and custom printing">Scanning, retouching and custom printing</asp:ListItem>
                                <asp:ListItem Value="Archive/history FHSU image retrieval">Archive/history FHSU image retrieval</asp:ListItem>
                            </asp:CheckBoxList>
                        </div>

                        <div id="social" class="areas" style="display:none">
                            <asp:CheckBoxList ID="socialASP" runat="server" OnSelectedIndexChanged="appendToSelectedList">
                                <asp:ListItem Value="Training">Training</asp:ListItem>
                                <asp:ListItem Value="Event coverage">Event coverage</asp:ListItem>
                                <asp:ListItem Value="Marketing/promotion">Marketing/Promotion</asp:ListItem>
                            </asp:CheckBoxList>
                        </div>

                        <div id="written" class="areas" style="display:none">
                            <asp:CheckBoxList ID="writtenASP" runat="server" OnSelectedIndexChanged="appendToSelectedList">
                                <asp:ListItem Value="Print">Print</asp:ListItem>
                                <asp:ListItem Value="Web">Web</asp:ListItem>
                            </asp:CheckBoxList>
                        </div>

                    </div>
                    <div class="tellusmore">
                        <h2>Tell us more about your request</h2>
                        <asp:TextBox ID="tellUsMoreBox" TextMode="MultiLine" runat="server" Columns="50" Rows="6" />
                    </div>

                    <!--<asp:Button ID="btnSubmit" runat="server" Text="Submit Application" Enabled="true" CssClass="submit" onclick="btnSubmit_Click" />
                    <asp:Button ID="asdf" runat="server" Text="Submit Application" Enabled="true" CssClass="submit" OnClick="btnSubmit_Click" />-->


                    <asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" CausesValidation="false"/>
                </div>
            </div>
        </div>
    </div>
</asp:Content>

这是 C# 文件:

protected void Page_Load(object sender, EventArgs e)
    {

        ContentBlock ContentBlock1 = new ContentBlock();
        ContentBlock1.SuppressWrapperTags = true;
        ContentBlock1.DefaultContentID = 64;
        ContentBlock1.DynamicParameter = "id";
        ContentBlock1.Page = this.Page;
        ContentBlock1.Fill();
        //this.ltrContent.Text = ContentBlock1.Text.Replace("width", "max-width");
        string lsContent = ContentBlock1.Text.Replace("width:", "max-width:");
        lsContent = lsContent.Replace("width=", "max-width=");
        //this.ltrContent.Text = lsContent;
        //this.ContentBlock1.Text = lsContent;

        FolderBreadcrumb1.Text += " > <a href=\"" + ContentBlock1.EkItem.QuickLink + "\" class=\"active-link\">" + new CultureInfo("en").TextInfo.ToTitleCase(Server.HtmlDecode(ContentBlock1.EkItem.Title)) + "</a>";

        // dynamically generate an image if it been added as a meta image selector
        Ektron.Cms.CustomAttributeList ContentMetaData;
        ContentMetaData = ContentBlock1.GetMetaData();

        if (ContentMetaData.GetItemByName("department-image").Value.ToString().Length > 0)
        {
            string extension;
            extension = Path.GetExtension(Convert.ToString(ContentMetaData.GetItemByName("department-image").Value));
            string ext = extension.Substring(0, 4);
            if (ext == ".swf")
            {
                //is flash
                litDeptImage.Text = "<object id=\"myId\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"730\" height=\"175\">";
                litDeptImage.Text += "  <param name=\"movie\" value=\"" + Convert.ToString(ContentMetaData.GetItemByName("department-image").Value) + "\" />";
                litDeptImage.Text += "  <!--[if !IE]>-->";
                litDeptImage.Text += "  <object type=\"application/x-shockwave-flash\" data=\"" + Convert.ToString(ContentMetaData.GetItemByName("department-image").Value) + "\" width=\"730\" height=\"175\">";
                litDeptImage.Text += "  <!--<![endif]-->";
                litDeptImage.Text += "  <div>";
                litDeptImage.Text += "      <p>You have javascript turned off or are not running the latest version of Adobe Flash Player. To best view this site, please <a href=\"http://www.adobe.com/go/getflashplayer\"><img src=\"http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif\" alt=\"Get Adobe Flash player\" /></a></p>";
                litDeptImage.Text += "  </div>";
                litDeptImage.Text += "  <!--[if !IE]>-->";
                litDeptImage.Text += "  </object>";
                litDeptImage.Text += "  <!--<![endif]-->";
                this.litDeptImage.Text += "</object>";
            }
            else
            {
                //else image
                litDeptImage.Text = "<img class=\"ImageInline\" src=\"" + Convert.ToString(ContentMetaData.GetItemByName("department-image").Value) + "\" alt=\" \" />";
            }
        }
        //grab collection id from the folder id that the content block belongs to
        Ektron.Cms.API.Content.Content apiContent = new Ektron.Cms.API.Content.Content();
        Ektron.Cms.Content.EkContent ekContent = apiContent.EkContentRef;
        Microsoft.VisualBasic.Collection colldat = ekContent.GetAllCollectionsInfo(ContentBlock1.EkItem.FolderId, "id");
        string collid = null;
        foreach (Microsoft.VisualBasic.Collection field in colldat)
        {
            collid = field[1].ToString(); // collection id
        }

        long collint = Convert.ToInt64(collid);
        //create the collection object we'll be using to get department nav pages
        Ektron.Cms.Controls.Collection coll = new Collection();
        coll.DefaultCollectionID = collint;
        // set the standard coll properties
        coll.DisplayXslt = "xml/departments-nav.xsl";
        // attatch it to our invisible literal and fill it
        coll.Page = Page;
        coll.Fill();
        if (!String.IsNullOrEmpty(collid))
            litInPageTopNav.Text = coll.Text;

        //Use the meta data we grabbed earlier to display title, keywords and description
        Literal1.Text = "<title>" + Convert.ToString(ContentMetaData.GetItemByName("Title").Value) + " - xxxxxx University</title>\n";
        Literal1.Text += "<meta name=\"keywords\" content=\"" + Convert.ToString(ContentMetaData.GetItemByName("Keywords").Value) + "\">\n";
        Literal1.Text += "<meta name=\"description\" content=\"" + Convert.ToString(ContentMetaData.GetItemByName("Description").Value) + "\">\n";

        //lblOutput.Text = Output;


    }
public void Button1_Click(object sender, EventArgs e)
    {
        lblOutput.Text = txtFirstName.Text;
    }

编辑 2: 这是在运行时查看页面源时页面上的标签,我相信可能是由主文件提供的。正如我之前所说,我们使用 ektron cms。表单标签会不会有问题?

<form method="post" action="/urm/intake/" id="aspnetForm" onsubmit="if(&#39;undefined&#39; == typeof Ektron || !Ektron.FormBlock || !typeof Ektron.FormBlock.validate  || Ektron.FormBlock.validate(this)){$ektron(&#39;input[type=submit]&#39;).attr(&#39;disabled&#39;,&#39;disable&#39;);return true;}else{return false;}">

最佳答案

想通了。 CMS 是错误的。我正在使用 Ektron CMS 提供的 URL 在线查看页面。更改模板

<CMS:FormBlock ID="FormBlock1" runat="server" DynamicParameter="ekfrm" />

到内容 block 固定它。 ektron CMS 中的表单 block 导致了问题。

关于c# - ASP :button onclick doesn't fire and page only refreshes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38750789/

相关文章:

c# - 从 Javascript 方法调用 c# 函数

c# - 需要使用 javascript 将所选文本设为粗体/斜体/下划线,并使用 C# 保存和检索相同内容

c# - 获取c#函数值并在html表中显示

html - 2 列最小高度 100% 困难

javascript - ASP :Net Required field turn control back color red for validation groups

asp.net - LINQ to SQL Web 应用程序最佳实践

c# - 在 c# 中存在模式后,我想从一组字符串中获取子字符串

html - 伪前后元素上的 CSS3 Box-shadow

php - 通过 PHP 中的用户 <a> 标记将一个页面链接到另一个页面

c# - 图片上传不工作总是得到假值