简体   繁体   English

如何引用 HTML 的输入<textarea>控制代码隐藏?

[英]How do I reference the input of an HTML <textarea> control in codebehind?

I'm using a textarea control to allow the user to input text and then place that text into the body of an e-mail.我正在使用 textarea 控件来允许用户输入文本,然后将该文本放入电子邮件正文中。 In the code behind, what is the syntax for referencing the users input?在后面的代码中,引用用户输入的语法是什么? I thought I could just use message.Body = test123.Text;我以为我可以只使用message.Body = test123.Text; but this is not recognized.但这不被承认。

HTML: HTML:

<textarea id="TextArea1" cols="20" rows="2" ></textarea>

CodeBehind:代码隐藏:

foreach (string recipient in recipients)
{         
  var message = new System.Net.Mail.MailMessage("sender@example.com", recipient);
  message.Subject = "Hello World!";         
  message.Body = test123.Text;                
  client.Send(message); 
} 

You are not using a .NET control for your text area.您的文本区域没有使用 .NET 控件。 Either add runat="server" to the HTML TextArea control or use a .NET control:runat="server"添加到 HTML TextArea 控件或使用 .NET 控件:

Try this:试试这个:

<asp:TextBox id="TextArea1" TextMode="multiline" Columns="50" Rows="5" runat="server" />

Then reference it in your codebehind:然后在你的代码隐藏中引用它:

message.Body = TextArea1.Text;

You need to use runat="server" like this:您需要像这样使用runat="server"

<textarea id="TextArea1" cols="20" rows="2" runat="server"></textarea>

You can use the runat=server attribute with any standard HTML element, and later use it from codebehind.您可以将 runat=server 属性与任何标准 HTML 元素一起使用,然后在代码隐藏中使用它。

First make sure you have the runat="server" attribute in your textarea tag like this首先确保您的textarea标记中有runat="server"属性,如下所示

<textarea id="TextArea1" cols="20" rows="2" runat="server"></textarea>

Then you can access the content via:然后您可以通过以下方式访问内容:

string body = TextArea1.value;

You should reference the textarea ID and include the runat="server" attribute to the textarea您应该引用 textarea ID 并将runat="server"属性包含到textarea

message.Body = TextArea1.Text;  

What is test123 ?什么是test123


缺少属性runat="server"或在代码中使用Request.Params["TextArea1"]

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

相关问题 如何从代码隐藏访问用户控件的公共属性? - How do I access a public property of a User Control from codebehind? 如何从代码隐藏中在用户控件的内容控件中的数据模板中启动故事板? - How do I start a Storyboard in a Data Template in a Content Control in a User Control from codebehind? 如何访问代码隐藏 object 中的公共变量以进行 ascx 控件? (内联表达式) - How do I access a public variable in the codebehind object for an ascx control? (Inline Expression) 如何从代码隐藏传递对象以填充JQuery UI控件 - How do i pass an object from codebehind to populate a JQuery UI control 在共享webforms代码隐藏以进行用户控制时,如何访问公共属性 - How do I access public properties when sharing webforms codebehind for user control 如何将代码隐藏在多个文件中? - How do I place codebehind in multiple files? 如何在Codebehind中编写此十六进制渐变? - How do I write this Hex Gradient in Codebehind? 如何从代码隐藏中使用“输入类型=”提交” OnServerClick =” - How do use 'input type=“Submit” OnServerClick=' from codebehind 如何将DropDownList引用到代码隐藏中? - How to get reference of DropDownList into codebehind? 在代码隐藏中找不到HTML控件 - Can't find HTML control in codebehind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM