转发器中必需的字段验证器

时间:2011-01-13 07:28:48

标签: asp.net

我在转发器中有一个转发器控件和文本框。我想在文本框中使用必填字段验证器,我可以这样做吗

8 个答案:

答案 0 :(得分:9)

<asp:Repeater id="myRep" OnItemDataBound="rep_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:TextBox id="tx" runat="server" />
        <asp:RequiredFieldValidator id="myVal" ControlToValidate="tx" ErrorMessage="Required" runat="server" />
    </ItemTemplate>
</asp:Repeater>

<强>更新

将此添加到代码隐藏(也可以稍微修改标记以订阅事件,参见上文):

protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
     RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("myVal");
     TextBox tb = (TextBox)e.Item.FindControl("tx");

     val.ControlToValidate = tb.ID;
}

答案 1 :(得分:5)

如果您希望通过单个按钮验证转发器中的所有文本框,那么就像这样简单

  <asp:Repeater ID="rpt" runat="server">
     <ItemTemplate>
        <asp:TextBox ID="txt" runat="server">
        </asp:TextBox>
        <asp:RequiredFieldValidator ID="rqf" ControlToValidate="txt" 
        ErrorMessage="*" ValidationGroup = "TestValidationGroup" runat = "server">
        </asp:RequiredFieldValidator>
    </ItemTemplate>
   </asp:Repeater>
   <asp:Button ID = "btnSubmit" runat = "server" ValidationGroup = "TestValidationGroup" />

无需检查数据绑定或任何事件的任何事件。

答案 2 :(得分:2)

您可以在repeater itemdatabound上设置ControlToValidate值。

答案 3 :(得分:1)

试试这个

<asp:Repeater ID="rptSample" runat="server">
    <ItemTemplate>
        Name:<br />
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName" runat="server" Display="Static" ErrorMessage="Name field cannot be left blank"></asp:RequiredFieldValidator> 
    </ItemTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />

答案 4 :(得分:0)

protected void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        ((RequiredFieldValidator)e.Item.FindControl("myVal")).ValidationGroup = ((TextBox)e.Item.FindControl("tx")).UniqueID;
    }
}

答案 5 :(得分:0)

protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e) {
    tblData tbldata = e.Item.DataItem as tblData;

    RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("rfv");

    if (tbldata.FieldName.ToUpper().Contains("NOT NULL")) {
        TextBox tb = (TextBox)e.Item.FindControl("txtDATFieldName");
        val.ControlToValidate = tb.ID;
    }
    else {
        val.Enabled = false;   // disable when you dont need a validator
    }
}

答案 6 :(得分:0)

也许你想要每行验证一次......将验证组设置为每行一组,如下所示

ValidationGroup='<%# "gropname" + Eval("Id") %>'

在验证器,文本框和按钮

中执行此操作

HTH -G。

答案 7 :(得分:0)

我在RegisterExpandoAttribute中不断尝试执行此操作时遇到重复的键异常。 我在转发器中使用UserControl,当&#34; OnDataBinding&#34;而不是&#34; ItemDataBinding&#34; 这对我有用:

/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.DataBinding" /> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnDataBinding(EventArgs e)
{
    base.OnDataBinding(e);
    foreach (Control ct in this.Controls)
    {
        BaseValidator bv = ct as BaseValidator;
        if (null == bv)
        {
            continue;
        }
        bv.ControlToValidate = this.FindControl(bv.ControlToValidate).ID;
        bv.ValidationGroup = this.ValidationGroup;
    }
}

是的,我认为它没有任何意义