Click here to Skip to main content
16,013,489 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI!

I have a Gridview with two command fields(Delete, Edit). I want to get a Confirm msg if the user clicks on delete, it works fine, but if he clicks on update, i get an error msg.

The code is:
C#
if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // reference the Delete LinkButton
                LinkButton db = (LinkButton)e.Row.Cells[5].Controls[0];
                db.OnClientClick = "return confirm('Biztos benne, hogy törölni kívánja a kiválasztott célt?');";
            }


and the gridview is:

XML
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-CssClass="gridview_alter"
                       AutoGenerateColumns="False" EmptyDataText=" " GridLines="None" OnRowCancelingEdit="RowCancelingEdit"
                       OnRowDataBound="GridView1_RowDataBound" OnRowDeleting="RowDeleting" OnRowEditing="RowEditing"
                       OnRowUpdating="RowUpdating" ShowHeaderWhenEmpty="True" Width="820px">
                       <AlternatingRowStyle CssClass="gridview_alter" />
                       <Columns>
                           <asp:TemplateField HeaderText="Leírás" Visible="false" />
                           <asp:BoundField DataField="Description" HeaderStyle-Width="150" HeaderText="Leírás" HtmlEncode="false">
                               <HeaderStyle Width="150px" />
                           </asp:BoundField>
                           <asp:BoundField HeaderText="Kritérium" DataField="IndexNumber" />
                           <asp:BoundField DataField="Weight" HeaderStyle-Width="50" HeaderText="Súly">
                               <HeaderStyle Width="50px" />
                           </asp:BoundField>
                           <asp:TemplateField HeaderStyle-Width="100" HeaderText="Cél típusa">
                               <ItemTemplate>
                                   <asp:Label ID="lblGoalType" runat="server"></asp:Label>
                               </ItemTemplate>
                               <HeaderStyle Width="100px" />
                           </asp:TemplateField>
                           <asp:CommandField DeleteText="Törlés" HeaderStyle-Width="60" ShowDeleteButton="true">
                               <HeaderStyle Width="60px" />
                           </asp:CommandField>
                                       <asp:CommandField CancelText="Mégse" DeleteText="Törlés" EditText="Szerkesztés"
                                           HeaderStyle-Width="60" ShowEditButton="True" UpdateText="Frissítés"
                                           ValidationGroup="Leírás, Súly">
                               <HeaderStyle Width="60px" />
                           </asp:CommandField>
                       </Columns>
                   </asp:GridView>



I get the error here:
LinkButton db = (LinkButton)e.Row.Cells[5].Controls[0];
and the error is:
Specified argument was out of the range of valid values.
Posted

I found the solution:
C#
foreach (DataControlFieldCell cell in e.Row.Cells)
{
    // check all cells in one row
    foreach (Control control in cell.Controls)
    {
        // Must use LinkButton here instead of ImageButton
        // if you are having Links (not images) as the command button.
        LinkButton button = control as LinkButton;
        if (button != null && button.CommandName == "Delete")
            // Add delete confirmation
            button.OnClientClick = "return confirm('Biztos benne, hogy törölni kívánja a kiválasztott célt?');";
    }
}

Anyway, thx everybody who read it, and hopefully, it will help anybody who has the same problem.
 
Share this answer
 
v2
Comments
Good work. Thanks for sharing. :) Take a 5 star.
Keep it up.

Regards,
Tadit
Try
XML
<asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:ImageButton ID="DeleteButton" runat="server" ImageUrl="~/image.png"
                    CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?');"
                    AlternateText="Delete" />
            </ItemTemplate>
        </asp:TemplateField>

Here is an example[^] of the confirmation box.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900