3

How can I call a Javascript function when a checkbox is checked when this checkbox is inside a gridview ?

protected void AlteraStatusExpiraSeteDias_Click(object sender, EventArgs e)
{
    for (int i = 0; i < grdImoveis2.Rows.Count; i++)
    {
        GridViewRow RowViewExpiraSeteDias = (GridViewRow)grdImoveis2.Rows[i];
        CheckBox chk = (CheckBox)grdImoveis2.Rows[i].FindControl("chkExpiraSeteDias");
        if (chk != null)
        {
            String codigo;
            if (chk.Checked)
            {
                codigo = (String)grdImoveis2.Rows[i].Cells[0].Text;                        
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Registra", "AlteraStatus(codigo);", false);
            }
        }
    }
}



<asp:GridView ID="grdImoveis2" CssClass="StyleGrid" Width="100%" runat="server" AutoGenerateColumns="false" DataSourceID="ds" BorderWidth="0" GridLines="None">
    <AlternatingRowStyle BackColor="White" CssClass="EstiloDalinhaAlternativaGrid"  HorizontalAlign="Center"/>
        <RowStyle CssClass="EstiloDalinhaGrid" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#e2dcd2" CssClass="thGrid" Height="20" />
    <Columns>
        <asp:BoundField HeaderText="Código" DataField="Imovel_Id" />
        <asp:BoundField HeaderText="Para" DataField="TransacaoSigla" />
        <asp:BoundField HeaderText="Valor" DataField="ValorImovel" DataFormatString="{0:c}" HtmlEncode="false" />
        <asp:TemplateField HeaderText="Endereco">
            <ItemTemplate>
                <%# Eval("Logradouro") %>, <%# Eval("Numero") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Data Cadastro" DataField="DataHora"  DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false"/>
        <asp:BoundField HeaderText="Data Expira" DataField="DataExpira"  DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false"/>
        <asp:TemplateField HeaderText="Ação">
            <ItemTemplate>
                <asp:CheckBox ID="chkExpiraSeteDias" runat="server" onclick="alert('Foo')" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Without the checkbox, when I put a image and put a link href to the javascript, it's works!, but with checkbox, no!

Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118

3 Answers3

6

Add onclick attribute to Checkbox markup and include the javascript function that you'd like to call.

<asp:CheckBox ID="chkExpiraTresDias" runat="server" onclick="alert('Foo')" />
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • it works, but, I check the checkbox, and I need to press the Send Button and then call my javascript function passing by parameters the id of the field id in the row. The CheckBox is inside a GridView – Lucas_Santos Sep 30 '11 at 15:08
0

this should help you

$('input:checkbox[ID$="chkExpiraTresDias"]').change(function() { alert('Hello world!'); });

Junaid
  • 1,708
  • 16
  • 25
0

I fear that you will have to iterate through the Gridview when your "Insert" button is clicked and then do what you have to do. Something like this:

foreach (GridViewRow row in this.grdImoveis2.Rows) {
    if (row.RowType == DataControlRowType.DataRow) {

        CheckBox chk = row.Cells(0).FindControl("chkExpiraTresDias");
        if (chk != null) {
            Response.Write(chk.Checked); //Do what you gotta do here if it's checked or not
        }
    }
}

Good luck.

Hanlet

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75