简体   繁体   English

如果代码中使用了 Response.Write,asp 按钮不起作用

[英]asp Button not working if Response.Write is used in the code

So, I have been working with asp.net since a while now and I came across this bug that I still don't know the cause of.所以,我已经使用 asp.net 有一段时间了,但我遇到了这个我仍然不知道原因的错误。

My aspx file:我的aspx文件:

<%@ Page Title="Incomes and Outcomes" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="IncomePage.aspx.cs" Inherits="AccountingSite.IncomePage" EnableEventValidation="false"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link rel="stylesheet" href="Res/display.css"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="cont">
 <div class="text-center">
    <p class="box-main">Incomes and Outcomes</p>
</div>
<br />

<br />
<div class="row">
    <div class="col50 b1">
     <asp:TextBox runat="server" placeholder="Description" ID="inputDesc"></asp:TextBox>
    </div>
    <div class="col50 b1">
     <asp:TextBox runat="server" placeholder="Value" ID="inputValue"></asp:TextBox>
    </div>
</div>
<br />
<div class="row">
    <div class="col50 b1 autocomplete">
        <asp:TextBox runat="server" placeholder="Customer" ID="inputCustomer"
            AutoCompleteType="Disabled" ClientIDMode="Static"></asp:TextBox>
    </div>
</div>
    <br />
<div class="text-center">
    <asp:Button runat="server" Text="Add" CssClass="sub topBtn" ID="AddBtn"
        OnClick="AddIncome_Click" CausesValidation="false"/>
    <asp:Button runat="server" Text="Remove selected" CssClass="sub-remove topBtn"
        OnClick="RemoveIncome_Click" CausesValidation="false"/>
</div>
<br />
<br />
  <div class="row text-center">
    <label style="font-size: 18px;">Date:</label>
    <input type="date" id="inputDate" runat="server" onchange="ValueChanged();"/>
    <asp:Button runat="server" ID="SOME_BTN" ClientIDMode="Static" Text="Test"
        OnClick="SOME_BTN_Click" CssClass="asdf" CausesValidation="false"/>
  </div>
    <br />
    <div class="row">
        <div class="colThird topRow text-center f1">
            <label>Description</label>
        </div>
        <div class="colThird topRow text-center f1">
            <label>+</label>
        </div>
        <div class="colThird topRow text-center f1">
            <label>-</label>
        </div>
    </div>
                      <%
                          Response.Write("<h1>Test</h1>");
                          //Write();
                      %>
</div>
<script>
    function ValueChanged() {
        var sb = document.getElementById("<%=SOME_BTN.ClientID %>");
        sb.click();
    }
</script>
</asp:Content>

The cs file: .cs 文件:

    public partial class IncomePage : System.Web.UI.Page
{
    bool running = false;
    public double pos = 0;
    public double neg = 0;
    private static string[] Customers;

    protected void Page_Load(object sender, EventArgs e)
    {
        Main.CurrentPage = IPage.IncomePage;
        if (Session["Logged"] == null)
        {
            //Response.Redirect("LoginPage.aspx");
            //return;
        }
    }

    protected void AddIncome_Click(object sender, EventArgs e)
    {
        Main.Send(this, "test");
        
    }

    protected void RemoveIncome_Click(object sender, EventArgs e)
    {
        Main.Send(this, "test");
    }

    public void Write()
    {
        
    }

    public double GetBalance()
    {
        return Main.GetBalance();
    }

    protected void SOME_BTN_Click(object sender, EventArgs e)
    {
        if(inputDate.Value != null)
        {
            var x = DateTime.Parse(inputDate.Value).ToString().Split(' ')[0];
            Response.Redirect("IncomePage.aspx?d="+x);
        }
    }

}

The method Main.Send() runs alert() in javascript code, so it serves debugging purpose. Main.Send() 方法在 JavaScript 代码中运行 alert(),因此它用于调试目的。

It seems that whenever I add Response.Write() to the aspx file (as shown in the code above), asp buttons stop working so the click events do not fire at all.似乎每当我将 Response.Write() 添加到 aspx 文件(如上面的代码所示)时,asp 按钮就会停止工作,因此单击事件根本不会触发。 Strange enough, the code runs perfectly fine in my other aspx files.奇怪的是,代码在我的其他 aspx 文件中运行得非常好。

Don't use Response write if you don't need to, it messes up a lot of things, like rendering and stylesheets.如果不需要,请不要使用 Response write,它会弄乱很多东西,例如渲染和样式表。 Instead maybe think about setting text of a label and displaying that label (Visible = true) when you need to.相反,可以考虑设置标签文本并在需要时显示该标签(Visible = true)。 You could also use alert boxes via您还可以通过以下方式使用警报框

 ScriptManager.RegisterStartupScript(Page, Page.GetType(), "alrt", "alert('+ " label.Text + "');", true);  

You can call you function from code behind as well您也可以从后面的代码中调用您的函数

 ScriptManager.RegisterStartupScript(Page, Page.GetType(), "func", "functioName();", true); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM