How to clear a textbox in asp.net? - c#

I tried TextBox1.Clear but it doesn't work and gives me error.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Clear
}

Do this way,
TextBox1.Text = string.Empty;

This could be one way to do it:
TextBox1.Text = string.Empty;

Related

How do I check the contents of a TextBox?

I have a textbox. In that textbox I write Human. Then I click a button, and if the word is human, then on a richtextbox, the word human will appear.
Here's the code I've tried.
private void button3_Click(object sender, EventArgs e)
{
if (textBox4.Text)
{
richTextBox1.Text = "human";
}
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
string value = textBox4.Text;
}
I tried making the textbox into a string so I could use it in the if statement, but it didn't work, so instead I used texbox4.text, but it is still wrong.
You could simply do with this piece of code,
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text =="human")
{
richTextBox1.Text = textBox1.Text;
}
}

Clear content from fields after save .net c#

I have aspx form with textboxes and gridview.
When i insert data to textbox it's shows on the gridview after clicking "Save".
I need to clear the content of fieds after i click on "Save".
I tried alot of things and searched in google but nothing works.
I am new at .net.. Can someone help please?
This is my save button fuction:
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
grdvUsers.DataBind();
}
This is my pageLoad:
protected void Page_Load(object sender, EventArgs e)
{
grdvUsers.DataSource = Context1.sp_GetAllUserTest(); // select all users into gridview. datasorce = the data we want to dispaly.
grdvUsers.DataBind();
}
Simply make a method for clearing the controls that you want to clear:
private void ClearControls()
{
txtName.Text =""; // resetting textbox
txtEmail.Tex="";
txtMobile.Text ="";
ddlSomeDropDown.SelectedIndex = -1; // reset dropdown
somecheckBox.Checked = false; // reset checkbox
someRadio.Checked = false; // reset radio
..................
.................
// more controls here
}
and call it after saving :
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
grdvUsers.DataBind();
ClearControls();
}
Just assign text fields a empty string.
protected void btnSave_Click(object sender, EventArgs e)
{
Context1.sp_CreateUserTest(txtName.Text, txtEmail.Text, txtMobile.Text, DateTime.Parse(txtBirthdate.Text));
txtName.Text = String.Empty;
txtEmail.Text= String.Empty;
txtMobile.Text= String.Empty;
txtBirthdate.Text= String.Empty;
grdvUsers.DataBind();
}
You can also do this.
You can clear date and datepicker with DatetimePicker1.Clear(); or with the provided ID

How to delete selected text from textbox in c#(SWF)

I try to do a notepad in c#,
I have some problems at delete function,
I want to delete selected text...
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a;
a = textBox1.SelectionLength;
textBox1.Text.Remove(textBox1.SelectionStart,a);
}
what is wrong?
Remove will return the truncated string, so you just need to reassign to the TextBox:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = textBox1.SelectionLength;
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart,a);
}
Use SelectedText like this :
textbox1.SelectedText = "";

Why # not work in query string?

In cs.aspx page i have a button with following code:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/cs.aspx?p=ali#25");
}
In page_load i get query string and display it:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["p"] != null)
{
string p = Request.QueryString["p"];
Response.Write("p= "+p);
}
}
in query string:
p = ali#25
but in run time display
p = ali
why string after # not shown.
found a solution. use Server.UrlEncode:
Response.Redirect("~/cs.aspx?pass="+Server.UrlEncode("a#25"));

Messagebox.Show doesnt work because its a method group? C#

Hi I'm very new to C# and I was just writing a very basic code could anyone help me out? I am trying to simply show a variable (inputted by the user via the textbox) show up in the message box... Thanks :)
private void button1_Click(object sender, EventArgs e)
{
string name1 = textBox1.Text;
MessageBox.Show = (name1);
textBox1.Text = ("");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string name1 = textBox1.Text;
}
It's a method, not a property, so you need to call it using this syntax
MessageBox.Show(name1);
Related, textBox1.Text is a string property so you need to change that syntax to
textBox1.Text = "my string";
Your click event could be rewritten as this:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(textbox1.Text);
textBox1.Text = "";
}
and this section of code doesn't actually have any significance. name1 is only in scope inside the text changed event handler, and you do nothing with it.
private void textBox1_TextChanged(object sender, EventArgs e)
{
string name1 = textBox1.Text;
}

Categories

Resources