Question : Repeater Enable/Disable RequiredFieldValidator depending a textbox

Hi,

I have a repeater and I want to enable/disable a requiredFieldValidator depending on the value of a textbox.

ascx:
<script type="text/javascript">
    function enableValidators (dateID, textID, valDate, valText) {
        if (document.getElementById(dateID).value != '' || document.getElementById(textID) != '') {
              ValidatorEnable(document.getElementById(valDate), true);
              ValidatorEnable(document.getElementById(valVisa), true);
            }
         else {
            ValidatorEnable(document.getElementById(valDate), false);
            ValidatorEnable(document.getElementById(valVisa), false);
        }
}  
</script>

<asp:Repeater ID="repeaterID" runat="server"
    OnItemDataBound="repeaterID_ItemDataBound"  EnableViewState="True">
<HeaderTemplate>
<table class="RubTabCoul" width="580px">
</HeaderTemplate>
<ItemTemplate>
    <tr>
        <td >
            <asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("COL_DATE", "{0:dd.MM.yyyy}") %>' Width="63px"
           <asp:RequiredFieldValidator ID="rfvDate" runat="server" Display="Static"  
                                                            ControlToValidate="txtDate" Text="<img src='../images/warning.gif' />" ValidationGroup="valSave"></asp:RequiredFieldValidator>
        </td>
        <td >
            <asp:TextBox ID="txtText" runat="server" Text='<%# Bind("COL_TEXT") %>' Width="79px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="rfvVisa" runat="server" Display="Static"  
                                                            ControlToValidate="txtText" Text="<img src='../images/warning.gif' />" ValidationGroup="valSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
</ItemTemplate>
<FooterTemplate>      
</table>
</FooterTemplate>
</asp:Repeater>


Ascx.cs:

protected void repeaterID_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TextBox text = (TextBox)e.Item.FindControl("txtText");
            TextBox date = (TextBox)e.Item.FindControl("txtDate");
            RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("rfvDate");
            RequiredFieldValidator val2 = (RequiredFieldValidator)e.Item.FindControl("rfvText");
            text.Attributes.Add("onchange", "enableValidators(" + text.ClientID + "," + date.ClientID + "," + val.ClientID + "," + val2.ClientID + ");");
            date.Attributes.Add("onchange", "enableValidators(" + text.ClientID + ", " + date.ClientID + ", " + val.ClientID + ", " + val2.ClientID + ");");
        }
    }


I become always a javascript error when I enter something in the a textbox :   'ctl00_plhContent_ctl00_...' is undefined

Thanks for your help

Answer : Repeater Enable/Disable RequiredFieldValidator depending a textbox

You can get an idea on how to enable disable RVF based on a textbox value.

http://forums.asp.net/t/1346158.aspx

But i think you are having issues on identifying the control in java script. This can happen if your text box control is within some other controls. Then you have to send the full control hierarchy to javascript code. Enable 'trace' option in ASP to identify the full path to your textbox and try to send that path to your javascript and see.
Random Solutions  
 
programming4us programming4us