Yasserzaid’s Weblog

December 26, 2008

Change TextBox Clor if it is Empty

Filed under: ASP.Net, Javascript — Tags: — yasserzaid @ 6:16 pm

Hi

try these examples to Change Color of Textbox if it is empty

First Example:

<script language=”javascript” type=”text/javascript”>
    function check1(source, args)
        {
            var txt = document.getElementById(‘<%= txt_Name.ClientID %>’);
            if(args.Value.length == 0)
            {
                txt.style.background = ‘#f1f4fb’;
                args.IsValid = false;
            }
            else
            {
                txt.style.background = ‘#ffffff’;
                args.IsValid = true;
            }
        }
        
    </script>

<asp:TextBox ID=”txt_Name” runat=”server” BorderStyle=”Dotted” BorderColor=”#C0C0FF” Width=”204px”></asp:TextBox>&nbsp;<br />
                        <asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ControlToValidate=”txt_Name”
                            ErrorMessage=”Your name is required.” Font-Size=”9pt”></asp:RequiredFieldValidator>
                            <asp:CustomValidator ID=”CustomValidator1″ runat=”server”
                                ClientValidationFunction=”check1″ ControlToValidate=”txt_Name” ValidateEmptyText=’true’
                                ErrorMessage=”CustomValidator” Font-Size=”9pt” Display=”None”></asp:CustomValidator>

Second Example:

<script type=”text/javascript”>
function DoClick(txtId) {
    var textbox = document.getElementById(txtId);
    if (textbox.value == “”) {
        textbox.style.backgroundColor = ‘#ff0000′;
    } else {
        textbox.style.backgroundColor = ‘#ffffff’;
    }
}
</script>
<asp:TextBox runat=”server” ID=”txtTextBox” Text=”Type Here” />
<asp:Button runat=”server” ID=”btnButton” Text=”Click Me” />

in code behind:

protected void Page_Load(object sender, Eventargs e)
{
    btnButton.OnClientClick = “DoClick(‘” + txtTextBox.ClientID + “‘)”;
}

Third Example:

<asp:TextBox style=”border: 1px solid #7F9DB9″ onkeyup=”if(this.value==”)this.style.backgroundColor=’#ff0000′;else this.style.backgroundColor=’#ffffff’;” Text=”Type Here” />

Hope it helps….

Good Luck

Check UserName Availability

Filed under: ASP.Net — yasserzaid @ 12:19 pm

Hi

try this example to Check UserName availability using Membership via Code:

 //– To check user name availability
            if (txt_User.Text.Length != 0)
            {
                MembershipUser user = Membership.GetUser(txt_User.Text);
                if (user != null)
                {
                    lblMsg.Text = “User ” + txt_User.Text + ” is already Exist”;
                    lblMsg.ForeColor = Color.Red;
                }
                else
                {
                    lblMsg.Text = “User ” + txt_User.Text + ” is available”;
                    lblMsg.ForeColor = Color.Green;
                }
            }
            else
            {
                lblMsg.Text = “Please Enter user Name”;
                lblMsg.ForeColor = Color.Red;

            }

Good Luck

Set focus to the UserName textbox of the Login control

Filed under: ASP.Net — Tags: — yasserzaid @ 11:15 am

Try this example:

TextBox userName = Login1.FindControl(“UserName”) as TextBox;

if (userName != null)
   Page.SetFocus(userName);

Good Luck

Session Timeout Issues

Filed under: ASP.Net — Tags: — yasserzaid @ 11:13 am

There are several methods to solve this issue.  We will look into

1. Setting up <authentication> section in web.config;

2. Defining the Session_End() handler

1. <authentication> section

<authentication mode=”Forms”>
  <forms loginUrl=”login.aspx” defaultUrl=”home.aspx” timeout=”20″ />
</authentication><authorization><deny users=”?”/></authorization>Based on the scenario you have described, user will need to login in order to use your web app.  Therefore, you could utilize the <authentication> section by specifying the loginUrl so that the user will get redirected to the login page if he/she Request -or- Postback data to the server.  Apart from that, notice that I have also included <authorization> section, it ensures that all unauthenticated users are denied, hence, redirecting to the loginUrl. 

2. Handling Session_End() in Global.asax

protected void Session_End(Object sender, EventArgs e)
{
   Response.Redirect(FormsAuthentication.LoginUrl); //LoginUrl is obtained form the Web.config <authentication>
}

Good Luck

Access Control in Master Page

Filed under: ASP.Net — Tags: , — yasserzaid @ 11:12 am

Hi

try this example:

Menu mainMenu = (Menu)this.Master.FindControl(“mainmenu”);
if (mainMenu != null)
{
      MenuItem item = new MenuItem();
      item.Text=”Home”;
      item.NavigateUrl=”~/home.aspx”;
      mainMenu.Items.Add(new MenuItem(“Home”, “0″));
}

Good Luck

Blog at WordPress.com.