How do you use if (IsPostBack) in ASP.NET C# - c#

How can I used if (IsPostBack){} to display user's names from two text boxes into another text box?
I have 3 text boxes and a button. The text boxes are named txtLastName, txtGivenName, txtOutput. My button is btnSubmit.
How can I display text from the txtLastName and txtGivenName in the txtOutput text box?
How can I display it as: First (space) Lastname or Last, Firstname in this code.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
}
}

Create an event handler for the Click event on the button and then in the code behind, do like this:
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtOutput.Text = string.Format("{0} {1}", txtGivenName.Text, txtLastName.Text);
}

How can I display text from the txtLastName and txtGivenName in the
txtOutput text box?
Go to the design of your page.
Click the button.
Click F4 or Right click and select properties. This will show you a window for button.
Click the event.
Double click the "Click" action.
This will navigate you to the code behind.
Write the code in this handler
This is how the design will look like for your event handler
protected void btnSubmit_Click(object sender, EventArgs e)
{
txtOutput.Text = string.Format("{0} {1}", txtGivenName.Text, txtLastName.Text);
}
How can I display it as: First (space) Lastname or Last, Firstname in
this code.
Write down the code in the handler as below.
txtOutput.Text = txtLast.Text + ", " + txtFirst.Text;

Why don't use use the submit button method to achieve this?
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
txtOutput.Text = txtLast.Text + ", " + txtFirst.Text;
}

try this
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
txtOutput.Text = txtLastName.Text + " " + txtLastName.Text;
}

Related

Add the same string from textbox in two lines listbox?

Is it possible to add the same string typed in the texbox in the two lines of the listbox, as shown in the image below?
private void metroButton10_Click(object sender, EventArgs e)
{
listBox2.Items.Add("Next station: " + metroTextBox1.Text);
listBox2.Items.Add(Environment.NewLine)
metroTextBox1.Clear();
listBox2.SelectedIndex = 0;
}
Use the following code.
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("Next Station: "+textBox1.Text);
listBox1.Items.Add("Station: " + textBox1.Text);
}
You Can't use new line. Because listbox can contain one item in a line.
Sure you can add same string typed in the textbox in the two lines of the listbox as;
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add("NextStation: " + textBox1.Text);
listBox1.Items.Add("Station: " +textBox1.Text);
}

Click Event Handler of ASP.Net Button in User Control is not Working

My User Control in my ASP.Net application has a Button named button. I've added a click event to it like this:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
button.Click += (object o, EventArgs ea) => {
Response.Redirect("~/Post.aspx?type=" + Type + "&id=" + PostID);
};
}
}
But whenever I click on the Button it does nothing but reloads the page. What is the problem with my code?
You should remove the if (!IsPostBack) condition to set the event handler on every postback:
protected void Page_Load(object sender, EventArgs e) {
button.Click += (object o, EventArgs ea) => {
Response.Redirect("~/Post.aspx?type=" + Type + "&id=" + PostID);
};
}

jump into new line after enter click inside TextBox with multi line

I have a Textbox with MultiLine enabled, in my application this Textbox controller used to insert some text.
All I want to do is to jump to a new line if the user clicks enter.
All I have tried is to find the write command inside my controller Enter event:
private void tbc_Enter(object sender, EventArgs e)
{
}
Is this what you want?
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "This" + Environment.NewLine + "A" + Environment.NewLine + "Multiline" + Environment.NewLine + "Textbox.";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.AppendText(Environment.NewLine);
textBox1.Focus();
}

how to create dynamic link labels in window form application

On clicking Go button the name given in textbox should be displayed as a link label and this should increment dynamically.
Here my code:
private void buttongo_Click(object sender, EventArgs e)
{
linkLabelName.Text = textBoxName.Text;
}
private void btnGo_Click(object sender, EventArgs e)
{
LinkLabel link = new LinkLabel();
link.Text = txtText.Text;
panTable.Controls.Add(link);
}

Retain the message in the textbox when the checkbox is still checked

I have 3 checkboxes with corresponding message in a textbox. My teacher wants the message to remain in the textbox when the checkbox is still checked and hide the text when it is unchecked. In my case when I checked the 3 checkboxes their 3 corresponding messages will appear but when I unchecked one of the checkboxes and the other two are still checked, all the message will disappear. My problem is when I unchecked one of the checkbox and and the other 2 are still checked the corresponding messages with the remaining two checked checkboxes will remain in their textboxes.
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
if (chkCarWheels.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.hasWheels(4);
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
if (chkCarAcceleration.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.Accelerate();
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
if (chkCarBreakpad.Checked == true)
lblMessage.Text = lblMessage.Text + mycar.hasBreak();
else
lblMessage.Text = "My " + txtName.Text + " Car";
}
Looks like you need to create message depending on checkboxes states. You can create method, which will do the job and call it when state of some checkbox changed.
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
Or the better one - create one event handler for all checkboxes:
// use for chkCarWheels, chkCarAcceleration, chkCarBreakpad
private void chkCar_CheckedChanged(object sender, EventArgs e)
{
BuildMessage();
}
private void BuildMessage()
{
lblMessage.Text = "My " + txtName.Text + " Car";
if (chkCarWheels.Checked)
lblMessage.Text = lblMessage.Text + mycar.hasWheels(4);
if (chkCarAcceleration.Checked)
lblMessage.Text = lblMessage.Text + mycar.Accelerate();
if (chkCarBreakpad.Checked)
lblMessage.Text = lblMessage.Text + mycar.hasBreak();
}
You don't need to compare boolean values with true/false. Use those values directly if (chkCarWheels.Checked). And keep in mind that in C# we use CamelCase names form methods. Also consider to use StringBuilder to build whole message and then assign it to label:
private void BuildMessage()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("My {0} Car", txtName.Text);
if (chkCarWheels.Checked)
sb.Append(mycar.hasWheels(4));
if (chkCarAcceleration.Checked)
sb.Append(mycar.Accelerate());
if (chkCarBreakpad.Checked)
sb.Append((mycar.hasBreak());
lblMessage.Text = sb.ToString();
}
Try this:
private void chkCarWheels_CheckedChanged(object sender, EventArgs e)
{
chkCar();
}
private void chkCarAcceleration_CheckedChanged(object sender, EventArgs e)
{
chkCar();
}
private void chkCarBreakpad_CheckedChanged(object sender, EventArgs e)
{
chkCar()
}
private void chkCar()
{
string msg="";
if (chkCarWheels.Checked)
msg=msg+mycar.hasWheels(4);
if(chkCarAcceleration.Checked)
msg=msg+mycar.Accelerate();
if(chkCarBreakpad.Checked)
msg=msg+mycar.hasBreak();
lblMessage.Text=msg;
}

Categories