locked
c# img applying onclick attribute on RowDatabound RRS feed

  • Question

  • User-2004582644 posted

    Hi all, I need your help.

    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 message;
    2. Open the new aspx page Default3.aspx.

    How to do solve this ?

    Thank you in advance for any help

    Tuesday, May 14, 2019 11:49 AM

Answers

  • User314352500 posted

    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
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 15, 2019 7:14 AM
  • User665608656 posted

    Hi Chevy Marl Sunderland,

    According to your description, I suggest that you could bind the URL property of the ImageButton control and the onClientClick event in the OnRowDataBound event in the code behind,and distinguish the parameters in the OnRowEditing event to jump into different pages.

    For more details, you can refer to the following code:

    <asp:GridView ID="gv" runat="server" Width="800px"
                    AutoGenerateColumns="False" DataKeyNames="LoginId" OnRowEditing="GridView1_RowEditing" OnRowDataBound="gv_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                        <asp:TemplateField HeaderText="LoginId" SortExpression="LoginId">
                           <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("LoginId") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="LoginPwd" SortExpression="LoginPwd">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("LoginPwd") %>'></asp:Label>
                           </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name" SortExpression="Name">
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Mail" SortExpression="Mail">
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("Mail") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Position" SortExpression="Position">
                            <ItemTemplate>
                                <asp:Label ID="Label5" runat="server" Text='<%# Bind("Position") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton
                                    ID="imgbtnEdit"
                                   CommandName="Edit"
                                    runat="server" />
                           </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    code behind:

            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                string sID = gv.DataKeys[e.NewEditIndex].Value.ToString();
                string hrefName = "Default2.aspx";
                Label label = (Label)gv.Rows[e.NewEditIndex].FindControl("Label5");
                if (label.Text.Equals("HR"))
                {
                    hrefName = "Default3.aspx";
                }
                string queryString = hrefName + "?sID=" + sID.ToString();
                StringBuilder newWin = new StringBuilder();
                newWin.Append("var Mleft = (screen.width/2)-(1200/2);");
                newWin.Append("var Mtop = (screen.height / 2) - (700 / 2);");
                newWin.Append("window.open('" + queryString + "', '_blank',");
                newWin.Append("'height=600,width=600, ");
                newWin.Append("status = yes, toolbar = no, scrollbars = yes, menubar = no,");
                newWin.Append(" location = no, top =\'+Mtop+\',  ");
                newWin.Append("left =\'+Mleft+\';'); ");
                Response.Write(string.Format("<script>" + newWin.ToString() + "</script>"));
            }
    
            protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    ImageButton img = (ImageButton)e.Row.FindControl("imgbtnEdit");
                    img.Width = 18;
                    img.Height = 18;
                    img.Enabled = true;
                    Label label = (Label)e.Row.FindControl("Label5");
                    if (label.Text.Equals("HR"))
                    {
                        img.ImageUrl = "../GridViewScroll-0.9.6.8/Images/arrowvb.png";
                        e.Row.Cells[6].Attributes.Add("onClick", "alert('Cell 1 Clicked');");
                    }
                    else if (label.Text.Equals("Applicant"))
                    {
                        img.ImageUrl = "../GridViewScroll-0.9.6.8/Images/arrowhl.png";
                        e.Row.Cells[6].Attributes.Add("onClick", "if (!confirm('Confirm ?')) return false;");
                    }
                    else
                    {
                        img.ImageUrl = "../GridViewScroll-0.9.6.8/Images/arrowhr.png";
                        e.Row.Cells[6].Attributes.Add("onClick", "if (!confirm('Confirm ?')) return false;");
                    }
                }
            }

    The result of my work demo: 
     
     


    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 15, 2019 10:03 AM

All replies

  • User314352500 posted

    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
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 15, 2019 7:14 AM
  • User665608656 posted

    Hi Chevy Marl Sunderland,

    According to your description, I suggest that you could bind the URL property of the ImageButton control and the onClientClick event in the OnRowDataBound event in the code behind,and distinguish the parameters in the OnRowEditing event to jump into different pages.

    For more details, you can refer to the following code:

    <asp:GridView ID="gv" runat="server" Width="800px"
                    AutoGenerateColumns="False" DataKeyNames="LoginId" OnRowEditing="GridView1_RowEditing" OnRowDataBound="gv_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                        <asp:TemplateField HeaderText="LoginId" SortExpression="LoginId">
                           <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("LoginId") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="LoginPwd" SortExpression="LoginPwd">
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("LoginPwd") %>'></asp:Label>
                           </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name" SortExpression="Name">
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Mail" SortExpression="Mail">
                            <ItemTemplate>
                                <asp:Label ID="Label4" runat="server" Text='<%# Bind("Mail") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Position" SortExpression="Position">
                            <ItemTemplate>
                                <asp:Label ID="Label5" runat="server" Text='<%# Bind("Position") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:ImageButton
                                    ID="imgbtnEdit"
                                   CommandName="Edit"
                                    runat="server" />
                           </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    code behind:

            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                string sID = gv.DataKeys[e.NewEditIndex].Value.ToString();
                string hrefName = "Default2.aspx";
                Label label = (Label)gv.Rows[e.NewEditIndex].FindControl("Label5");
                if (label.Text.Equals("HR"))
                {
                    hrefName = "Default3.aspx";
                }
                string queryString = hrefName + "?sID=" + sID.ToString();
                StringBuilder newWin = new StringBuilder();
                newWin.Append("var Mleft = (screen.width/2)-(1200/2);");
                newWin.Append("var Mtop = (screen.height / 2) - (700 / 2);");
                newWin.Append("window.open('" + queryString + "', '_blank',");
                newWin.Append("'height=600,width=600, ");
                newWin.Append("status = yes, toolbar = no, scrollbars = yes, menubar = no,");
                newWin.Append(" location = no, top =\'+Mtop+\',  ");
                newWin.Append("left =\'+Mleft+\';'); ");
                Response.Write(string.Format("<script>" + newWin.ToString() + "</script>"));
            }
    
            protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    ImageButton img = (ImageButton)e.Row.FindControl("imgbtnEdit");
                    img.Width = 18;
                    img.Height = 18;
                    img.Enabled = true;
                    Label label = (Label)e.Row.FindControl("Label5");
                    if (label.Text.Equals("HR"))
                    {
                        img.ImageUrl = "../GridViewScroll-0.9.6.8/Images/arrowvb.png";
                        e.Row.Cells[6].Attributes.Add("onClick", "alert('Cell 1 Clicked');");
                    }
                    else if (label.Text.Equals("Applicant"))
                    {
                        img.ImageUrl = "../GridViewScroll-0.9.6.8/Images/arrowhl.png";
                        e.Row.Cells[6].Attributes.Add("onClick", "if (!confirm('Confirm ?')) return false;");
                    }
                    else
                    {
                        img.ImageUrl = "../GridViewScroll-0.9.6.8/Images/arrowhr.png";
                        e.Row.Cells[6].Attributes.Add("onClick", "if (!confirm('Confirm ?')) return false;");
                    }
                }
            }

    The result of my work demo: 
     
     


    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 15, 2019 10:03 AM