Skip to content

Instantly share code, notes, and snippets.

@Phuseos
Last active February 1, 2017 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phuseos/94fd32f10bbaf9a94650aab63c1b78a7 to your computer and use it in GitHub Desktop.
Save Phuseos/94fd32f10bbaf9a94650aab63c1b78a7 to your computer and use it in GitHub Desktop.
protected void TextBoxNullCatcher(object sender, EventArgs e)
{//Code to makes sure all controls that are textboxes have at least a '1' value.
if (sender is Control)
{
var usedControl = sender as Control; //Declare the control
if (usedControl is TextBox)
{
TextBox txtType = usedControl as TextBox; //Set the control as a textbox
if (string.IsNullOrEmpty(Convert.ToString(txtType.Text)))
{
txtType.Text = Convert.ToString("1");
/*
You can shorten it like this:
txtType.Text = string.IsNullOrEmpty(txtType.Text) ? "1" : txtType.Text;
Or txtType.Text = txtType.Text ?? "1";
*/
}
}
}
//Rest of your code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment