Here goes the MesageBox function in C#, which we can call anytime. It takes the message text (string) as input and displays as in the figure above. The only trick involved is:
1. Create a new label from code behind
3. Add this label to to the page's control bag.
That's all. Now lets look into the function itself.
//the message box function
private void MessageBox(string msg)
Label lbl = new Label();
Now lets go with an example. We will have a form in which there is one textbox and one button. We will message user when undesired special character is detected in the textbox. Lets have the form markup like this [within the form].
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Your Name:"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="OK" onclick="Button1_Click" />
Now is the time for the event handler on button click.
//invoked when button btnOK is clicked
protected void Button1_Click(object sender, EventArgs e)
string name = txtName.Text;
MessageBox("You can not put special characters _, & or @ in your name.");
MessageBox("Your name is" + " " + name);
This is just another function for test purpose only, that validates the characters in the textbox.
//returns true if the string contains _,& or @
//otherwise returns false
public bool ValidateString(string str)
if((str.Contains("_") || (str.Contains("&"))||(str.Contains("@"))))
Your creative suggestions are always welcome! Happy programming!