locked
ASP.NET Ajax Problem on VS2008 (Microsoft JScript runtime error: Sys.InvalidOperationException) RRS feed

  • 問題

  • Hi I am having some ASP.NET AJAX problem.

    I am not sure why whenever I do some action on navigate to another page E.g. LinkButton or Button that trigger response.redirect("") , I will receive the following error.

    <<Microsoft JScript runtime error: Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomEvent.addHandler method.>>

    I am wondering if there any way that I can get around it?

    Thanks in advance.

    Chi

    here it is the structure of the page.

    <Drop down List/>
    <Update Panel1>

    GridView where I allow  user to expand another UpdatePanel to display more information about the current record
    button to go to another page
    Button to delete the record which will trigger AjaxToolKit ConfirmPopUp control

    <UpdatePanel2>

    <User control>
    button to modify data
    button to close the section
    </UpdatePanel2>
    </Update Panel1>

    Hope it could help it illustrate my problem
    2008年9月18日 下午 02:44

解答

  • Do you mind that you have the DeleteButton in GridView and you wanna fire client Confirm Button?

    I have done similar, with custom javascript.

     

    Code Snippet

    <script type="text/javascript">
    var _source; //  keeps track of the delete button for row that is going to be removed
    var _popup;                            // keep track of the popup div
    function showConfirm(source){
    this._source = source;
    this._popup = $find('mdlPopup');       //  find the confirm ModalPopup and show it   
    this._popup.show();
    }
    function okClick(){                    //  find the confirm ModalPopup and hide it   
    this._popup.hide();
    __doPostBack(this._source.name, '');   //  use the cached button as the postback source
    }
    function cancelClick(){                //  find the confirm ModalPopup and hide it
    this._popup.hide();                    //  clear the event source
    this._source = null;
    this._popup = null;
    }
    </script>

     

    <asp:GridView...>

        <Columns>
            <asp:TemplateField HeaderText="..." SortExpression="Delete">
                <ItemTemplate>
                    <asp:ImageButton ID="delete_button" runat="server" ImageUrl="~/image/delete_16x.ico" CommandName="Delete" ToolTip="Delete" OnClientClick = "showConfirm(this); return false;"  CausesValidation="False"  />
                </ItemTemplate>
            </asp:TemplateField>

    </asp:GridView>

    <div id="div" runat="server" align="center" class="confirm" style="display:none">
        <table border="0">
            <tr><td align="left" style="background-color:ThreeDDarkShadow; height:20px; ">&nbsp; <b>
                <asp:Label ID="lblAttachment" runat="server" Text="Attachment" ForeColor="white" Font-Size="Larger"></asp:Label></b></td></tr>
            <tr><td align="left" style="height:50px">
                    <b><img align="absmiddle" src="Image/warning.jpg" alt="Warning: " />&nbsp; Are you sure you want to delete this item? </b></td></tr>
            <tr><td align="right"><asp:Button ID="btnOK" runat="server" Text="Yes" Width="50px" />&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnNo" runat="server" Text="No" Width="50px" /></td></tr>
        </table>   
    </div>
    <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" DropShadow="true" BehaviorID="mdlPopup" TargetControlID="div" PopupControlID="div" OkControlID="btnOK" OnOkScript="okClick();" CancelControlID="btnNo" OnCancelScript="cancelClick();" BackgroundCssClass="modalBackground" >
    </ajaxToolkit:ModalPopupExtender>

     

     

    2008年10月22日 上午 08:02

所有回覆

  • Hi Chi,

     

    May be you want to look at this tutorial? It creates a demo which is very similar to what you are trying to do. Note that in the "UpdatePanel" there is a "ContentTemplate" as well as a "Triggers" tag.

     

    Rgds,

    Jack

    2008年9月19日 上午 08:36
  • Hi Jack

    Thanks for your information. I am afraid, the solution that you provide does not work for me = (.
    I am sorry for I did not explain my situation properly. Some of the button that I need to click is actually on the GridView. I do not think I can use UpdatePanel's trigger for the the control which is inside GridView. I tried it before, it always said the control cannot be find, not sure if I done it correctly. Another linkbutton that I allow used to click on is generated dynamically when the gridview is generate (which is about custom paging). I am not sure what ControlID can I put onto the trigger as I create it by using a loop and add them onto a div.
    E.g. div.Controls.add(linkbutton)


    <Drop down List/>
    <Update Panel1>

    <GridView>
    GridView where I allow  user to expand another UpdatePanel to display more information about the current record
    button to go to another page
    Button to delete the record which will trigger AjaxToolKit ConfirmPopUp control


    </GridView>
    <div>
    Custom Paging linked button which is generated dynamically when the grid view is load, it will contain some link button to allow user to click on and go to next page of the gird view
    </div>
    <UpdatePanel2>

    <User control>
    button to modify data
    button to close the section
    </UpdatePanel2>
    </Update Panel1>


    I find some solution online, they all said something like I need to modifiy the UpdatePanel and add some extra JavaScript on the webpage. to remove some of the Handler. I hope I do not need to do it that way.

    Thanks a lots.


    2008年9月19日 上午 09:17
  • Do you mind that you have the DeleteButton in GridView and you wanna fire client Confirm Button?

    I have done similar, with custom javascript.

     

    Code Snippet

    <script type="text/javascript">
    var _source; //  keeps track of the delete button for row that is going to be removed
    var _popup;                            // keep track of the popup div
    function showConfirm(source){
    this._source = source;
    this._popup = $find('mdlPopup');       //  find the confirm ModalPopup and show it   
    this._popup.show();
    }
    function okClick(){                    //  find the confirm ModalPopup and hide it   
    this._popup.hide();
    __doPostBack(this._source.name, '');   //  use the cached button as the postback source
    }
    function cancelClick(){                //  find the confirm ModalPopup and hide it
    this._popup.hide();                    //  clear the event source
    this._source = null;
    this._popup = null;
    }
    </script>

     

    <asp:GridView...>

        <Columns>
            <asp:TemplateField HeaderText="..." SortExpression="Delete">
                <ItemTemplate>
                    <asp:ImageButton ID="delete_button" runat="server" ImageUrl="~/image/delete_16x.ico" CommandName="Delete" ToolTip="Delete" OnClientClick = "showConfirm(this); return false;"  CausesValidation="False"  />
                </ItemTemplate>
            </asp:TemplateField>

    </asp:GridView>

    <div id="div" runat="server" align="center" class="confirm" style="display:none">
        <table border="0">
            <tr><td align="left" style="background-color:ThreeDDarkShadow; height:20px; ">&nbsp; <b>
                <asp:Label ID="lblAttachment" runat="server" Text="Attachment" ForeColor="white" Font-Size="Larger"></asp:Label></b></td></tr>
            <tr><td align="left" style="height:50px">
                    <b><img align="absmiddle" src="Image/warning.jpg" alt="Warning: " />&nbsp; Are you sure you want to delete this item? </b></td></tr>
            <tr><td align="right"><asp:Button ID="btnOK" runat="server" Text="Yes" Width="50px" />&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnNo" runat="server" Text="No" Width="50px" /></td></tr>
        </table>   
    </div>
    <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" DropShadow="true" BehaviorID="mdlPopup" TargetControlID="div" PopupControlID="div" OkControlID="btnOK" OnOkScript="okClick();" CancelControlID="btnNo" OnCancelScript="cancelClick();" BackgroundCssClass="modalBackground" >
    </ajaxToolkit:ModalPopupExtender>

     

     

    2008年10月22日 上午 08:02
  • Hi Ken,

    Thanks very much for your code, I will try it out later,

    Actually, the problem I had is not only for the button, I also has a problem with my custom paging, When I click on the linkedbutton and try to navigate to another page.

    Do you think I need to use Javascript to deal with it as well?
    I am not good at javascript,

    I would be grateful if you could give me some sample code.

    Thanks very much, I will try out your suggestion later.
    2008年10月22日 上午 09:01
  • Well, for that one, You could need to give the div with Runat=Server and ID=divLinkButton for it.

    And then in your code behind, you need to specific

    1) giving ID for the linkbutton, ie, lbtn1, lbtn2, lbtn3....etc

    2) You may need to use divLinkButton.FindControl(ctlID) in order to access it.

     

    I am not sure how you create and what you need for the linkbutton, but why not using the paging from GridView? The default paging of gridView already has page number, next and previous button.

     

    2008年10月22日 上午 09:50
  • Hi Ken,

    Thanks for your reply,

    I did not use the GridView's paging feature,
    because the way I retrieve data into grid view is by using DataTable and the record that I retrieve each time is just 30, of course also retrieve the total amount of record I had from the same stored procedure E.g. 10000 record, so that I can work out the number of page that I need.

    If I just feed whole 10000 of record into the GridView each time but i only display 30 of them, it will waste a bit of resource,

    please correct me if I am wrong, and I would be grateful, if you can point me to a correct way to do it.

    Thanks a lot
    2008年10月22日 下午 02:25
  • oh...in this way, then you really need to only retrive that 30 records from Stored Procedure in a time.

    Anyway, i suggest that you open another thread to discuss the paging problem.

     

    Let's focus on the AJAX issue in this thread pls. Have you tried my code sample?

     

    2008年10月27日 上午 05:50
  • Hi Ken,

    Sorry for not trying your sample yet. I may need to redo the page again, I had lose the code because the Virtual Machine I used had been messed up, the code that I had at the moment have not got the feature on it.  = (

    I will post again, once I find the problem again.

    Thanks very much for your help.
    2008年10月27日 上午 08:35