ASP.net中文本框的占位符

时间:2016-04-02 06:11:50

标签: c# asp.net

需要一个占位符来显示文本框的文本,当我在文本框中单击或输入文本时,文本框会消失

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
 using System.Data;



 public partial class SIGNUPaspx : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data            Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\Documents\Visual Studio  2010\WebSites\my website\App_Data\GYMDatabase.mdf;Integrated Security=True;User Instance=True");
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select * from Client_Data Where C_Email='" + TextBox6.Text + "'";
    cmd.Connection = con;
    SqlDataReader rd = cmd.ExecuteReader();
    rd.Read();
    if (rd.HasRows)
    {
        Label1.Text = "Email Already Exist";
    }
    else
    {
        SqlConnection con1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Admin\Documents\Visual Studio 2010\WebSites\my website\App_Data\GYMDatabase.mdf;Integrated Security=True;User Instance=True");
        con1.Open();
        String str = "INSERT INTO Client_Data(C_FName,C_LName,C_Address,C_Gender,C_Email,C_Password,C_MobileNo,C_RegistrationCode) VALUES ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox8.Text + "','" + RadioButtonList1.Text + "','" + TextBox6.Text + "','" + TextBox4.Text + "','" + TextBox7.Text + "','" + TextBox5.Text + "')";
        SqlCommand cm = new SqlCommand(str, con1);
        String str1 = "INSERT INTO PaymentTable(C_Email) VALUES ('" +         TextBox6.Text + "')";
        SqlCommand cmp = new SqlCommand(str1, con1);
        cm.ExecuteNonQuery();
        cmp.ExecuteNonQuery();
        con.Close();
        Label1.Visible = true;
        Response.AddHeader("REFRESH", "3;URL=LOGIN.aspx");
    }
    rd.Close();
    con.Close();

}
private void TextBox1_Enter(object sender, EventArgs e)
{
    if (TextBox1.Text == "First Name")
    {
        TextBox1.Text = "";
    }
}
private void TextBox1_Leave(object sender, EventArgs e)
{

        if (TextBox1.Text == "")
        {
            TextBox1.Text = "First Name";
            TextBox1.ForeColor = System.Drawing.Color.LightGray;
        }
    }

}

2 个答案:

答案 0 :(得分:2)

您不需要从C#中完成所有这些工作。只需在TextBox

中添加占位符属性即可
<asp:TextBox ID="UserName" runat="server" CssClass="form-control" placeholder="Username or Email" Width="350"></asp:TextBox>

答案 1 :(得分:0)

enter code here
<%@ Page Title="SIGNUP" Language="C#" MasterPageFile="~/Master1.master"    AutoEventWireup="true" CodeFile="SIGNUP.aspx.cs" Inherits="SIGNUPaspx" %>

             注册........     

    <asp:TextBox ID="TextBox1" runat="server" 
        style="margin-left: 0px" Width="199px" ViewStateMode="Enabled" 
        ForeColor="#CCCCCC" >First Name</asp:TextBox>

                              宽度=“215px”&gt;最后一个名称                            地址              

<div align="center" style="padding: 10px">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
        RepeatDirection="Horizontal" RepeatLayout="Flow">
        <asp:ListItem>Male</asp:ListItem>
        <asp:ListItem>Female</asp:ListItem>
    </asp:RadioButtonList>
</div>

<div align="center" style="padding: 10px">

    <asp:TextBox ID="TextBox6" runat="server" Width="215px">Email</asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
        ControlToValidate="TextBox6" ErrorMessage="*" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</div>
<div align="center" style="padding: 10px">
    <asp:TextBox ID="TextBox3" runat="server" Height="24px" 
         Width="213px">PASSWORD</asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 
        ControlToValidate="TextBox3" ErrorMessage="*" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</div>
<div align="center" style="padding: 10px">
    <asp:TextBox ID="TextBox4" runat="server" Height="20px" 
         style="margin-left: 0px" Width="213px">CONFIRM PASSWORD</asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" 
        ControlToValidate="TextBox4" ErrorMessage="*" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</div>
<div align="center" style="padding: 10px">
    <asp:TextBox ID="TextBox7" runat="server" Width="215px">CONTACT NUMBER</asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
        ControlToValidate="TextBox7" ErrorMessage="*" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</div>
<div align="center" style="padding: 10px">
    <asp:TextBox ID="TextBox5" runat="server" Width="162px">REGISTRATION CODE</asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
        ControlToValidate="TextBox5" ErrorMessage="*" ForeColor="#FF3300"></asp:RequiredFieldValidator>
</div>
<div align="center" style="padding: 10px">
    <asp:Button ID="Button1" runat="server" BackColor="#006600" 
        BorderStyle="Groove" ForeColor="#000099" Height="31px" Text="SIGNUP" 
        Width="127px" onclick="Button1_Click" />
</div>
<div align="center" style="padding: 5px">
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
        ControlToValidate="TextBox6" ErrorMessage="Enter A valid Email Address" 
        ForeColor="#FF3300" 
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
    &nbsp;</div>
<div align="center" style="padding: 5px">
    <asp:Label ID="Label1" runat="server" Visible="False">Signup Successfull...</asp:Label>
</div>
<div align="center" style="padding: 5px">
    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
        ControlToValidate="TextBox7" ErrorMessage="Enter A valid Mobile Number" 
        ForeColor="#FF3300" ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
</div>
<div align="center" style="padding: 5px 5px 10px 5px">
    <asp:CompareValidator ID="CompareValidator1" runat="server" 
        ControlToCompare="TextBox3" ControlToValidate="TextBox4" 
        ErrorMessage="Passwords must be same..! " ForeColor="#FF3300"></asp:CompareValidator>
</div>
</asp:Content>