问题描述
我有一个用户控制哪些由带有一些额外的文本框,但是这个例子的目的只是假设它是一个普通的文本框就足够了。我打电话从Web窗体这个用户控件,并希望能够使用一个RequiredFieldValidator说,如果我用它在Web表单上的一个文本框基本上都会作用是相同的。如何配置我的用户控件来处理呢?
编辑:
DatePicker.ascx
< ASP:文本框=服务器ID =myControlTB>
DatePicker.ascx.cs
[ValidationProperty(文本)
公共部分类的DatePicker:System.Web.UI.UserControl
{
公共字符串文本{{返回myControlTB.Text; }集合{myControlTB.Text =价值; }}
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ }
}
WebForm.aspx
<铜:用户控件=服务器ID =显示myControl>
< ASP:的RequiredFieldValidator =服务器的ErrorMessage =这是一个必填字段。的ControlToValidate =显示myControl>
< ASP:按钮的ID =btnSubmit按钮=服务器文本=提交的onclick =btnSubmit_Click/>
WebForm.aspx.cs
保护无效btnSubmit_Click(对象发件人,EventArgs的发送)
{
Page.Validate();
如果(Page.IsValid)
{
//邮件形式
}
您需要设置控件上ValidationProperty属性和揭露文本框的文本属性作为控件属性
[ValidationProperty(文本)
公共部分类控制
{
公共字符串文本
{
{返回textbox.Text;}
} }
I have a User Control which consists of a TextBox with a few extras, but for purposes of this example just assuming it's a plain TextBox will be sufficient. I am calling this User Control from a Web Form and would like to be able to use a RequiredFieldValidator that basically would function the same as if I used it on a TextBox on the web form. How do I configure my User Control to handle this?
EDIT:
DatePicker.ascx
<asp:TextBox runat="server" ID="myControlTB">
DatePicker.ascx.cs
[ValidationProperty("Text")]
public partial class DatePicker : System.Web.UI.UserControl
{
public String Text { get { return myControlTB.Text; } set { myControlTB.Text = value; } }
protected void Page_Load(object sender, EventArgs e)
{
}
}
WebForm.aspx
<cu:UserControl runat="server" ID="myControl">
<asp:RequiredFieldValidator runat="server" errormessage="This is a required field." ControlToValidate="myControl">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
WebForm.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
// e-mail the form
}
You would need to set the ValidationProperty attribute on the control and expose the Textbox Text property as a control property
[ValidationProperty("Text")]
public partial class Control
{
public string Text
{
get { return textbox.Text;}
}
}
这篇关于处理用户控件中的RequiredFieldValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!