1

i have a question ( i am learning VB) is there is a way in vb.net to make the Me.LoadComplete loads after the client side , i develop this simple code to make it easy to understand my question

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 Handles    Me.LoadComplete
    MsgBox(myhiddenField.Value)
End Sub

Client

 </script>
<title></title>
<script type="text/javascript">
    $(document).ready(function() {
           $("#<%=myhiddenField.ClientID%>").val('this is the value ') ;
    });

</script>

<form id="form1" runat="server">
<div>
    <asp:LinkButton ID="myLink" runat="server"
      PostBackUrl="~/Default.aspx?value=1" >
      MyLink </asp:LinkButton>

    <asp:HiddenField ID="myhiddenField" runat="server"/>

</div>
</form>

when the page loads , the MsgBox will be empty but when you click the link button the MsgBox has the value you set in the javascript , so i assume that the page_load function gets excuted first the question is how can i load the client content before the page_load ?

4

3 回答 3

2

You can't. All of the server side code must be entirely finished before the request can be sent to the client, and none of the client side code can run until then.

If you want to have server side code run after client side code then you need to send a page to the client, have it run some javascript, and then send an another request back to the server so that it can run some more server side code.

If you were a bit more specific about what you're actually trying to accomplish, rather than in the means in which you are trying to actually do it, then we could probably be more helpful.

于 2012-06-14T13:36:42.493 回答
2

The ASP.NET Lifecycle says no because content isn't pushed to the browser until Page_Render, which happens after Page_Load.

You could try doing a bunch of work in Page_Init, and use Response.Flush() to force the server to send content to the browser, but

  • You'd only be pushing the content created already, so it wouldn't allow you to push the WHOLE page to the browser before Page_Load
  • I'm not sure it would work
  • Even if it did, there'd be side-effects. For example, you'd probably need to manage control creation, Viewstate value application, etc.

Even if it is possible in any way, shape, or form (which I doubt) - It would be an interesting experiment, but I wouldn't recommend designing a live app around the idea. The Page Lifecycle is something every ASP.NET developer is (or should be) familiar with, and doing goofy things like that just make it harder for maintenance programmers.

于 2012-06-14T13:38:43.880 回答
1

I think you may have asked your question a little incorrectly. I upvoted @DavidStratton because you absolutely have to learn more about the page lifecycle before you can continue, but as I read your question, it looks like you're asking how to populate the hidden field with a value before the PostBack occurs. This is actually workable from the client side. Since you're already using jQuery I'll keep that going in my sample:

Here is the html version of your aspx:

<form id="form1" runat="server">
<div>
    <asp:LinkButton ID="myLink" runat="server"
        PostBackUrl="~/Default.aspx?value=1" >
            My Link</asp:LinkButton>

    <asp:HiddenField ID="myhiddenField" runat="server"/>
</div>
</form>

Now these controls are created on the first render, so you don't know what that will be until the page is rendered. However, you can access this in your script by using the MyControl.ClientID property to access it. This is a concept you've at least touched on since it's used in your code, but I'm not sure you've gotten it all the way. Since you want to use the click event of the LinkButton, you'll need to assign a javascript action to its click event.

<script type="text/javascript">
    $(
        $("#<%=myLink.ClientID%>").click(
            function() {
                $("#<%=myhiddenField.ClientID%>").val('this is the value ');
            });
    );    
</script>

The $(document).ready() method is an older syntax. Newer jQuery modules use $(handler); The above script will assign an extra step before the postback. That extra step should set the value of your hidden field, and then the postback should occur in its own course and the value of the hidden field should be visible during Page_Load.

于 2012-06-14T13:51:54.903 回答