ASP.NET page is posting back but "isPostBack" is false?

KevySaysBeNice

[H]ard|Gawd
Joined
Dec 7, 2001
Messages
1,452
Hi all!

I've got a fairly simple page I'm working on in ASP.NET/VB.NET and I'm having a strange problem that I can't figure out.

Essentially I have a form with an <asp:button> on it. When clicked, the page_load sub in the code behind is called, and so it appears that it's a "postback" however the "ispostback" variable is set to false.

Essentially my page looks like this:

ASP.NET
Code:
<table>
<tr>
                <td class="editformtitle">Video File Name</td>
                <td><asp:TextBox ID="tbxVideoFileName" runat="server" CssClass="videoeditbox" Columns="70"></asp:TextBox></td>
            </tr>    
            <tr>
                <td class="editformtitle">Thumbnail File Name</td>
                <td><asp:TextBox ID="tbxThumbnailFileName" runat="server" CssClass="videoeditbox" Columns="70"></asp:TextBox></td>
            </tr>           
            <tr>
                <td class="editformtitle">Video Width</td>
                <td><asp:TextBox ID="tbxWidth" runat="server" CssClass="videoeditbox" Columns="5"></asp:TextBox></td>
            </tr>
            <tr>
                <td class="editformtitle" >Video Height</td>
                <td><asp:TextBox ID="tbxHeight" runat="server" CssClass="videoeditbox" Columns="5"></asp:TextBox></td>
            </tr>
            <tr>
            <td colspan="2" style="padding-top:20px; text-align:center;">
                <asp:Button ID="btnSubmit" runat="server" Text="Submit Video" CssClass="videoeditsubmit" />                
            </td>
            </tr>


VB.NET (code behind) - Note that this is where I'd expect IsPostBack to be true (after clicking the submit button) however it's always set to false.
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try

            If Not IsPostBack Then
                If Not _blnNewVideo Then
                    PopulateFields()
                End If

            Else
                SubmitVideoChanges()
            End If



        Catch ex As Exception

        Finally

        End Try
    End Sub
 
I think you're approaching the problem incorrectly. Try this instead:
- Remove the "Else" clause from your Page_Load method.
- Create a new method from the button's click event (tied to "OnClick"). You can make this by hand, or goto the "Design" view of the aspx and doubleclick on the displayed button.
- Put the "SubmitVideoChange()" call in the button's "onclick" event in your code-behind.

Basically use the Page_Load for the initial stuff to happen on the first page load. Use other defined methods to intercept and process actual postbacks.
 
Thanks for the help!

I've looked at a few tutorials, and this seems like it SHOULD work:

I changed the button to:
Code:
<asp:Button ID="btnSubmit" runat="server" Text="Submit Video" OnClick="submit" CssClass="videoeditsubmit" />
and in my code behind I have
Code:
Public Sub submit(ByVal sender As Object, ByVal e As System.EventArgs)
        SubmitVideoChanges()
    End Sub

But, when I step through the code and click the "submit" button, my submit sub is never called, however the page_load is called.


Any other tips? I basically did this: http://www.w3schools.com/ASPNET/aspnet_button.asp
 
Here's a quick snippet I threw together:

Default.aspx
Code:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" EnableViewState="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<p><asp:Label ID="lblTime" runat="server"></asp:Label></p>
		<p><asp:Button ID="btnSubmit" runat="server" Text="Submit" /></p>
    </div>
    </form>
</body>
</html>


Default.aspx.vb
Code:
Partial Class _Default
    Inherits System.Web.UI.Page

	Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
		lblTime.Text = lblTime.Text & "<br />" & "Button Click -- " & DateTime.Now
	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If (Not Page.IsPostBack) Then
			lblTime.Text = "Page Load -- " + DateTime.Now
		End If
	End Sub

End Class


Notice the "Handles" keyword near the end of the codebehind events. This is what wires the two together on a VB.Net codebehind. Drop a breakpoint in each method of the code behind if you want to follow the execution path.

Couple of extra suggestions:
- Change the "Public" level of exposure to "Protected".
- Change the declaration line of your "aspx" page to have this attribute: AutoEventWireup="true"
More info on AutoEventWireup.
 
Thank you a ton for the help - I'm going to read the AutoEventWireup info as soon as I finish typing this :)

That said, I think I found my problem, and I don't believe it has to do with the button/etc/etc.

The problem seems to stem from the fact that rather then inheriting System.Web.UI.Page
, I'm inheriting our own "cPage". cPage inherits System.Web.UI.Page, but has it's own "on_load" sub and Page_Prerender sub.

Even with a super simple page with nothing but your code on it, when I switch from System.Web.UI.Page to cPage, everything stops working. And by everything, I mean things like IsCallBack, the OnClick value in the asp:button control, etc.

So I guess for now this is a bigger issue then I had hoped.

cPage is made up of public subs, and the page_load and page_prerender subs.

If you might have any other hints as to what's going on, I'd appreciate it, but I am unable to modify the cPage class and I need to use it's public methods on my page. I might be able to just make an instance of the cPage class and use the methods that way, rather then referencing them directly (if that makes sense).

Thank you very very much!
 
Thank you a ton for the help - I'm going to read the AutoEventWireup info as soon as I finish typing this :)

That said, I think I found my problem, and I don't believe it has to do with the button/etc/etc.

The problem seems to stem from the fact that rather then inheriting System.Web.UI.Page
, I'm inheriting our own "cPage". cPage inherits System.Web.UI.Page, but has it's own "on_load" sub and Page_Prerender sub.

Even with a super simple page with nothing but your code on it, when I switch from System.Web.UI.Page to cPage, everything stops working. And by everything, I mean things like IsCallBack, the OnClick value in the asp:button control, etc.

So I guess for now this is a bigger issue then I had hoped.

cPage is made up of public subs, and the page_load and page_prerender subs.

If you might have any other hints as to what's going on, I'd appreciate it, but I am unable to modify the cPage class and I need to use it's public methods on my page. I might be able to just make an instance of the cPage class and use the methods that way, rather then referencing them directly (if that makes sense).

Thank you very very much!
Is your cPage an abstract or a partial class?

Is there a need to have a common set of events fire specifically in the Page_Load and/or Page_PreRender events for every page that inherits cPage?
 
Is your cPage an abstract or a partial class?

Is there a need to have a common set of events fire specifically in the Page_Load and/or Page_PreRender events for every page that inherits cPage?

Long story short, yes and no...

For this particular page I'm writing I don't really need the page event subs, so I created an instance of the class so I could access it's methods.

So, long story short, I think I should be good to go now!

Thanks a ton, I really appreciate the help :).
 
Bonus question that is almost unrelated but is something I'm curious about:

This code above is going to be used for making an administration page for a website. Anyway, there is an "index" page that lists of all of the videos on the site. The way I'm building the list is by using a repeater, with my divs/etc inside of it. Then I've got things like:
Code:
<tr>
                                <td class="itemname">
                                    W/H
                                </td>
                                <td>
                                    <%#Container.DataItem("width")%>/<%#Container.DataItem("height")%>
                                </td>
                            </tr>

In side the <ItemTemplate> section.

I'm using a repeater because I have divs/etc also, and I didn't want any markup output from the repeater.

Anyway, the question is this: Say some videos don't have a width/height set, and I want some default width/height to be displayed by default, how do I do that? Or say I want to apply logic, say if the width is greater then 10 I want to ouput "error" or something. How does one go about doing that?

I've seen something about handling the "databound" event, but the tutorials I've seen seem to be overkill for what I want to do.

Any "pro tips"? If not, thanks anyway for all of the help above!
 
The approach you're doing is some late-binding of data, and is one approach for raw data injection from bound collections.

You're on the right track with the RowDataBound (or perhaps OnRowDataBound). One possible approach is this:
- Swap out the various "Container.DataItem" references, and create individual asp:Label controls.
- Then when your row data bound event fires for the individual row, you can cast locally defined objects equal to a reference to the current label object. The "FindControl()" method will help. Do any business checks then and assign values to the label as needed.
 
Can you look at the code for cpage? If so, can see if cpage calls base.OnLoad()? If it doesn't it needs to be added. I added a c# snipped, because I had it handy.

Code:
protected override void OnLoad(EventArgs e)
        {
            //custom code
            //custom code
            //custom code

            base.OnLoad(e);
        }
 
Back
Top