so i have a repeater that contains :
<table>
<tr>
<td>
<div class="divStyle">
<asp:CheckBox ID="cbSelection" runat="server" AutoPostBack="true"/>
</div>
<div class="divStyle">
<asp:Literal ID="ltlName" runat="server"></asp:Literal>
</div>
<div style="float:left; width:110px; margin-right:13px;">
<asp:DropDownList ID="ddlMode" runat="server"></asp:DropDownList>
</div>
<div style="float:left; width:110px;">
<asp:DropDownList ID="ddlQuantity" runat="server"
onChange="javascript:CheckPostBack();"></asp:DropDownList>
</div>
</td>
</tr>
what i want to do is as follows : each time the drodDown item is changed, i check if the check box is checked and if it's so i execute a method in the code behind.
so i created the following JavaScript :
<script>
function CheckPostBack() {
var checkElt = document.getElementById(GetCheckBoxClientID());
if (checkElt != null) {
alert($(checkElt).prop('checked'));
if ($(checkElt).prop('checked')) {
var hdnBtn = document.getElementById(GetHiddenBtnClientID());
hdnBtn.click();
}
}
}
hdnBtn is a button that i use to fire a postBack, GetCheckBoxClientID and GetHiddenBtnClientID are methods on the code behind injected onPageLoad so i can get the respective id of the check box and the HiddenButton.
the problem is that this methods didn't work, because the method is the same for all items so each test is done for the checkBox from the first item of the repeater.
so, it take me a little bit to find a solution (ugly one in fact) i add an id to the div containig the checkBox and then used the DOM and navigate to the CheckBox which is in the same Row
<script type="text/javascript">
function CheckPostBack(sourceElt) {
var DivCollection = sourceElt.parentNode.parentNode.getElementsByTagName("div");
for (var i = 0; i < DivCollection.length; i++) {
var oCurDiv = DivCollection[i];
if (oCurDiv.id.indexOf('lnkContainer') >= 0) {
var checkElt = (oCurDiv.firstElementChild || oCurDiv.firstChild)
if (checkElt != null) {
alert($(checkElt).prop('checked'));
if ($(checkElt).prop('checked')) {
var hdnBtn = document.getElementById(GetHiddenBtnClientID());
hdnBtn.click();
}
}
}
}
}
in fact i used the answer that i found on this link: How to access a Div inside a repeater using javascript
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments