Pergunta

I have an ASP button on my page that is supposed to trigger an event on post back. This used to work but has stopped working on every single page that the search form is on. This particular code has not been updated since I got it all set up and working. The button code looks like:

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

And the post back event code looks like:

    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();
    }

I tried replacing the code inside of the event with just Response.Write("Hit");, but it never triggered at all. The page does post back though. There are no extra </form> tags on the page (or any page), leaving just one open form tag and one closing form tag. And like I said, this used to work but now is not.

The only code in the Page_Load method is code that creates drop down options for the search form (which has always worked and still does). There's nothing that would end the output or functionality. I'm trying to get debugging ideas as to how to figure out why this would stop working. I've tried to get the ID of the object used to cause post back but it's coming up blank. Then again, maybe I'm doing it wrong. In the Page_Load method, I did something along the lines of `Request["__(something)"];'. I don't remember exactly what it was, but it was setting that to a string variable which was supposed to have the object ID in it. Anyway, any help would be greatly appreciated.

EDIT

I also want to point out that if I change the OnClick attribute of my button to something that does not exist, it does err out. So it seems as if things are set correctly as I have them (to me, anyway). Also, every other control on the site still works and fires it's post back event.

Here is the panel my control is in:

<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>

At the top of my page:

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

The entire code behind for this particular page:

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);
        }
    }
}
Foi útil?

Solução

Well, I finally found the issue. I was thinking back and realized the very last change I made was adding Google's Tag Manager code to the website for some SEO tracking. That code turned out to be what was causing this one thing to stop working. I have no clue why. I removed it and everything is working.

    <!-- 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 -->
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top