asp.net按钮导致回发但不触发事件 [英] asp.net button causes posts back but doesn't fire event

查看:87
本文介绍了asp.net按钮导致回发但不触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个ASP按钮,该按钮应该触发回发事件.这曾经可以工作,但已停止在搜索表单所在的每个页面上工作.自从我完成所有设置并开始工作以来,此特定代码尚未更新.按钮代码如下:

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

回发事件代码如下:

    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>标签,只剩下一个打开的表单标签和一个关闭的表单标签.就像我说的那样,这曾经有用,但现在却行不通.

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.

Page_Load方法中唯一的代码是为搜索表单创建下拉选项的代码(一直有效,现在仍然有效).没有什么可以结束输出或功能的.我正在尝试获得有关如何弄清楚为什么它将停止工作的调试想法.我试图获取用于引起回发的对象的ID,但是它变成空白.再说一次,也许我做错了.在Page_Load方法中,我按照`Request ["__(something)"];'的方式进行了一些操作.我不记得确切是什么,但它是将其设置为应该在其中包含对象ID的字符串变量.无论如何,我们将不胜感激.

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.

编辑

我还想指出,如果我将按钮的OnClick属性更改为不存在的属性,它确实会出错.因此,好像一切都按我的方式正确设置了(无论如何对我来说).此外,该网站上的所有其他控件仍然有效,并触发它的回发事件.

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>

在页面顶部:

<%@ 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);
        }
    }
}

推荐答案

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

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

这篇关于asp.net按钮导致回发但不触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆