Search code examples
c#gridviewrowdatabound

c# img applying onclick attribute on RowDatabound


On my gridview in c# it's possible edit the single row.

    <asp:TemplateField ItemStyle-HorizontalAlign="Center">
        <ItemTemplate>
            <asp:ImageButton 
                ID="imgbtnEdit" 
                CommandName="Edit" 
                runat="server"
                ImageUrl='<%# Eval("Stopped").ToString().Equals("1") ? 
                "/aspnet/img/edit_1.gif" : 
                "/aspnet/img/edit_2.gif" %>'
                OnClientClick="if (!confirm('Confirm ?')) return false;" />
        </ItemTemplate>
    </asp:TemplateField>

protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
    string sID = gv.DataKeys[e.NewEditIndex].Value.ToString();
    string queryString = "Default2.aspx?sID=" + sID.ToString();
    string newWin = "var Mleft = (screen.width/2)-(1200/2);
                     var Mtop = (screen.height/2)-(700/2);
                     window.open('" + queryString + "','_blank',
                    'height=600,width=600,
                     status=yes,toolbar=no,scrollbars=yes,menubar=no,
                     location=no,top=\'+Mtop+\', 
                     left=\'+Mleft+\';');";
    ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}

In rowdatabound event I have added this condition when the value of cell number two is equal to zero :

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {                       
        ImageButton img = (ImageButton)e.Row.FindControl("imgbtnEdit");

        if (e.Row.Cells[2].Text.Equals("0"))
        {
            img.ImageUrl = "/aspnet/img/stop.gif";
            img.Width = 18;
            img.Height = 18;
            img.Enabled = true;

            e.Row.Cells[1].Attributes.Add("onClick", "alert('Cell 1 Clicked');");
        }
    }
}

But clicking on image stop.gif is opened first the alert with Confirm? message and last the alert with Cell 1 Clicked message and is opened in window popup the asp page Default2.aspx for editing row.

In this case I need :

  1. Open only the alert Cell 1 Clicked;
  2. Open the new aspx page Default3.aspx.

How to do solve this ?

Thank you in advance for any help.


Solution

  • I hope I was helpful.

    protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
    {    
        int xCount = Int32.Parse(gv.Rows[e.NewEditIndex].Cells[2].Text);
    
        if (xCount > 0)
        {        
           string sID = gv.DataKeys[e.NewEditIndex].Value.ToString();
           string queryString = "Default2.aspx?sID=" + sID.ToString();
           string newWin = "var Mleft = (screen.width/2)-(1200/2);
                            var Mtop = (screen.height/2)-(700/2);
                            window.open('" + queryString + "','_blank',
                           'height=600,width=600,
                            status=yes,toolbar=no,scrollbars=yes,menubar=no,
                            location=no,top=\'+Mtop+\', 
                            left=\'+Mleft+\';');";
           ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);    
        }
    }
    
    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {                       
            ImageButton img = (ImageButton)e.Row.FindControl("imgbtnEdit");
    
            if (e.Row.Cells[2].Text.Equals("0"))
            {
                img.ImageUrl = "/aspnet/img/stop.gif";
                img.Width = 18;
                img.Height = 18;
                img.Enabled = true;
    
                img.OnClientClick = "";
                img.Attributes["onclick"] = "if(!confirm('...')){return false;};";//open Default3.aspx
            }
        }
    }