vb.net 在 javascript 函数中获取中继器内的控件以进行验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24358678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:45:22  来源:igfitidea点击:

getting controls inside a repeater in javascript function for validation

javascriptjqueryasp.netvb.net

提问by DJs

I put several controls inside a repeater control with id = "rpContacts". The repeater is placed inside a table with id = "ParentTable". I need to validate the control inside the repeater(say a text box- txtLastName) for duplicate value with value inside another text box outside the repeater. The validation function is called by a custom Validator which is outside the repeater.

我将几个控件放在一个 id = "rpContacts" 的中继器控件中。转发器被放置在一个 id = "ParentTable" 的表中。我需要验证转发器内的控件(比如文本框 - txtLastName)是否具有重复值与转发器外另一个文本框内的值。验证函数由转发器外部的自定义验证器调用。

The problem is I could not get the client id of neither the repeater nor the controls inside it in my javascript function.

问题是我无法在我的 javascript 函数中获取转发器和其中控件的客户端 ID。

I could get the values of controls inside the repeater if I give their rendered ClientId like

如果我给他们呈现的 ClientId 像,我可以获得中继器内控件的值

$find('ctl00_body_content_rpContacts_ctl00_txtLastName').Value; 

But this client id of control wil change for each row of the repeater.

但是这个控制的客户端 ID 会随着转发器的每一行而改变。

ie. for the txtZip in 2nd repeater row its id will be 'ctl00_body_content_rpContacts_ctl01_txtLastName'.

IE。对于第二个转发器行中的 txtZip,它的 id 将是'ctl00_body_content_rpContacts_ctl01_txtLastName'.

Any suggestions how I can do this validation.

任何建议我如何进行此验证。

--

——

Repeater code:

中继器代码:

<telerik:RadAjaxPanel ID="AjaxPanel1" runat="server" ClientEvents-OnRequestStart="conditionalPostback">
    <table class="ParentTable">
        <asp:Repeater ID="rpContacts" runat="server">
            <ItemTemplate>
                <tr> <td> 
                    <table class="ParentTable" runat="server" id="tblEC"> 
                        <tr> <td> <div> 
                            <asp:TextBox ID="txtLastName" runat="server" Width="180"/> 
                        </div> </td> </tr> 
                    </table> 
                </td> </tr> 
            </ItemTemplate> 
        </asp:Repeater> 
    </table> 
</telerik:RadAjaxPanel>

采纳答案by Ali

Validate your controls this way:

以这种方式验证您的控件:

$find('.rpContacts').Value;

$find('.rpContacts').Value;

//your javascript code
var elemArray = document.getElementsByClassName('rpContacts');
for(var i = 0; i < elemArray.length; i++){
    var elem = elemArray[i].value;
}

//your aspx code
<asp:Repeater ID="Repeater1" runat="server">
  <ItemTemplate>
    <asp:TextBox ID="txtLastName" runat="server" CssClass="rpContacts"></asp:TextBox>
....

回答by Amit Kumar

here is simple demo through which you can get the id and value of controls inside repeater

这是一个简单的演示,您可以通过它获取中继器中控件的 id 和值

 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate >              
                <asp:TextBox ID="lblSubject" runat="server" Text='<%#Eval("Name") %>' Font-Bold="true"/><br />
            </ItemTemplate>
        </asp:Repeater>

and jquery would be

和 jquery 将是

<script type="text/javascript">
        $(document).ready(function () {             
            $('[id*=Repeater1_lblSubject]').on('change', function () {
                var id = $(this).attr('id');
                var value = $('#' + id).val();                  
                alert(id+'_'+value);
            });
        });
    </script>