如何在选中Checkbox时调用javascript函数

时间:2011-09-30 14:54:56

标签: c# javascript asp.net

当此复选框位于gridview内时,如果选中复选框,如何调用Javascript函数?

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>

没有复选框,当我放置图片并将链接href放到javascript时,它可以工作!,但是带有复选框,没有!

3 个答案:

答案 0 :(得分:6)

onclick属性添加到Checkbox标记,并包含您要调用的javascript函数。

<asp:CheckBox ID="chkExpiraTresDias" runat="server" onclick="alert('Foo')" />

答案 1 :(得分:0)

这应该可以帮到你

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

答案 2 :(得分:0)

我担心在点击“插入”按钮后你必须遍历Gridview,然后再做你必须做的事情。像这样:

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
        }
    }
}
祝你好运。

Hanlet