我有一個用戶控件,它包含一個帶有一些額外內容的文本框,但爲了本示例的目的,假設它是純文本框就足夠了。我從Web窗體調用此用戶控件,並希望能夠使用RequiredFieldValidator,它基本上的功能與我在Web窗體上的TextBox上使用它的功能相同。我如何配置我的用戶控制來處理這個?在用戶控件中處理RequiredFieldValidator
編輯:
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
}
這與我的類似,除了我沒有設置屬性和我的驗證器第一次出現。此外,在我的工作代碼中,我使用Page.Validate()手動驗證並檢查Page.IsValid。它可能不是你的要求,但可能值得檢查在調用需要驗證發生的事件或方法時它是否像這樣工作。 – 2011-01-28 16:16:22
給了它一個鏡頭,但沒有運氣:/無論如何。 – Kyle 2011-01-28 16:21:25
hmmm,奇怪的是,如果我把Page.Validate()放在Page_Load方法中,它可以工作。不幸的是,這對客戶端驗證沒有幫助。 – Kyle 2011-01-28 16:23:44