-
HiddenVariable Fields
I'm trying to use Hidden Variables for the first time.
But running into a problem
Everytime i run this code and enter my username, it tells me that the key has already been added.
I'm just learning C# right now and actually this is a code thats almost identical to that from a book which is why im confused why it isnt working
I work on Visual Studio .NET
Here is the code for the html:
<%@Page Inherits="practiceCoding.MobileWebForm1" AutoEventWireup="true" Language="c#" CodeBehind="TextBoxExample.aspx.cs" %>
<%@Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %>
<mobile:Form id="Form1" runat="server">
<mobile:Label id="Label1" runat="server">Your name</mobile:Label>
<mobile:TextBox id="TextBoxName" runat="server"></mobile:TextBox>
<mobile:Command id="Command1" runat="server">Submit</mobile:Command>
</mobile:Form>
<mobile:Form id="Form2" runat="server">
<mobile:Label id="Label2" runat="server">Your email</mobile:Label>
<mobile:TextBox id="TextBoxEmail" runat="server"></mobile:TextBox>
<mobile:Command id="Command2" runat="server">Submit</mobile:Command>
</mobile:Form>
<mobile:Form id="Form3" runat="server">
<mobile:TextView id="TextView1" runat="server">TextView</mobile:TextView>
</mobile:Form>
Here is my .cs file
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace practiceCoding
{
/// <summary>
/// Summary description for MobileWebForm1.
/// </summary>
public class MobileWebForm1 : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.Form Form1;
protected System.Web.UI.MobileControls.Form Form2;
protected System.Web.UI.MobileControls.Form Form3;
protected System.Web.UI.MobileControls.Label Label1;
protected System.Web.UI.MobileControls.TextBox TextBoxName;
protected System.Web.UI.MobileControls.Command Command1;
protected System.Web.UI.MobileControls.Label Label2;
protected System.Web.UI.MobileControls.TextBox TextBoxEmail;
protected System.Web.UI.MobileControls.Command Command2;
protected System.Web.UI.MobileControls.TextView TextView1;
private void Page_Load(object sender, System.EventArgs e)
{
}
public MobileWebForm1()
{
Page.Init += new System.EventHandler(Page_Init);
}
private void Page_Init(Object sender, EventArgs e)
{
HiddenVariables.Clear();
InitializeComponent();
}
private void InitializeComponent()
{
this.Command1.Click += new System.EventHandler(this.Command1_Click);
this.Command2.Click += new System.EventHandler(this.Command2_Click);
this.Form3.Activate += new System.EventHandler(this.Form3_Activate);
this.Load += new System.EventHandler(this.Page_Load);
}
private void Command1_Click(object sender, System.EventArgs e)
{
HiddenVariables.Add(TextBoxName.ID,TextBoxName.Text);
this.ActiveForm=Form2;
}
private void Command2_Click(object sender, System.EventArgs e)
{
HiddenVariables.Add(TextBoxEmail.ID, TextBoxEmail.Text);
this.ActiveForm=Form3;
}
private void Form3_Activate(Object sender, System.EventArgs e)
{
String FormData="";
foreach(Object o in HiddenVariables.Keys)
{
FormData+=o.ToString()+" "+HiddenVariables[0]+"<br>";
}
TextView1.Text=FormData;
}
}
}
Here is my error
Item has already been added. Key in dictionary: "TextBoxName" Key being added: "TextBoxName"
The error is on line 59 which reads:
HiddenVariables.Add(TextBoxName.ID,TextBoxName.Text);
Thanx a lot !
-
that happens when u try to add to an hashtable a key that already exists, you can't have duplicated keys, why is it happening i dont know as right now i dont have the time to look carefully at ur code
-
Thanx, i dont think i figured out why it was happening but i found a way around it, thanx for the help