开发者

How can I call a javascript function when Checkbox is checked

开发者 https://www.devze.com 2023-04-10 07:42 出处:网络
How can I call a Javascript function when a checkbox is checked when this checkbox is inside a gridview ?

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>
        &开发者_运维百科lt;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!


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')" />


this should help you

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号