lundi 10 septembre 2018

gridview row checkbox onclick client side

I have gridview that has columns that have checkboxes in. Next to the checkboxes I have labels that display dates depending if the checkbox is checked. (i've removed some here for simplification)

What I want to do is fire the onclick even of the checkbox selected and display/hide the label in the column of that row. I have tried a number of different methods but can't get it working.

Any help would be greatly appreciated

 

    function ShowHide(rowId, chk, lbl) {

        if (chk.checked == true) {
            document.getElementById(lbl).style.display ="block";
        }
        if (chk.checked == false) {
            document.getElementById(lbl).style.display ="none";
        }
    }

<asp:GridView ID="grd" ClientIDMode="Static" runat="server" 
                Width="100%" DataKeyNames="ID" AutoGenerateColumns="False" GridLines="Vertical"
                CellPadding="4" AllowPaging="false" AllowCustomPaging="false" PageSize="25" PagerStyle-Visible="False"
                ShowFooter="true"  OnRowDataBound="grd_RowDataBound">
                <Columns>

                    <asp:TemplateField HeaderText="Address">
                        <ItemTemplate>
                            <asp:Label ID="lblAddress" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address") %>'></asp:Label>
                              <asp:HiddenField ID="ID" runat="server" Value='<%# DataBinder.Eval(Container,"DataItem.ID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>

                     <asp:TemplateField HeaderText="Moved Date">
                        <ItemTemplate>
                            <asp:CheckBox ID="chkMove" ClientIDMode="Static" runat="server"></asp:CheckBox>
                             <asp:Label ID="lblMove" ClientIDMode="Static" runat="server"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                </Columns>
</asp:GridView>

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {


            CheckBox chk = (CheckBox)e.Row.FindControl("chkMove");

            Label lbl = (Label)e.Row.FindControl("lblMove");


            chk.Attributes.Add("onClick", "ShowHide(" + e.Row.rowId + "," + chk.UniqueID + "," + lbl.UniqueID + ")");


        }
    }




Aucun commentaire:

Enregistrer un commentaire