简体   繁体   English

如何在ASP.NET中创建具有与表单的输入元素绑定的公共属性的WebControl?

[英]How can I create a WebControl in ASP.NET that has public properties tied to the input elements of a form?

In short, I want to create a WebControl that may appear multiple times in an input form. 简而言之,我想创建一个在输入表单中可能多次出现的WebControl。 The WebControl I've drafted out has properties that are assumed to be related to the values in the web control (text boxes etc.) What I want to be able to do is access the contents of the WebControl's input elements by name in my submit handler as below: 我草拟的WebControl具有假定与Web控件中的值相关的属性(文本框等)。我要能够做的是通过提交中的名称访问WebControl输入元素的内容。处理程序如下:

var firstname = customControl.FirstName;
var lastname = customControl.LastName;

I come from a WPF background so this is just familiar enough I am getting myself into trouble. 我来自WPF,所以对它的熟悉程度足以使我陷入麻烦。

This is off the top of my head (just getting back to .NET after a few months on other languages), and I'm assuming it's for a User control because Server controls are pretty well documented for properties. 这是我的首要任务(使用其他语言几个月后才回到.NET),并且我假设它是针对User控件的,因为Server控件的属性记录非常好。

First, assume your user control has textboxes txtFirst and txtLast . 首先,假设您的用户控件具有文本框txtFirsttxtLast

In the code-behind, add public properties FirstName and LastName. 在后面的代码中,添加公共属性FirstName和LastName。 The properties would look like this: 这些属性如下所示:

public string FirstName {
 get { return this.txtFirst.Text.Trim(); }
 set { this.txtFirst.Text = value ?? ""; }
}

public string LastName {
 get { return this.txtLast.Text.Trim(); }
 set { this.txtLast.Text = value ?? ""; }
}

That's it. 而已。 My apologies if you weren't asking about a user control. 如果您没有询问用户控件,我深表歉意。

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

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