Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a textbox inside a gridview. i call a javascipt function on textbox onclick event. and need to bind a checkboxlist using this javascript function.

here is my code:-

<asp:GridView ID="GridView2" runat="server" C AutoGenerateColumns="False" Width="100%">
    <Columns>
        <asp:TemplateField HeaderText="text1" >
            <ItemTemplate>
                <asp:TextBox ID="txt1" runat="server" onclick='<%#string.Format("javascript:BindCheckboxlist(\"{0}\", \"{1}\")", ((GridViewRow) Container).FindControl("txt1").ClientID, ((GridViewRow) Container).FindControl("txt2").ClientID) %>' Text='<%# Bind("txt1") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="text2" >
            <ItemTemplate>
                <asp:TextBox ID="txt2" runat="server" Text='<%# Bind("txt2") %>'></asp:TextBox>
            </ItemTemplate>
            <ControlStyle Width="150px" />
        </asp:TemplateField>
    </Columns>
</asp:GridView> 


<asp:CheckBoxList ID="chklist" runat="server">
  </asp:CheckBoxList>



i am getting error
Cannot read property 'getElementsByTagName' of null
.
can someone suggest me this solution. how can i do it this.

What I have tried:

<script type="text/javascript">
            function BindCheckboxlist(_txt1, _txt2) {

                var CHK = document.getElementById("<%=chklist.ClientID%>");

                    addToCheckBoxListList(CHK,'abc','abc')
            }

            function addToCheckBoxListList(checkBoxListId, displayText, checkBoxValue) {
                var checkBoxListRef = document.getElementById(checkBoxListId);
                var rowArray = checkBoxListRef.getElementsByTagName('input');
                var rowCount = rowArray.length;

                var rowElement = checkBoxListRef.insertRow(rowCount);
                var columnElement = rowElement.insertCell(0);

                var checkBoxRef = document.createElement('input');
                var labelRef = document.createElement('label');

                checkBoxRef.type = 'checkbox';
                checkBoxRef.value = checkBoxValue;
                checkBoxRef.id = checkBoxListId + '_' + rowCount;
                checkBoxRef.name = checkBoxListId + ':' + rowCount;

                labelRef.innerHTML = displayText;

                columnElement.appendChild(checkBoxRef);
                columnElement.appendChild(labelRef);
            }
    </script>
Posted
Updated 15-Jun-20 8:54am
v2

1 solution

Start by passing the ID of the checkbox list to the addToCheckBoxListList function, rather than passing the element itself:
JavaScript
function BindCheckboxlist(_txt1, _txt2) {
    var CHK = "<%=chklist.ClientID%>";
    addToCheckBoxListList(CHK, 'abc', 'abc');
}
You'll need to check whether the control actually renders a surrouding element. If it doesn't, document.getElementById won't find anything.

Also, the server-side control won't know anything about the values you've added via Javascript. You'll have to read the Request.Form collection to read the values from them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900