2012-10-06 36 views
0

所以我试图让用户输入信息后点击一个按钮显示。我收到了这条消息,但它显示了输入到文本框中的值。我错过了什么? 这里是我的代码可以从字符串得到vlaue

private string _eyecolor; 
    public string Eyecolor 
    { 
     get { return _eyecolor; } 
     set 
     { 
      if (!string.IsNullOrEmpty(value)) 
      { 
       _eyecolor = value.Substring(0, 1).ToUpper() + value.Substring(1); 
      } 
      else 
      { 
       _eyecolor = value; 
      } 
     } 
    } 

    public string getEyeColor() 
    { 
     return "You have " + _eyecolor + "eyes!!"; 
    } 

这里是我的html代码:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <asp:Label runat="server" ID="lbl_1" AssociatedControlID="txtb1" Text="what is your eyes color?" Autopostback="true" /> 
    <asp:TextBox ID="txtb1" runat="server" /> 
    <asp:Button ID="btn_submit" Text="Submit" runat="server" OnClick="subSubmit" /> 
    <asp:Label runat="server" ID="lbl_output" /> 
    </div> 
    </form> 
</body> 
</html> 

和我隐藏:

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

public partial class _Default : System.Web.UI.Page 
{ 
    Profile P = new Profile(); 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
protected void subSubmit(object sender, EventArgs e) 
{ 
    lbl_output.Text=P.getMsg(); 
    lbl_output.Text+=P.getEyeColor(); 
} 
} 
+0

此刻你错过任何形式的代码注入到任何一个文本框。 –

+0

我们需要看到背后的代码(整个事情,以及在前面的代码,如果这是web表单(了'.aspx'和'aspx.cs'。 –

+0

我已经发布的一切吧。请看到我很想念 – Sarahfromnowhere

回答

0

由于阿迪尔正确地指出,你需要抓住从<asp:TextBox>输入值,并将其分配给您的财产Eyecolor

由于getEyeColorProfile对象P在您发布的代码的功能,请尝试更改您提交处理程序如下:

protected void subSubmit(object sender, EventArgs e) 
{ 
    P.Eyecolor = txtb1.Text.Trim; //Add this to set the Eyecolor to user input. 
    lbl_output.Text = P.getMsg(); 
    lbl_output.Text += P.getEyeColor(); 
} 
+0

它工作后,我用你建议的线,但我用ToString()而不是修剪。 – Sarahfromnowhere

+0

'.Text'已经是'String'类型,所以你很有效y调用'String.ToString()'哪个(虽然不是有害的)是多余的。我把修剪放在那里,所以你不会像“绿色”一样以眼睛颜色结束 – pete

1

你需要一些输入控件,你必须将其值赋给一些GUI控制显示。

在HTML

<asp:TextBox id="txtInput" runat="server" Text="Blue" ></asp:TextBox> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> 
<asp:Label id="lbl" runat="server" ></asp:Label> 

在后面的代码

protected void Page_Load(object sender, EventArgs e) 
{ 
    Eyecolor = "Blue"; 
    lbl.Text = getEyeColor(); 
} 


protected void Button1_Click(object sender, EventArgs e) 
{ 
     lbl.Text = txtInput.Text; 
} 

作为问题的更新。

您有来自用户的输入,但在输出不使用它。 You need to show txtb1 text in lbl_1 Text。这可以通过声明来完成

lbl.Text = txtInput.Text;

您正在使用配置文件类对象和你没有指定颜色配置文件类对象的属性if you do not want txtb1 color then you have to assign color to profile object before assigning to lbl_1 text不多大意义,但对于了解你需要的东西等。

protected void subSubmit(object sender, EventArgs e) 
{ 
    //lbl_output.Text=P.getMsg(); 
    P.Eyecolor = "Blue"; 
    lbl_output.Text+=P.getEyeColor(); 
} 
+0

我已经有HTML文件和代码隐藏文件我只贴类文件messae显示的onClick应该包含的眼睛颜色 – Sarahfromnowhere

+0

我。更新我有更多一点的解释回答。请仔细阅读,并告诉我,如果您有任何疑问。 – Adil

+0

太好了。谢谢你的解释。我想有在味精使用的用户输入。 – Sarahfromnowhere