Hypnoz
Hypnoz

Reputation: 1136

How to find ClientId in aspx(javascript) that the control is in the template

        function CheckRequired(target, value) {
        if ($("#<%=rbWaiveyes.ClientId %>").attr("checked") || $("#<%=rvWaiveno.ClientId %>").attr("checked")) {
            value.IsValid = true;
        } else {
            value.IsValid = false;
        }
    }

I want to get rbWaiveyes and rvWaiveno ID, the script works in ordinary way. but the rbWaiveyes and rvWaiveno are actually in the template of gridview. what we do in the c# file is that we can use findcontrol method, but how can i find id in the aspx(javascript) way.

And the this is actually custom validater control which is also put in the template, so call is also from template.

Upvotes: 3

Views: 800

Answers (2)

john
john

Reputation: 295

 function CheckRequired(target, value) {
     value.IsValid = $(target).parents("td").find(".waiveRadioButton :radio:checked").length > 0; }


 <ItemTemplate>
     <asp:RadioButton runat="server" ID="rbWaiveyes" Text="Yes" CssClass="waiveRadioButton"
         GroupName='<%# "Waive_" + Container.DataItemIndex %>' />&nbsp;
     <asp:RadioButton runat="server" ID="rbvWaiveno" Text="No" CssClass="waiveRadioButton"
         GroupName='<%# "Waive_" + Container.DataItemIndex %>' />
     <asp:CustomValidator runat="server" ID="cv1" 
         ValidationGroup='<%# "Waive_" + Container.DataItemIndex %>'
         ClientValidationFunction="CheckRequired" ErrorMessage="Make your choice" />
     <asp:Button runat="server" Text="Ok" ValidationGroup='<%# "Waive_" + Container.DataItemIndex %>'
         CommandName="Save" /> </ItemTemplate>

Upvotes: 1

IUnknown
IUnknown

Reputation: 22478

You can't use id selector here as for each row ids of rbWaiveyes and rvWaiveno will be different. Try this code instead:

function CheckRequired(target, value) {
    value.IsValid = $(target).parents("td").find(".waiveRadioButton :radio:checked").length > 0;
}

<ItemTemplate>
    <asp:RadioButton runat="server" ID="rbWaiveyes" Text="Yes" CssClass="waiveRadioButton"
        GroupName='<%# "Waive_" + Container.DataItemIndex %>' />&nbsp;
    <asp:RadioButton runat="server" ID="rbvWaiveno" Text="No" CssClass="waiveRadioButton"
        GroupName='<%# "Waive_" + Container.DataItemIndex %>' />
    <asp:CustomValidator runat="server" ID="cv1" 
        ValidationGroup='<%# "Waive_" + Container.DataItemIndex %>'
        ClientValidationFunction="CheckRequired" ErrorMessage="Make your choice" />
    <asp:Button runat="server" Text="Ok" ValidationGroup='<%# "Waive_" + Container.DataItemIndex %>'
        CommandName="Save" />
</ItemTemplate>

Upvotes: 0

Related Questions