可以从字符串中得到vlaue吗?

时间:2023-02-05 20:58:58

So I am trying to make the user input information that displays after clicking a button. I get the message but it doenst show the value input into the textbox. What am I missing? here is my code

因此,我正在尝试让用户输入的信息在单击按钮后显示。我得到消息,但它将值输入显示在文本框中。我缺少什么?这是我的代码

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!!";
    }

Here is my html code:

这是我的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>

and my codebehind:

我的后台代码:

 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();
}
}

2 个解决方案

#1


0  

As Adil quite rightly pointed out, you need to grab the value from the <asp:TextBox> input and assign it to your property Eyecolor.

正如Adil正确指出的那样,您需要从 输入中获取值并将其分配给您的属性Eyecolor。

Since getEyeColor is a function of the Profile object P in your posted code, try changing your Submit handler to the following:

由于getEyeColor是在您的发布代码中为Profile对象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();
}

#2


1  

You need some input control and you have assign its value to some gui control to get it displayed.

您需要一些输入控件,并将其值赋给一些gui控件以使其显示。

In html

在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>

In Code behind

在代码后面

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;
}

As update in question.

更新的问题。

You are having input from user but not using it in the output. You need to show txtb1 text in lbl_1 Text. Which could be done by the statement

您正在从用户那里获得输入,而不是在输出中使用它。您需要在lbl_1文本中显示txtb1文本。这可以通过声明来实现吗?

lbl.Text = txtInput.Text;

lbl。文本= txtInput.Text;

You are using Profile class object and you did not assign color to Profile class object property if you do not want txtb1 color then you have to assign color to profile object before assigning to lbl_1 text which does not make much sense but for understanding you need something like.

您正在使用概要类对象和你没有指定颜色配置文件类对象的属性,如果你不希望txtb1颜色然后你必须指定颜色配置文件对象分配lbl_1文本之前并没有多大意义,但对于理解你需要什么东西。

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

#1


0  

As Adil quite rightly pointed out, you need to grab the value from the <asp:TextBox> input and assign it to your property Eyecolor.

正如Adil正确指出的那样,您需要从 输入中获取值并将其分配给您的属性Eyecolor。

Since getEyeColor is a function of the Profile object P in your posted code, try changing your Submit handler to the following:

由于getEyeColor是在您的发布代码中为Profile对象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();
}

#2


1  

You need some input control and you have assign its value to some gui control to get it displayed.

您需要一些输入控件,并将其值赋给一些gui控件以使其显示。

In html

在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>

In Code behind

在代码后面

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;
}

As update in question.

更新的问题。

You are having input from user but not using it in the output. You need to show txtb1 text in lbl_1 Text. Which could be done by the statement

您正在从用户那里获得输入,而不是在输出中使用它。您需要在lbl_1文本中显示txtb1文本。这可以通过声明来实现吗?

lbl.Text = txtInput.Text;

lbl。文本= txtInput.Text;

You are using Profile class object and you did not assign color to Profile class object property if you do not want txtb1 color then you have to assign color to profile object before assigning to lbl_1 text which does not make much sense but for understanding you need something like.

您正在使用概要类对象和你没有指定颜色配置文件类对象的属性,如果你不希望txtb1颜色然后你必须指定颜色配置文件对象分配lbl_1文本之前并没有多大意义,但对于理解你需要什么东西。

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