Stc5097
Stc5097

Reputation: 289

why is my page passing a blank value for my textBox

I asked this question last night but it was not very well written so I am going to ask it again. I am creating a simple calculator using ASP.NET and c# as my code behind. I am currently just trying to test and make sure that my code behind is getting the value typed entered into the textbox by the user. I put in a if statement that assigns the textbox a value of mehhh if the string it gets passed it empty. My text box displays mehhh so I knoe it is getting an empty string but i am not sure why? here is a link to the site... http://scort323.csweb.kutztown.edu/Calc.aspx Below is my code for the code behind part of my page:

  public partial class Assign2_Calc : System.Web.UI.Page
  {
     protected void ButtonEqual_Click(object sender, EventArgs e)
     {
       string inputStr = inputBox.Text;
       if (inputStr == string.Empty)
       {
         inputBox.Text = "mehhhhhh";
       }
       else
       {
      inputBox.Text = inputStr; //result.ToString();
    }
  }

 }  

below is my .aspx page:

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calc.aspx.cs"    
      Inherits="Assign2_Calc" Debug="true" %>

      <!DOCTYPE html>






     <html xmlns="http://www.w3.org/1999/xhtml">
     <head id="Head1" runat="server">
     <title></title>
     <link href="CalculatorStyle.css" rel="stylesheet" type="text/css" />

     <script>
     var maxInputLength = 20;

     function checkButtonClick(clickedValue) {
      var buttonValue = clickedValue;
      var inputStr = document.getElementById('inputBox').value;

      if (buttonValue == '<--') {
          if (inputStr.length >= 1) {
              document.getElementById('inputBox').value = inputStr.substring(0,      inputStr.length - 1);

          }
      }

      else if (buttonValue == 'C') {
          document.getElementById('inputBox').value = "";
      }
      else {
          if (inputStr.length < maxInputLength) {
              document.getElementById('inputBox').value = inputStr + buttonValue;
          }
          else {
              //document.getElementById('msg').innerHTML = "Maxmum length is " +  maxInputLength;
          }
      }
      return false;
  }

  </script>
  </head>

<body>
<form id="form1" runat="server">
<div>
    <div id="main" class="main">
        <div id="content" class="content">
            <h3 id="h3">Simple Calculator</h3>
                <div id="calculatorDiv">
                    <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td colspan="4">
                           <asp:TextBox runat="server" CssClass="inputBox" ReadOnly="true" ViewStateMode="Enabled" ID="inputBox"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="ButtonNum7" runat="server" Text="7" CssClass="CalcButtons" OnClientClick="return checkButtonClick(7)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonNum8" runat="server" Text="8" CssClass="CalcButtons" OnClientClick="return checkButtonClick(8)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonNum9" runat="server" Text="9" CssClass="CalcButtons" OnClientClick="return checkButtonClick(9)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonDivide" runat="server" Text="/" CssClass="CalcButtons" OnClientClick="return checkButtonClick('/')" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="ButtonNum4" runat="server" Text="4" CssClass="CalcButtons" OnClientClick="return checkButtonClick(4)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonNum5" runat="server" Text="5" CssClass="CalcButtons" OnClientClick="return checkButtonClick(5)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonNum6" runat="server" Text="6" CssClass="CalcButtons" OnClientClick="return checkButtonClick(6)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonMultiply" runat="server" Text="*" CssClass="CalcButtons" OnClientClick="return checkButtonClick('*')" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="Button1" runat="server" Text="1" CssClass="CalcButtons" OnClientClick="return checkButtonClick(1)" />
                        </td>
                        <td>
                            <asp:Button ID="Button2" runat="server" Text="2" CssClass="CalcButtons" OnClientClick="return checkButtonClick(2)" />
                        </td>
                        <td>
                            <asp:Button ID="Button3" runat="server" Text="3" CssClass="CalcButtons" OnClientClick="return checkButtonClick(3)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonSubtract" runat="server" Text="-" CssClass="CalcButtons" OnClientClick="return checkButtonClick('-')" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="ButtonDackspace" runat="server" Text="<--" CssClass="CalcButtons" OnClientClick="return checkButtonClick('<--')" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonNum0" runat="server" Text="0" CssClass="CalcButtons" OnClientClick="return checkButtonClick(0)" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonClear" runat="server" Text="C" CssClass="CalcButtons" OnClientClick="return checkButtonClick('C')" />
                        </td>
                        <td>
                            <asp:Button ID="ButtonAdd" runat="server" Text="+" CssClass="CalcButtons" OnClientClick="return checkButtonClick('+')" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <asp:Button ID="ButtonEqual" runat="server" Text="=" CssClass="CalcButtonEqual" OnClick="ButtonEqual_Click" />
                        </td>
                    </tr>
                </table>
                </div>
        </div>
    </div>
</div>
</form>
</body>
</html>

Upvotes: 0

Views: 2943

Answers (2)

EdSF
EdSF

Reputation: 12341

MSDN doc on ReadOnly:

The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.

You are "manipulating" the value in client script (not in server code - assuming above is all the code). The original value of the TextBox is preserved (empty).

If you test by removing the ReadOnly attribute (or set it to False), your code will work and you will see the effect of the setting...

Hth....


Update:

..how would I need to go about making it so the user cant use the keyboard to enter anything? making it so they must use the buttons

Unless you have/had a reason to use a server-side control for that input field, a standard HTML Input field with readonly should work.

You will then obtain it's value from the standard POST in the Request (at the end of the day, WebForms is still an HTTP POST), instead of ASP.Net controls.

Trivial example:

Instead of server-control:

<asp:TextBox runat="server" CssClass="inputBox" ReadOnly="true" ViewStateMode="Enabled" ID="inputBox"></asp:TextBox>

Use plain html input field:

<input id="inputBox" name="inputBox" readonly type="text" />

The value of the field (html_readonly) can be obtained in the POST Request:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        string _html = Request["inputBox"]; //here it is

        //do whatever...

    }
}

Hth...

Upvotes: 1

Shashank Chaturvedi
Shashank Chaturvedi

Reputation: 2793

Looking at the source of your web page I found that you have disabled your textbox. If a control is disabled it cannot be edited and its content is excluded when the form is submitted. So instead of disabling (remove disable attribute completely) your textbox set it to readonly (readonly = 'true').

Upvotes: 5

Related Questions