|
-
Dec 21st, 2010, 06:48 AM
#1
IsPostBack issues
I have this code on my asp.net page in page load event:
vb.net Code:
If Not IsPostBack Then recordId = Request.QueryString("rid") myRecord = GetRecord(recordId) If myRecord Is Nothing Then ShowErrorMessage("Record not found!") ' pnlMainBody.Visible = False Exit Sub End If ViewState("RecordId") = recordId Else recordId = ViewState("RecordId").ToString End If
and this javascript code:
javascript Code:
function GotoRecord() { var tb = document.getElementById('txtRecordNumber'); if (isNaN(tb.value) || tb.value.indexOf('.') >= 0) { tb.focus(); alert("Enter valid Record Number."); } else window.location = 'ShowRecord.aspx?rid=' + tb.value; }
It works fine until the "pnlMainBody.Visible = False" is commented out and shows the message ""Record not found!" correctly. But if I uncomment that line, it believes that it is a postback and enters the ELSE block and errors out, when user enters some record number in the textbox and hits enter (it executes the javascript code)
Any ideas?
Last edited by Pradeep1210; Dec 21st, 2010 at 06:51 AM.
-
Dec 21st, 2010, 07:34 AM
#2
Re: IsPostBack issues
Hey,
So, let me see if I have this right...
The server side code that you have posted above, works exactly as you expect it to, until you comment on the pnlMainBody line, and then it stops working?
I can't think of any reason what that would be happening?!?
Gary
-
Dec 21st, 2010, 07:43 AM
#3
Re: IsPostBack issues
All this is on one page only. The textbox is outside the panel.
If I put that http://site/ShowRecord.aspx?rid=xx in the browser URL it works.
But problem is only when I put something in the textbox and use that javascript. It thinks it is a postback if that line is uncommented, and not a postback when that line is commented out.
-
Dec 21st, 2010, 07:46 AM
#4
Re: IsPostBack issues
Very bizarre! I can't say I have ever heard of something like this happening before!
I really can't think of anything to suggest 
Gary
-
Dec 21st, 2010, 07:47 AM
#5
Re: IsPostBack issues
Also, it works if I replace that line with:
pnlMainBody.Style("display") = "none"
Problem occurs only when I set it to invisible. Means I make the control unavailable in the rendered page.
-
Dec 21st, 2010, 07:49 AM
#6
Re: IsPostBack issues
 Originally Posted by Pradeep1210
Also, it works if I replace that line with:
pnlMainBody.Style("display") = "none"
Problem occurs only when I set it to invisible. Means I make the control unavailable in the rendered page.
Really!?!?
Setting the style directly, rather than the visible property makes this work.
Have you tested this on a new project, i.e. completely blank, nothing else on the page apart from the bare minimum. Is it repeatble there?
Gary
-
Dec 21st, 2010, 07:52 AM
#7
Re: IsPostBack issues
Yes. But I can see why that works.
Setting the style will just make it invisible to the eye, but is still in the page. It is just like setting:
<div style="display:none">
But when we set a control Visible=False, then that control will not be available on the rendered page. It will be missing from the rendered html altogether.
-
Dec 21st, 2010, 07:53 AM
#8
Re: IsPostBack issues
Agreed, but I still don't see why uncommenting that line would effect the way that the PostBack is handled.
It has to be altering something in the page life cycle of the page, but I can't think what, or why.
Gary
-
Dec 21st, 2010, 09:09 AM
#9
Re: IsPostBack issues
Can't you run a WINDIF on the source of the pages created both ways and see a difference that might uncover why?
-
Dec 21st, 2010, 09:20 AM
#10
Re: IsPostBack issues
That is a good idea. However, I would be VERY surprised if there was a difference.
Gary
-
Dec 21st, 2010, 10:10 AM
#11
Re: IsPostBack issues
The problem is that when that line is uncommented, it goes into the ELSE block where it gets stuck with server error: "Object reference not set to an instance of an object."
So I don't have 2 pages to compare.
-
Dec 21st, 2010, 10:14 AM
#12
Re: IsPostBack issues
Pradeep, the suggestion was to render the page to the client with the line commented out, and then take the source of the page from the browser and save it off. Then, render the page to the client with the line uncommented, and take the source of the page and save it off. Then compare the two.
Are there any differences?
Gary
-
Dec 21st, 2010, 10:30 AM
#13
Re: IsPostBack issues
I'm beginning to see what is going wrong...
There is a javascript function in the TextBox onkeypress which in turn calls that javascript function I posted in post #1.
javascript Code:
function txtRecordNumber_KeyPress(event) { if (event.keyCode == 13) GotoRecord(); }
What happens is with that line uncommented, for some reason or the other, the page is called twice - once without a postback and once with postback. The call without postback doesn't get stuck anywhere so was not visible at first sight. While the one with postback gets stuck there, because there is no ViewState("RecordId").
Enter keypress in the textbox is causing this. If I call the function on some button click, it works fine. Enter keypress is causing it to be called once via window.loacation=... line and once via a postback.
Since now I can see the problem, I'll start on a workaround to it.
I still can't figure out though, why setting the Panel.Visible = True/False is causing this problem.
-
Dec 21st, 2010, 10:34 AM
#14
Re: IsPostBack issues
Hmm, interesting....
Can you upload a small sample application that reproduces this error? I would be curious to have a look.
Gary
-
Dec 22nd, 2010, 03:14 AM
#15
Re: IsPostBack issues
Here is a stripped down version of the page which has just the bare essential elements.
Unfortunately on this one setting Panel.Visible=True/False has no effect, though the event is fired twice as I mentioned in the earlier post.
My original page (problem page) is inherited from a custom basepage which does a lot of work in between for me. And I have a lot of self made custom controls too on the page. So possibly the Panel.Visible On/Off thing has to do something with that.
Nevertheless, the root of the problem is still same - why is there two calls to the page - one without postback and one with postback? Why pressing enter key does a postback automatically to the server?
aspx file:
Code:
<%@ Page Language="vb" AutoEventWireup="false" Inherits="TestArea.Test" CodeFile="Test.aspx.vb"
EnableEventValidation="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Page</title>
</head>
<body>
<form method="post" runat="server">
<div>
<br />
<asp:Label runat="server" ID="lblMessage"></asp:Label>
<br />
</div>
<div>
<br />
<asp:Label Text="Goto Record:" runat="server"></asp:Label>
<asp:TextBox ID="txtRecordNumber" runat="server" onkeypress="txtRecordNumber_KeyPress(event);"
AutoPostBack="false"></asp:TextBox>
<input type="button" onclick="GotoRecord()" value="Show Record" />
<a onclick="GotoRecord()">Show Record</a>
<br />
</div>
<asp:Panel ID="pnlMainBody" runat="server" BorderStyle="Groove" BorderWidth="2px"
HorizontalAlign="Left">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
Some controls in this panel
<br />
<br />
<asp:Label ID="Label1" Text="Label1" runat="server"></asp:Label><br />
<asp:ListBox runat="server" Rows="10" Width="500px" SelectionMode="Multiple"></asp:ListBox>
<br />
<asp:Button runat="server" Text="Add New" Style="width: 100px; text-align: center" /><br />
<br />
</td>
</tr>
</table>
</asp:Panel>
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
<!--
function GotoRecord() {
var tb = document.getElementById('txtRecordNumber');
if (isNaN(tb.value) || tb.value.indexOf('.') >= 0) {
tb.focus();
alert("Enter valid Record Number.");
}
else
window.location = 'Test.aspx?rid=' + tb.value;
}
function txtRecordNumber_KeyPress(event) {
if (event.keyCode == 13) GotoRecord();
}
-->
</script>
codebehind:
Code:
Namespace TestArea
Partial Class Test
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblMessage.Text = "<br>IsPostBack = " & IsPostBack.ToString
lblMessage.Text &= "<br>PostBack control = " & Request.Params("__EVENTTARGET")
lblMessage.Text &= "<br>RecordId = " & Request.QueryString("rid")
If IsPostBack Then
'pnlMainBody.Visible = False
End If
End Sub
End Class
End Namespace
-
Dec 22nd, 2010, 06:54 AM
#16
Re: IsPostBack issues
ok... so this is what I did and it is working now.
1. Modified the javascript code to return true for anything except ENTER.
Code:
<script language="javascript" type="text/javascript">
<!--
function GotoRecord() {
var tb = document.getElementById('txtRecordNumber');
if (isNaN(tb.value) || tb.value.indexOf('.') >= 0) {
tb.focus();
alert("Enter valid Record Number.");
return true;
}
else {
window.location = 'Test.aspx?rid=' + tb.value;
return false;
}
}
function txtRecordNumber_KeyPress(event) {
if (event.keyCode == 13) return GotoRecord();
}
-->
</script>
2. And then passed it on to onkeypress:
Code:
<asp:TextBox ID="txtRecordNumber" runat="server"
onkeypress="return txtRecordNumber_KeyPress(event);"
AutoPostBack="false"></asp:TextBox>
This is working for the time being, though I'll need to test more.
But still no idea about why it is happening in the first place. There is no default button for the form or any div/panel and AutoPostBack is also false.
-
Dec 22nd, 2010, 08:43 AM
#17
Re: IsPostBack issues
Hmmm, still don't know what to say about this one 
Will try and do some digging later.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|