代码如下:private void lbtnCheck_Click(object sender, System.EventArgs e)
{
  Response.Write("<script>alert('" + txtNickname.Text.ToString().Length + "')</script>");
}我在 txtNickname 中输入英文能准确的给出长度,但如果输入中文的话就是 0

解决方案 »

  1.   

    你的主要目的是干什么?把txtNickname.Text的值存如数据库???
      

  2.   

    string txtname=txtNickname.Text.tostring();
    监视其值!
    不可能出现你那种情况的!
      

  3.   

    大家可以帮我测试一下。地址是:http://www.real-weboy.com/rwsgb/web/leaveword.aspx昵称那一栏下面的那个“检测此昵称是否可用”就是用来检测长度的。大家可以试试!
      

  4.   

    还有哦,加个Trim()是不是会更好点呢?
      

  5.   

    把ToString()去掉.直接用Text试试!
      

  6.   

    hehe!你是怎么写的,写出来看看!不对呀!我点击了,如果是中文就会乱码。
      

  7.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Configuration;
    using RWSGB.AccessDAL;namespace RWSGB.Web
    {
    /// <summary>
    /// leaveword 的摘要说明。
    /// </summary>
    public class leaveword : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox txtNickname;
    protected System.Web.UI.WebControls.TextBox txtPassword;
    protected System.Web.UI.WebControls.TextBox txtRePassword;
    protected System.Web.UI.WebControls.TextBox txtHeadPage;
    protected System.Web.UI.WebControls.TextBox txtEmail;
    protected System.Web.UI.WebControls.TextBox txtQQ;
    protected System.Web.UI.WebControls.TextBox txtMSN;
    protected System.Web.UI.WebControls.TextBox txtICQ;
    protected System.Web.UI.WebControls.TextBox txtTitle;
    protected System.Web.UI.WebControls.TextBox txtContent;
    protected System.Web.UI.WebControls.LinkButton lbtnCheck;
    protected System.Web.UI.WebControls.LinkButton lbtnPrivate;
    protected System.Web.UI.WebControls.Button btnSubmit;
    protected System.Web.UI.WebControls.Button btnCancel;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!(IsPostBack))
    {
    txtPassword.Enabled=false;
    txtRePassword.Enabled=false;
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.lbtnCheck.Click += new System.EventHandler(this.lbtnCheck_Click);
    this.lbtnPrivate.Click += new System.EventHandler(this.lbtnPrivate_Click);
    this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
    this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnSubmit_Click(object sender, System.EventArgs e)
    {
    if(txtNickname.Text=="" || txtTitle.Text=="" || txtContent.Text=="")
    {
    Response.Write("<script>alert('请将  昵称、标题、内容  填写完整!')</script>");
    return;
    } if(txtPassword.Text.ToString()!=txtRePassword.Text.ToString())
    {
    Response.Write("<script>alert('两次输入的密码不同!')</script>");
    return;
    } Leaveword lw = new Leaveword();
    lw.Add(
    ManageString(0,txtNickname.Text),ManageString(0,txtPassword.Text),
    ManageString(0,txtTitle.Text),ManageString(0,txtContent.Text),
    ManageString(0,txtHeadPage.Text),ManageString(0,txtEmail.Text),
    ManageString(0,txtQQ.Text),ManageString(0,txtMSN.Text),
    ManageString(0,txtICQ.Text),Request.UserHostAddress.ToString(),DateTime.Now
    );
    } private void lbtnPrivate_Click(object sender, System.EventArgs e)
    {
    if (lbtnPrivate.Text=="我<想>使用密码保护我的昵称")
    {
    txtPassword.Enabled=true;
    txtRePassword.Enabled=true;
    lbtnPrivate.Text="我<不想>使用密码保护我的昵称";
    }
    else
    {
    txtPassword.Enabled=false;
    txtRePassword.Enabled=false;
    lbtnPrivate.Text="我<想>使用密码保护我的昵称";
    }
    } private void btnCancel_Click(object sender, System.EventArgs e)
    {
    Response.Redirect("index.aspx");
    } private void lbtnCheck_Click(object sender, System.EventArgs e)
    {
    Response.Write("<script>alert('" + txtNickname.Text.ToString() + "')</script>");
    } public static string ManageString(int intMode,string strString)
    {
    if(intMode==0)
    {
    strString=HttpContext.Current.Server.HtmlEncode(strString);
    strString=strString.Replace(" ","&nbsp;");
    strString=strString.Replace("\r\n","</br>");
    strString=strString.Replace("'","''"); return strString;
    }
    else
    {
    strString=strString.Replace("&nbsp;"," ");
    strString=strString.Replace("</br>","\r\n");
    strString=strString.Replace("''","'");
    strString=HttpContext.Current.Server.HtmlDecode(strString); return strString;
    }
    }
    }
    }
      

  8.   

    我觉的你的弹出框用的方式不对.
    改成这样Page.RegisterStartupScript("p","<script>alert('错误!请重新尝试!');</script>");试试
      

  9.   

    不关这个事的。关键是 TextBox 不能检测到中文。那如果我不写弹出提示,直接输入数据库的话。那数据库就是空的!
      

  10.   

    我看应该不是逻辑方面的错误,应该是哪儿设置没有弄好的原因。我的 index.aspx 也出现了这个问题!
      

  11.   

    非常感谢你的热心帮助:a7lang(阿七(程序员要做多好才有资格娶老婆?))请看此贴:http://community.csdn.net/Expert/topic/3687/3687045.xml?temp=9.934634E-02
      

  12.   

    很奇怪的问题:)请把ResponseEncoding="gb2312" 去掉就OK。
    这个问题以前碰到过。
    以下是完整源码:<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    <script language="c#" runat="server">
    private void Page_Load(object sender, System.EventArgs e)
    {
      Response.Write("<script>alert('" + txtNickname.Text.ToString().Length + "')</"+"script>");
    }
    </script>
    </head>
    <body>
    <form runat="server">
      <asp:TextBox ID="txtNickname" Text="你好" TextMode="SingleLine" runat="server" />
    </form>
    </body>
    </html>