显示确认消息上-gridview的-deleting.aspx

时间:2017-01-03 07:06:26

标签: javascript c# asp.net gridview

我想在删除之前做确认消息,但我有错误

  

无法转换类型为#System; Web.UI.LiteralControl'的对象输入   ' System.Web.UI.WebControls.LinkBut​​ton'

我不知道问题在哪里以及如何解决,

<script type="text/javascript">
    function ConfirmOnDelete(item)
    {
        if (confirm("Are you sure to delete: " + item + "?")==true)
            return true;
          else
            return false;
    }
</script>
 <asp:GridView ID="GVAllProjects" runat="server"
    OnRowDataBound="GVAllProjects_RowDataBound"
    OnRowDeleting="GVAllProjects_RowDeleting"
   OnRowEditing="GVAllProjects_RowEditing"
   OnRowUpdating="GVAllProjects_RowUpdating"
   OnRowCancelingEdit="GVAllProjects_RowCancelingEdit"
   OnPageIndexChanging="GVAllProjects_PageIndexChanging"
   PagerSettings-Mode="NumericFirstLast"
   AutoGenerateColumns="False" AllowPaging="True" PageSize="3" DataKeyNames="ID"
   CssClass="grid">
  <EditRowStyle CssClass="GridViewEditRow" />
     <Columns>
       <asp:CommandField ButtonType="Image" UpdateText="تحديث" CancelImageUrl="~/images/close-btn.png" CancelText="الغاء" DeleteImageUrl="images/delete.png"  DeleteText="حذف" EditText="تعديل" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
     <asp:TemplateField Visible="false">
       <ItemTemplate>
         <asp:Label ID="lblProID" Visible="false" runat="server" Text='<%# Eval("ID")%>' />

       </ItemTemplate>
   </asp:TemplateField>
     <asp:TemplateField HeaderText="المشروعات" ItemStyle-Width="150">
                                                <ItemTemplate>
     <asp:Label ID="lblProjectName" runat="server" Text='<%# Eval("ProjectName")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtProjectName" runat="server" Text='<%# Eval("ProjectName") %>'></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="الغرض" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblPurposeName" runat="server" Text='<%# Bind("Purpose.PurposeName")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:DropDownList ID="ddlPurposeName" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="valPurposeName" runat="server" ControlToValidate="ddlPurposeName"
                                                        Display="Dynamic" ErrorMessage="PurposeName is required." ForeColor="Red" SetFocusOnError="True"
                                                        ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
                                                </EditItemTemplate>
                                                <FooterTemplate>
                                                    <asp:DropDownList ID="ddlPurposeNameNew" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="valClassificationNew" runat="server" ControlToValidate="ddlPurposeNameNew"
                                                        Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
                                                        ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
                                                </FooterTemplate>
                                            </asp:TemplateField>

                                            <asp:TemplateField HeaderText="الموقع" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblDistrictName" runat="server" Text='<%# Eval("District.DistrictName") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:DropDownList ID="ddlDistrictName" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="valDistrictName" runat="server" ControlToValidate="ddlDistrictName"
                                                        Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
                                                        ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
                                                </EditItemTemplate>
                                                <FooterTemplate>
                                                    <asp:DropDownList ID="ddlDistrictNameNew" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="valDistrictNameNew" runat="server" ControlToValidate="ddlDistrictNameNew"
                                                        Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
                                                        ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
                                                </FooterTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="النوع" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblTypes" runat="server" Text='<%# Bind("Type.TypeName")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:DropDownList ID="ddlTypes" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="valTypes" runat="server" ControlToValidate="ddlTypes"
                                                        Display="Dynamic" ErrorMessage="Types is required." ForeColor="Red" SetFocusOnError="True"
                                                        ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
                                                </EditItemTemplate>
                                                <FooterTemplate>
                                                    <asp:DropDownList ID="ddlTypesNew" runat="server">
                                                    </asp:DropDownList>
                                                    <asp:RequiredFieldValidator ID="valTypesNew" runat="server" ControlToValidate="ddlTypesNew"
                                                        Display="Dynamic" ErrorMessage="Types is required." ForeColor="Red" SetFocusOnError="True"
                                                        ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
                                                </FooterTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="المساحات" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblAreas" runat="server" Text='<%# Eval("Areas")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtAreas" runat="server" Text='<%# Eval("Areas") %>'></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="نظام السداد" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblPaymentSystem" runat="server" Text='<%# Eval("PaymentSystem")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtPaymentSystem" runat="server" Text='<%# Eval("PaymentSystem") %>'></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="تاريخ الاستلام" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblReceivedDate" runat="server" Text='<%# Eval("ReceivedDate", "{0:d}")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtEditDate" Text='<%# Eval("ReceivedDate", "{0:d}")%>' runat="server"></asp:TextBox>
                                                    <asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" />
                                                    <ajaxToolkit:CalendarExtender PopupButtonID="imgPopup" ID="CalendarExtender1" runat="server" TargetControlID="txtEditDate"
                                                        Format="MM/dd/yyyy" Enabled="true" />
                                                </EditItemTemplate>
                                                <FooterTemplate>
                                                    <asp:TextBox ID="txtAddDate" runat="server"></asp:TextBox>
                                                    <asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" />
                                                    <ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" PopupPosition="BottomRight"
                                                        TargetControlID="txtAddDate" PopupButtonID="imgPopup" Enabled="true" Format="dd/MM/yyyy" />
                                                </FooterTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="تصنيف العقار" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <asp:Label ID="lblClassification" runat="server" Text='<%# Eval("PropertyClassification")%>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtClassification" runat="server" Text='<%# Eval("PropertyClassification") %>'></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="صورة المشروع" ItemStyle-Width="150">
                                                <ItemTemplate>
                                                    <img src='ProjectsImages/<%# Eval("ProjectImage") %>' style="width: 135px; height: 85px;" />
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:FileUpload ID="UploadPhoto" runat="server" />
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                        </Columns>
                                    </asp:GridView>

背后的代码

if (e.Row.RowType == DataControlRowType.DataRow)
{
        string id = e.Row.Cells[0].Text; // Get the id to be deleted
                                         //cast the ShowDeleteButton link to linkbutton
        LinkButton lb = (LinkButton)e.Row.Cells[9].Controls[2];
        if (lb != null)
        {
            //attach the JavaScript function with the ID as the paramter
            lb.Attributes.Add("onclick", "return ConfirmOnDelete('" + id + "');");
        }

        ddlDistrictName = e.Row.FindControl("ddlDistrictName") as DropDownList;
        ddlPurposeName = e.Row.FindControl("ddlPurposeName") as DropDownList;
        ddlTypes = e.Row.FindControl("ddlTypes") as DropDownList;
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

开始使用控件按钮的项目模板,您将有更多选择。

<asp:TemplateField ShowHeader="False">
    <ItemTemplate>
        <asp:ImageButton ID="DeleteItem" runat="server" ImageUrl="~/delete.png" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this item?');" AlternateText="Delete" />
    </ItemTemplate>
</asp:TemplateField>

请注意使用CommandName,在本例中为Delete。您还可以使用EditUpdateCancel。基本上与CommandField

中的项目相同