• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

From a Textbox in HTML page to a class

Hi,

I have a web application project. I need to know how I write the code when I have included a text box on the HTML page to get the text from it and also the opposite. To set the text.

Right know I have included a text box and a button. When I click the button I come to the ordinary page to write code so the button is no problem. But how do I call the textbox?. I cant find that it exist or anything?

Please explain how this is done.
0
Mickeys
Asked:
Mickeys
  • 4
  • 3
1 Solution
 
philipjonathanCommented:
How did you add the textbox to the HTML page, is it by adding:
<input type="text" ...>

if so, just make sure that it has ID and runat="server", eg:
<input type="text" id="txbMyTextBox" runat="server">

Then in your code-behind, add a protected member field, with the same name as the textbox, to the page class:

public class MyPage : Page
{
  ...
  protected System.Web.UI.HtmlControls.HtmlInputText txbMyTextBox;
  ...
}

To access or set the value, use txbMyTextBox.Value, eg:
txbMyTextBox.Value = "Hello World";
0
 
MickeysAuthor Commented:
I included asp textbox instead. And I maneged to include text in the text box and getting the infomation by doing this:

        protected void Button1_Click(object sender, EventArgs e)
        {
            temp.SendToServer(TextBox1.Text,TextBox2.Text, TextBox3.Text);
           
        }

But I havent managed to set the text.
I have a class that I want to be able to set the txt from.
Do I have to send the text I want to set over to the default.aspx and set it there or can I set it directly form my class?
0
 
philipjonathanCommented:
In that case, you need to somehow pass the reference of the textbox to that class. In the example below, I instantiate the class, and pass the reference.

protected void Button1_Click(object sender, EventArgs e)
{
  temp.SendToServer(TextBox1.Text,TextBox2.Text, TextBox3.Text);
  Class1 c = new Class1();
  c.SetText(TextBox1);
}


Then in Class1:
public class Class1
{
  ...
  public void SetText(System.Web.UI.WebControls.TextBox textbox)
  {
    textbox.Text = "Hello World";
  }
  ...
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MickeysAuthor Commented:
Could you show in my  code?
This is my code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public Class1 temp = new Class1();        
 
        protected void Page_Load(object sender, EventArgs e)
        {
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            temp.SendToServer(TextBox1.Text,TextBox2.Text, TextBox3.Text);            
        }
 
        public void SetAnswer(String message)
        {
            TextBox4.Text = message;
        }
    }
}
 
 
 
-----------------------------------
 
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace WebApplication1
{
    public class Class1
    {
        
        public void SendToServer(String namn, String efternamn, String pnr)
        {
            webservice_localhost.Service1 service = new webservice_localhost.Service1();
 
            WebApplication1.webservice_localhost.PersonData person = service.GetPersonData(namn, efternamn, pnr);
 
            if (person.pnr != null)
            {
                String message = person.forNamn;
                message = message + "\n";
                message = message + person.efterNamn;
                message = message + "\n";
                message = message + person.pnr;
                message = message + "\n";
 
                int i = 0;
                while (person.barnPnr[i] != null)
                {
                    message = message + person.barnForNamn;
                    message = message + "\n";
                    message = message + person.barnEfterNamn;
                    message = message + "\n";
                    message = message + person.barnPnr;
                    i++;
                }
 
                if (i == 0)
                    message = message + "inga barn";
 
 
 
 
 
            }
 
            else
            {
                String message = "Inga";
            }
           
        }
    }
}

Open in new window

0
 
MickeysAuthor Commented:
I want to call SetAnswer
0
 
philipjonathanCommented:
Do you mean: call SetAnswer from Class1.SendToServer?

Try this:
Change line #57 to:
public string SendToServer(String namn, String efternamn, String pnr)

Add after line #84:
return message;

Add after line #94:
return message;

Change line #26 to:
string message = temp.SendToServer(TextBox1.Text,TextBox2.Text, TextBox3.Text);
SetAnswer(message);

0
 
MickeysAuthor Commented:
STUIPED. It is to early for me.
Thanx
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now