繁体   English   中英

使用c#从asp.net中的文本框获取内容

[英]getting the content from textbox in asp.net using c#

我创建了两个动态文本框。 像这样...

protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1 = new TextBox();
        TextBox1.ID = "TextBox1";
        TextBox1.Style["Position"] = "Absolute";
        TextBox1.Style["Top"] = "25px";
        TextBox1.Style["Left"] = "100px";

        Panel1.Controls.Add(TextBox1);

        TextBox2 = new TextBox();
        TextBox2.ID = "TextBox2";
        TextBox2.Style["Position"] = "Absolute";
        TextBox2.Style["Top"] = "60px";
        TextBox2.Style["Left"] = "100px";
        Panel1.Controls.Add(TextBox2);
       //this.TextBox1.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
       //this.TextBox2.TextChanged += new System.EventHandler(this.TextBox_TextChanged);

            }

now, i want to retrieve the content from the textboxes. please tell immediately how to do this .

您可以找到控件并获取文本,例如以下示例:

string s = null;
TextBox control = FindControl("TextBox2") as TextBox;

if( control != null )
{
     // The content is here:
     s = control.Text;
}

如果您没有使用MasterPage,则可以询问请求:

string s = Request.Params["TextBox2"];

对不起,我没有读过第一个代码(格式不正确)。 您已经为TextBoxChanged事件增值,在EventHandler中您可以使用“sender”:

TextBox control = sender as TextBox;
string s = null;
if(control != null)
   s = control.Text;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM