Adding jquery dialog box to formview delete button causes other buttons to cause an error RRS feed

  • Question

  • User-563885515 posted
    Dear All, 
    i have the following function that confirms if the user wants to delete a record in a formview.
              $(function () {
              $("#<%= FormView1.FindControl("DeleteButton").ClientID %>").click(function (e) {
                  autoOpen: false,
                  resizable: false,
                  height: "auto",
                  width: 400,
                  modal: true,
                  buttons: {
                      "Delete item": function () {
                          javascript: __doPostBack('FormView1$DeleteButton', '')
                      Cancel: function () {
    the code for my formview and ItemTemplate that contains the Delete Button
           <asp:FormView ID="FormView1" runat="server" AllowPaging="True" CellPadding="4" DataKeyNames="Car_ID" DataSourceID="SqlDataSource1" ForeColor="#333333">
                    <asp:Label ID="Car_IDLabel" runat="server" Text='<%# Eval("Car_ID") %>' />
                    <br />
                    <asp:Label ID="Car_Make_FkeyLabel" runat="server" Text='<%# Bind("Car_Make_Name") %>' />
                    <br />
                    <asp:Label ID="Car_ModelLabel" runat="server" Text='<%# Bind("Car_Model_Name") %>' />
                    <br />
                    <asp:Label ID="Car_Color_FkeyLabel" runat="server" Text='<%# Bind("Color_Name") %>' />
                    <br />
                    <asp:Label ID="Car_YearLabel" runat="server" Text='<%# Bind("Car_Year") %>' />
                    <br />
                    <asp:Label ID="Car_PriceLabel" runat="server" Text='<%# Bind("Car_Price") %>' />
                    <br />
                    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                    &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
                        CommandName="Delete" Text="Delete"/>
                    &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    However, if i click the EditButton or NewButton i get the following error:
        System.NullReferenceException: 'Object reference not set to an instance of an object.'
    on the following line of the function:
        $("#<%= FormView1.FindControl("DeleteButton").ClientID %>").click(function (e) {

    Sunday, March 29, 2020 5:21 PM

All replies

  • User-719153870 posted

    Hi khedir,

    This is little confusing that in your thread title, you said it causes other buttons to cause an error, while in your post you said the error is on the DeleteButton function code which seems contradictory.

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    I built a demo based on the code your provided, but the issue can not be reproduced. According to the error and the location you provided where the error occurs, it's most likely that your c# code can not find the control whose id is "DeleteButton".

    Can you provide the complete code including both of the front end and back end? And please pay attention to the format of your post content, it's really hard to read if you post so many meaningless html elements. Maybe the "Paste as Text"  can help.

    Best Regard,

    Yang Shen

    Monday, March 30, 2020 1:54 AM