views:

302

answers:

3

Hi,

I've an aspx page which has got a textbox and a submit button.Entering data in the textbox and hitting the button shows a listview control which displays data retrived from the database.Listvew has sorting,pagination,edit functionalities. For each of these,the corresponding method is triggered on the server side...for example: lv_sorting lv_Edit etc. If the user types in data in the textbox and hits enter key, Page.IsPostback is returned as false in IE browsers but is returned as true in Firefox(any idea, why is this so?).

When postback is true,the corresponding method gets called ie. lv_sorting etc. But since in Page_Load event,am only pulling data from the database when postback is false, page does not show any data in firefox browser.

I need to show data in listview when enter key is pressed in firefox browsers. I tried using DefaultButton="submitBtnId" and also forcefully invoking button click when enter key is pressed in the textbox,but no luck.

Thanks.

FYI, here is the javascript code that gets called when enter key is pressed in the textbox:

 function clickButton(e, buttonid) {
            var bt = document.getElementById(buttonid);
            if (typeof bt == 'object') {
                if (navigator.appName.indexOf("Netscape") > (-1)) {
                    if (e.keyCode == 13) {
                        if (bt && typeof (bt.click) == 'undefined') {
                            bt.click = addClickFunction1(bt)
                        }
                    }
                }
                if (navigator.appName.indexOf("Microsoft Internet Explorer") > (-1)) {

                    if (event.keyCode == 13) {
                        bt.click();
                        return false;
                    }
                }
            }
        }



        function addClickFunction1(bt) {
            var result = true;
            if (bt.onclick) result = bt.onclick();
            if (typeof (result) == 'undefined' || result) {
                eval(bt.href);
            }
        }
+1  A: 

Do you have any Javascript code that tries to handle the enter key?

The default behaviour when pressing the enter key when a form field has focus is to activate the first button in the form. That would post the form to the server, and naturally cause IsPostBack to be true.

I don't know why IE doesn't do that in this case, that is why I wonder if you have any Javascript that may change the behaviour.

The IsPostBack property is only false when you get to the page the first time, for example when you click a link on another page (or even the same page), or use Response.Redirect from another page (or even the same page). When the form on the page is posted back to the server, the IsPostBack property is true.

Unless you make sure that you always make a GET request (for example by setting window.location.href in the browser) to update the page, you can't rely on IsPostBack to tell you when data needs to be loaded.

The way that the ViewState is used by the list view is so that you can load it with data once, and it will retain that data through postbacks. If you change the underlying data and want that change to be visible in the list view you have to remove the current data from it and load it with the new data. Alternatively turn of EnableViewState for the list view and load it with fresh data for every postback.

Guffa
+1  A: 

Internet Explorer does not submit the button when enter is pressed; other browsers do. This means the button's click event does not fire under these circumstances. Thankfully it's possible to set a default button in ASP.NET 2.0.

Once you have all browsers consistently clicking on the button, you should load the values from the database when the button's clicked event fires, not when the page loads.

jleedev
A: 

why not use an asp.net button control, add an event handler for the click and then populate the Listview from the event handler?

Simon