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.

My aspx file:

<%@ 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:

    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.

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. Strange enough, the code runs perfectly fine in my other aspx files.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Don’t use Response write if you don’t need to, it messes up a lot of things, like rendering and stylesheets. Instead maybe think about setting text of a label and displaying that label (Visible = true) when you need to. 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);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x