asp:按钮onclick不会触发,页面只刷新

时间:2016-08-03 18:07:47

标签: 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中的其他函数正常工作。

单击我的按钮时发生的所有事情都是页面刷新。说实话,我不确定我是否可以将其称为刷新,来自的值仍然存在,页面只是跳回到顶部而没有其他事情发生。

我还尝试创建第二个按钮,其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;}">

1 个答案:

答案 0 :(得分:0)

想出来。 CMS是有错的。我正在使用Ektron CMS提供的URL在线查看页面。更改formblock

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

到内容块修复它。 ektron CMS中的表单块引发了问题。