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!

2
  • Are you/Can you use anything like JQuery? Commented Sep 30, 2011 at 14:57
  • I can use, but how you think the JQuery would work? Commented Sep 30, 2011 at 15:13

3 Answers 3

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')" />
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

this should help you

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

1 Comment

But, i has to call a javascript function, just when I click in a "Insert Button", and not when I check a checkbox
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.