asp.net按钮导致帖子返回,但不会触发事件

时间:2014-05-12 19:09:36

标签: asp.net onclick postback

我的页面上有一个ASP按钮,可以在回发时触发事件。这曾经工作但已停止在搜索表单所在的每个页面上工作。这个特殊的代码没有更新,因为我已经完成所有设置和工作。按钮代码如下所示:

<asp:Button id="search_button" class="search_button" runat="server" OnClick="search_button_click" />

回发事件代码如下:

    protected void search_button_click(Object sender, EventArgs e)
    {
        SessionHandler.sqlSearchTerm = searchBox.Text;

        if (Int32.Parse(searchCatDdl.SelectedValue.ToString()) > 0)
        {
            SessionHandler.search_mcat_id = searchCatDdl.SelectedValue.ToString();
        }
        else
        {
            SessionHandler.search_mcat_id = "0";
        }

        Response.Redirect("/search.aspx");
        Response.End();
    }

我尝试用Response.Write("Hit");替换事件内部的代码,但它根本没有触发。该页面确实回发了。页面(或任何页面)上没有额外的</form>标记,只留下一个打开的表单标记和一个结束表单标记。就像我说的那样,这曾经有效,但现在却没有。

Page_Load方法中唯一的代码是为搜索表单创建下拉选项的代码(它始终有效且仍然有效)。没有什么可以结束输出或功能。我试图获得调试想法,以便弄清楚为什么这会停止工作。我试图获取用于导致回发的对象的ID,但它是空白的。再说一遍,也许我做错了。在Page_Load方法中,我做了一些“请求[&#34; __(某事)&#34;];&#39;。我不记得它到底是什么,但它将它设置为一个字符串变量,该变量应该包含对象ID。无论如何,任何帮助将不胜感激。

修改

我还想指出,如果我将按钮的OnClick属性更改为不存在的内容,则会出错。因此,似乎事情设置正确,因为我拥有它们(无论如何)。此外,网站上的每个其他控件仍然有效,并触发它的回发事件。

以下是我的控件所在的面板:

<asp:Panel cssClass="search_items" id="pnlSearchButton" runat="server" DefaultButton="search_button">
    <div class="search_bar">
        <table>
            <tr>
                <td width="200"><h3 class="title">auction items</h3></td>
                <td width="230"><asp:TextBox ID="searchBox" runat="server" placeholder="Search" name="search" /></td>
                <td width="220">
                    <div class="select_cont option-set" id="filters">
                        <asp:DropDownList runat="server" ID="searchCatDdl" cssClass="option-set clearfix"  data-filter-group="selectset">
                        </asp:DropDownList>
                    </div>
                </td>
                <td width="70"><asp:Button id="search_button" cssClass="search_button" runat="server" OnClick="search_button_click" /></td>
                <td>
                    <a class="search_icon icon_collapse" id="toggle4"></a>
                    <div class="search_icon divider"></div>
                    <a href="#" class="search_icon icon_gridview" id="toggle6">&nbsp;</a> 
                    <a href="#" class="search_icon icon_listview" id="toggle5">&nbsp;</a>
                    <div class="search_icon divider"></div>
                    <a href="/search.aspx?adv=1" class="search_icon icon_advanced">&nbsp;</a>
                </td>
            </tr>
        </table>
    </div>
</asp:Panel>

在我的页面顶部:

<%@ Master Language="C#" MasterPageFile="~/master-pages/Site.Master" AutoEventWireup="true" CodeFile="HeaderFooter.master.cs" Inherits="master_pages.HeaderFooter" %>

此特定页面背后的完整代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CowansRedesign.master_pages
{
    public partial class HeaderFooter : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!String.IsNullOrEmpty(Request.QueryString["logout"]))
                {
                    SessionHandler.mailId = "";
                    SessionHandler.mailName = "";
                    SessionHandler.mailFirstName = "";
                }

                if (!String.IsNullOrEmpty(SessionHandler.mailId) && !String.IsNullOrEmpty(SessionHandler.mailFirstName) && Request.ServerVariables["SCRIPT_NAME"].ToString() != "/default.aspx")
                {
                    if (hiName != null) {
                        hiName.Text = "Hi " + SessionHandler.mailFirstName;
                    }
                }
            }

            if (!IsPostBack && searchCatDdl != null)
            {
                Dictionary<string, string> mainCatList = new Dictionary<string, string>();

                mainCatList.Add("0", "All Categories");

                using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["wesdb1SQL"].ToString()))
                using (SqlCommand strSQL = conn.CreateCommand())
                {
                    strSQL.CommandText = "Select mcat_id, mcat_name " +
                                         "From tblMcat " +
                                         "ORDER BY mcat_name ASC";

                    try
                    {
                        conn.Open();
                        using (SqlDataReader itemReader = strSQL.ExecuteReader())
                        {
                            while (itemReader.Read())
                            {
                                mainCatList.Add(itemReader["mcat_id"].ToString(), itemReader["mcat_name"].ToString());
                            }
                            itemReader.Close();
                        }
                    }
                    catch (Exception e1)
                    {
                        Console.WriteLine(e1.ToString());
                        //Response.Write(e.ToString());
                    }
                    finally
                    {
                        conn.Close();
                    }
                }

                searchCatDdl.DataSource = mainCatList;
                searchCatDdl.DataTextField = "Value";
                searchCatDdl.DataValueField = "Key";
                searchCatDdl.DataBind();
            }
        }

        protected void overlay_itemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                bool isSaleOnline = Public.isSaleOnline(DataBinder.Eval(e.Item.DataItem, "EventSaleId").ToString());
                bool isSaleLotted = Public.isSaleLotted(DataBinder.Eval(e.Item.DataItem, "EventSaleId").ToString());
                Image overlayImage = (Image)e.Item.FindControl("overlayImage");
                HyperLink auctionLink = (HyperLink)e.Item.FindControl("viewAuction");
                HyperLink regLink = (HyperLink)e.Item.FindControl("viewReg");
                HyperLink catalogLink = (HyperLink)e.Item.FindControl("viewCatalog");
                Label slide_date = (Label)e.Item.FindControl("slide_date");
                Label EventName = (Label)e.Item.FindControl("EventName");

                EventName.Text = DataBinder.Eval(e.Item.DataItem, "EventName").ToString();

                overlayImage.ImageUrl = "http://cowansauctions.com/webimages/events/" + DataBinder.Eval(e.Item.DataItem, "EventMain");

                string[] formats = { "MM/dd/yyyy", "MM-dd-yyyy", "yyyy-MM-dd HH:mm:ss", "yyyyMMdd HH:mm:ss" };
                IFormatProvider culture = new CultureInfo("en-US", true);

                DateTime formattedDate;

                //Response.Write(DataBinder.Eval(e.Item.DataItem, "homeDate").ToString());
                //Response.End();

                DateTime.TryParseExact(DataBinder.Eval(e.Item.DataItem, "homeDate").ToString(), formats, culture, DateTimeStyles.None, out formattedDate);

                slide_date.Text = String.Format("{0:MM.dd.yy}", formattedDate);

                if (DataBinder.Eval(e.Item.DataItem, "EventSaleId").ToString().Length >= 1)
                {
                    auctionLink.Text = "More about the auction&nbsp;>";
                    auctionLink.NavigateUrl = "/auctions/details.aspx?id=" + DataBinder.Eval(e.Item.DataItem, "EventId");

                    if (isSaleOnline)
                    {
                        catalogLink.Text = "View Catalog&nbsp;>";
                        catalogLink.NavigateUrl = "/auctions/catalog.aspx?id=" + DataBinder.Eval(e.Item.DataItem, "EventSaleId") + "" + (!String.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "EventStartPage").ToString()) ? "&page=" + DataBinder.Eval(e.Item.DataItem, "EventStartPage") : "");

                        regLink.Text = "Register to bid online&nbsp;>";
                        regLink.NavigateUrl = "/auctions/live-bid.aspx";
                    }
                    else
                    {
                        if (Convert.ToBoolean(DataBinder.Eval(e.Item.DataItem, "EventRegister")))
                        {
                            regLink.Text = "Register to bid online&nbsp;>";
                            regLink.NavigateUrl = "/auctions/live-bid.aspx";
                        }

                        if (isSaleLotted)
                        {
                            catalogLink.Text = "View Catalog&nbsp;>";
                            catalogLink.NavigateUrl = "/auctions/catalog.aspx?id=" + DataBinder.Eval(e.Item.DataItem, "EventSaleId") + "" + (!String.IsNullOrEmpty(DataBinder.Eval(e.Item.DataItem, "EventStartPage").ToString()) ? "&page=" + DataBinder.Eval(e.Item.DataItem, "EventStartPage") : "");
                        }
                    }
                }
                else
                {
                    catalogLink.Text = "View Event Details&nbsp;>";
                    catalogLink.NavigateUrl = "/event.aspx?id=" + DataBinder.Eval(e.Item.DataItem, "EventId");

                    auctionLink.Visible = false;
                    regLink.Visible = false;
                }
            }
        }

        protected void search_button_click(Object sender, EventArgs e)
        {
            SessionHandler.sqlSearchTerm = searchBox.Text;

            if (Int32.Parse(searchCatDdl.SelectedValue.ToString()) > 0)
            {
                SessionHandler.search_mcat_id = searchCatDdl.SelectedValue.ToString();
            }
            else
            {
                SessionHandler.search_mcat_id = "0";
            }

            Response.Redirect("/search.aspx");
            Response.End();
        }

        public static string StripHTML(string htmlString)
        {
            string pattern = @"<(.|\n)*?>";

            return Regex.Replace(htmlString, pattern, string.Empty);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好吧,我终于找到了这个问题。我正在回想起并意识到我所做的最后一次更改是将Google的代码管理器代码添加到网站进行一些SEO跟踪。该代码原来是导致这一件事停止工作的原因。我不知道为什么。我删除了它,一切正常。

    <!-- Google Tag Manager -->
    <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-5XQX2B"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-fdsafdsa');</script>
    <!-- End Google Tag Manager -->