Click here to Skip to main content
15,882,698 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This seems pretty straightforward .

I am trying to create a simple asp.net webform with textbox and button.

Whenever i should hit click button, the value in textbox should increment by 1 . Here's my code

C#
public partial class WebForm1 : System.Web.UI.Page
{
    int clickload = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            TextBox1.Text = "0";
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (ViewState["clicks"] != null)
        {
            clickload = (int)ViewState["Clicks"]+1;

        }

            TextBox1.Text = clickload.ToString();

            ViewState["clicks"] = clickload;


    }


But, when i run it. It shows me 1 correctly , but then it throws me this error

http://postimg.org/image/elosn84wr/[^]


can someone tell me whats wrong with the code?

Thanks
Posted

you have capital C in the Clicks (view state key name) but when you set you have simple c. make it simple and try again.
viewState Key Name is case sensitive
C#
if (ViewState["clicks"] != null)
{
  clickload = (int)ViewState["clicks"]+1;           
}
 
Share this answer
 
v2
Comments
hypermellow 22-May-15 6:05am    
Beat me to it! 5+
Hi,

I can't see the image that contains the error detail, but I suspect your issue is related to the ViewState Key being case sensitive.

C#
ViewState["Clicks"]

is not that same as:
C#
ViewState["clicks"]


C#
protected void Button1_Click(object sender, EventArgs e)
{
    if (ViewState["clicks"] != null)
    {
        clickload = (int)ViewState["clicks"]+1;
    }
    TextBox1.Text = clickload.ToString();
    ViewState["clicks"] = clickload;
}


... hope it helps.
 
Share this answer
 
why you use viewstate at all? it is not necessary, just use update panel to update the textbox with the new value
here is the code

code behind:

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           txtnumber.Text = "0";

       }
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
       int number;
       number = int.Parse(txtnumber.Text);
       number++;
       txtnumber.Text = number.ToString();

   }




html :


XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="txtnumber" runat="server"></asp:TextBox>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


let me know if it worked for you, mine was fine
good luck
 
Share this answer
 
v2
The reason seems to be the capital C in
ViewState["Clicks"]

However this can also be done without viewstate like below :

protected void Button1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(TextBox1.Text))
            {
                int clicks = 0;
                Int32.TryParse(TextBox1.Text ,out clicks);
                TextBox1.Text  = (clicks + 1).ToString();
            } 

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900