如何在页面上应用必填字段有效用户控件文本框? [英] How to apply required field validaiton user control textbox on the page?

查看:64
本文介绍了如何在页面上应用必填字段有效用户控件文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在页面上应用了一个说AddressSearchControl.ascx的用户控件.有一个名为扩展邮政编码的文本框.我已经在usercontrol中对此应用了reqular表达式,现在我想对此应用必需的字段验证器,但是我只想在页面上应用它,而不是在用户控件上应用.此文本框呈现为"ctl00_DefaultContent_AddressProviderBillingAddress_TextBoxExtendedZipCode".现在,plz指南如何使此文本框成为必需的.请尽快引导我,而不是像告诉我一个可怕的事情一样告诉我:)

Hi,
I have a usercontrol say AddressSearchControl.ascx, applied on a page. There is a textbox named Extended Zip code. I have applied reqular expression on this in usercontrol now I want to apply required field validator on this but I want to apply this on the page only instead of applying on the user control. This textbox renders as "ctl00_DefaultContent_AddressProviderBillingAddress_TextBoxExtendedZipCode". Now plz guide how can I make this textbox mandatory. Plz guide me ASAP instead of telling me as if I have asked a terrible thing :)

推荐答案


您可以通过添加属性将其用于两个目的.

用户控制
Hi,
You can use this for both purpose by adding property.

user control
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uctextBox.ascx.cs" Inherits="UserControlRegulerExpressionValidator.uctextBox" %>
<table><tr><td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"

        runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="ucval" Enabled="false" ></asp:RequiredFieldValidator></td><td><asp:Button ID="Button1"

    runat="server" Text="Button" ValidationGroup="ucval" /></td></tr></table>





用户控件后面的代码



code behind of the user control

protected void Page_Load(object sender, EventArgs e)
       {
           if (MyProperty)
               RequiredFieldValidator1.Enabled = MyProperty;
       }

       public bool MyProperty { get; set; }




您的页面代码




Your page code behind

protected void Page_Load(object sender, EventArgs e)
        {
            uctextBox1.MyProperty = true;
        }


只需使用该属性即可启用和禁用用户控件所需的字段验证器.

如果您需要更多说明,请根据您的要求询问并改进此代码,如果对您有帮助,请不要忘记将其标记为答案.


simply enable and disable user control reqiured field validator using the property.

If you need more clarifications please ask and improve this code according to your requirement and don not forget to mark as answer if this helps to you.


这篇关于如何在页面上应用必填字段有效用户控件文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆