Amol Kadam
Amol Kadam

Reputation: 85

Not able to pass repeater Container.ItemIndex to javascript function as an argument

<script type="text/javascript" language="javascript">
function EnableDisablePkgInclusionDropDown(val) {

        alert("RowIndex: " + val);        
        var repeater = document.getElementById('<%= pnlrptPckgInclusions.ClientID %>');
        var dropdowns = repeater.getElementsByTagName('select');
        dropdowns[val].disabled = true;
        dropdowns[val].selectedIndex = "0";
}
</Script>

    <asp:Repeater runat="server" ID="rptPckgInclusions">
                                <HeaderTemplate>
                                    <table cellpadding="0" cellspacing="0" width="100%" border="0">
                                        <tr>

                                            <td align="left" style="width: 20%; text-align: left">
                                                Parent MarkUp
                                            </td>

                                        </tr>
                                    </table>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <table cellpadding="0" cellspacing="0" width="100%" border="0">
                                        <tr>                                            

                                            <td align="center" style="width: 20%;">
                                                <asp:CheckBox runat="server" ID="chkApplyParentMarkUp" OnClick="javascript:EnableDisablePkgInclusionDropDown(<%# Container.ItemIndex %>);"/>
                                            </td>

                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:Repeater>

I am trying to pass container.ItemIndex as an argument to javascript function on checkbox OnClick event to enable/disable few controls.But Its not showing the value in Javascript function.

Thanks in advance!

Upvotes: 0

Views: 2015

Answers (2)

Gaurav Prakash
Gaurav Prakash

Reputation: 51

I would like to suggest my way. Just add an attribute say name='<%# Container.ItemIndex %>' in the markup. For example,

<ItemTemplate>
  <table cellpadding="0" cellspacing="0" width="100%" border="0">
       <tr>                                            
       <td align="center" style="width: 20%;">
           <asp:CheckBox runat="server" ID="chkFoo" OnClick="Test(this)" name='<%# Container.ItemIndex %>'/>
       </td>
       </tr>
     </table>
  </ItemTemplate>

Now, in the javascript method Test(this), you can get the repeater row index using 'this.name'.

Alternatively, if you observe the generated markup html, you will find the ID of the checkbox is generated as something 'ConetentPlaceholder1_rptTest_chkFoo_0. You can split this id in javascript method to get the repeater row index.

Upvotes: 1

dimonser
dimonser

Reputation: 683

First of all be careful in components namings inside your JS function (pnlrptPckgInclusions != rptPckgInclusions)

After this change you OnClick handler to this

OnClick='<%# "javascript:EnableDisablePkgInclusionDropDown(" + Container.ItemIndex + ")" %>'

It should help. Have a nice day !

Upvotes: 0

Related Questions