events do not raise the first time

Last post 11-27-2008 11:54 AM by tulipvn. 15 replies.

Sort Posts:

  • events do not raise the first time

    11-23-2008, 6:00 PM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

     Hi,

    I dont like the whole page does postback every time a new page is loaded. So my idea is to place a PlaceHolder in an updatePanel. There are another buttons in the the UpdatePanel as well to change the user controls of the PlaceHolder.

    The problem is that all buttons or events on the user control inside the PlaceHolder, that only work from the second time (do not work at the first time click). REALLY STRANGE!!!

    At this moment, I have an idea to add a counter for every user control inside the Page_Load to raise the event until the second time then stop this raising...

    But I dont think this is a good solution... Therefore I really kindly ask your help....

    I dont know whether my implementation to reduce postback for the whole page using PlaceHolder inside an UpdatePanel will always cause this problem or faulty of Asp.net... or maybe there is a solution to fix my problem...?

    Any help? I am very appreciated!

    Thanks in advance.

     

  • Re: events do not raise the first time

    11-24-2008, 5:11 AM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78
    Anyone, please?
  • Re: events do not raise the first time

    11-24-2008, 5:16 AM
    • Member
      118 point Member
    • Henxon
    • Member since 06-16-2008, 1:42 PM
    • Posts 109

     Are the buttons dynamically generated?

  • Re: events do not raise the first time

    11-24-2008, 5:24 AM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

     No, they are all static buttons...

     Thanks for your reply.

  • Re: events do not raise the first time

    11-24-2008, 5:43 AM
    • Member
      118 point Member
    • Henxon
    • Member since 06-16-2008, 1:42 PM
    • Posts 109

    Just a tip: I tried to put my entire site inside an update panel a while ago. Though, it didn't take me long time to figure out that it's not a good way to use AJAX for many reasons; you can't put some of the standard components (file upload for example) on your site, debugging gets harder since the update panel can be the cause of some things not working, the most of the contents will reloaded instead of the exaxt data of for example a single gridview.

    Will your buttons work if you use update panels inside the content holder? Remember to use updatemode="conditional"...

    Regards

    Henxon

  • Re: events do not raise the first time

    11-24-2008, 6:21 AM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

     Hi Henxon,

    I tried to place an update panel inside the placeholder with updatemode="conditional", but still it only works from the second time on...

    :( 

     Thanks, 

    Any other ideas?

  • Re: events do not raise the first time

    11-24-2008, 6:37 AM
    • Member
      118 point Member
    • Henxon
    • Member since 06-16-2008, 1:42 PM
    • Posts 109

     Where do u check for the button press? Inside the buttons press function or inside the page_load?

  • Re: events do not raise the first time

    11-24-2008, 6:46 AM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

    Hi Henxon,

    Off-course, I do it inside button function...

    I just disable all the ajax update panel, but it still only works from the second time :(

     I upload my code to here http://www.sendspace.com/file/dd4j12

    Could you please help me to check what is wrong here?

    Thanks 1000 times...

    Very appreciated if you get this wroking...

  • Re: events do not raise the first time

    11-24-2008, 9:00 AM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78
    Any help, please?
  • Re: events do not raise the first time

    11-24-2008, 9:06 AM
    • Member
      118 point Member
    • Henxon
    • Member since 06-16-2008, 1:42 PM
    • Posts 109

     It's tricky to debug code that I can't rune since I lack alot of your classes. Allthough - u load the ascx dynamically. Try to load it in the aspx-code instead, so that it just loads once and see if that makes the buttons work correctly

     

    /henxon

  • Re: events do not raise the first time

    11-24-2008, 10:42 AM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

    Hi Henxon,

     Yes, I loade asxc dynamically because I want to add an updatepanel to catch postback.

    I am sure that If I add it in aspx pages seperately. It will work... 

    However my idea is to reduce postback of the pages by using placeholder and user controls.

    I think the source code I uploaded is enough to demonstrate my idea..but :( 

    Any other ideas?

    Thanks, Henxon.

  • Re: events do not raise the first time

    11-24-2008, 11:06 AM
    • Member
      118 point Member
    • Henxon
    • Member since 06-16-2008, 1:42 PM
    • Posts 109

     In that case: make sure u load the ascx-file on every postback since dynamically added objects must be reloaded to be able to use their buttons etc.

    Not sure if it should be placed in page_init or page_load.. Have u tried both?

    /Henxon

  • Re: events do not raise the first time

    11-24-2008, 11:26 AM
    Answer
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

     Hi Henxon,

    Yes, I have tried both... :( 

    THanks

  • Re: events do not raise the first time

    11-24-2008, 1:17 PM
    • Member
      72 point Member
    • tulipvn
    • Member since 09-28-2005, 11:35 AM
    • Eindhoven - The Netherlands
    • Posts 78

     any help, please?

  • Re: events do not raise the first time

    11-27-2008, 4:55 AM
    Answer

    Hi tulipvn,

    I have checked all your code uploaded in this page http://www.sendspace.com/file/dd4j12. After building a sample of it, I assume your scenario is there are many UserControls which can be dynamically added into the PlaceHolder of page by clicking the related ImageButton. Each UserControl has its own function that we don’t want to affect the whole page, i.e. after clicking the ‘Save’ button of the ‘Account’ UserControl, the Data of the ‘Account’ can be saved but the whole page would not post back. If I have misunderstood you, please tell me.

    So far, you have tried to enclose the UserControl with an UpdatePanel by placing it either in the UserControl or in the PlaceHolder, and I think each method won’t achieve the goal. Firstly, if the UpdatePanel is in the UserControl, then when we add the UserControl in the button_click event, there will raise one error caused by the unregistered UpdatePanel could not be added or removed dynamically in the Control Tree. In the other hand, placeing the UpdatePanel inside the PlaceHolder can’t help the UserControl’s “self” asynchronous postback.

    What we can do is call the WebService at the client side by handling the ‘Save’ button’s onclick event. There is a detailed tutorial about this:
    http://www.asp.net/AJAX/Documentation/Live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx

    By referring to this link, I have built a sample of your solution, please refer to it (I’m sorry it’s written by VB, maybe you can convert it to C#):

    .aspx file 

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestPlaceHolderInsideUpdatePanel.aspx.vb"
    Inherits="Test_UpdatePanel.TestPlaceHolderInsideUpdatePanel" %>


    <!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>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
    <asp:ServiceReference Path="~/UpdateWebService.asmx" />
    </Services>
    </asp:ScriptManager>
    <asp:UpdatePanel ID="PageUpdatePanel" runat="server">
    <ContentTemplate>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Button ID="Button1" runat="server" Text="LoadControl1" />
    <asp:Button ID="Button2" runat="server" Text="LoadControl2" />
    </ContentTemplate>
    </asp:UpdatePanel>
    </div>

    <script type="text/javascript">
    //the function handles the onclientclick event of the save button
    function callWebService(e) {
    //get the input value by modifying the SaveButton's id
    var txtEmailID = e.id.replace("ImageButton1", "TextBox_Email");
    var inputvalue = $get(txtEmailID).value;
    //call the service method to get data from web service and handle the success call back event.
    Test_UpdatePanel.UpdateWebService.GetDataFromService(inputvalue, SucceededCallbackWithContext, FailedCallback, e.id);
    }
    // This is the callback function invoked if the Web service
    // succeeded.
    // It accepts the result object as a parameter.
    function SucceededCallbackWithContext(result, userContext, methodName) {
    // get the corresponding span to display feedback by using the context.
    var lblResultID = userContext.replace("ImageButton1", "Label_TextResult");
    var lblResult = $get(lblResultID)
    lblResult.innerText = result;
    }

    // This is the callback function invoked if the Web service
    // failed.
    // It accepts the error object as a parameter.
    function FailedCallback(error) {
    // Display the error.
    alert("Service Error: " + error.get_message());
    }
    </script>

    </form>
    </body>
    </html>
    .aspx.vb file 
    Partial Public Class TestPlaceHolderInsideUpdatePanel
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If IsPostBack Then
    AddUC(String.Empty)
    End If
    End Sub

    Protected Sub
    Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    AddUC("Button1")
    End Sub

    Protected Sub
    Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    AddUC("Button2")
    End Sub

    Protected Sub
    AddUC(ByVal ucName As String)

    If PlaceHolder1.Controls.Count = 1 Then
    PlaceHolder1.Controls.Clear()
    End If

    Dim
    uc1 As New TestAccountUC
    If Not String.IsNullOrEmpty(ucName) Then
    uc1 = LoadControl("TestAccountUC.ascx")
    uc1.message = ucName
    Session("uc") = uc1
    Else
    If
    Session("uc") IsNot Nothing Then
    uc1 = CType(Session("uc"), TestAccountUC)
    Else
    Exit Sub
    End If
    End If

    PlaceHolder1.Controls.Add(uc1)
    End Sub
    End Class
    .ascx file 
    <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestAccountUC.ascx.vb"
    Inherits="Test_UpdatePanel.TestAccountUC" %>

    <asp:TextBox ID="TextBox_Email" runat="server"></asp:TextBox><asp:CheckBox ID="CheckBox1"
    runat="server" />
    <%--The "return false" is important to disable the Server-Side event--%>
    <asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="callWebService(this);return false;" />
    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
    ControlToValidate="TextBox_Email" ErrorMessage="Email is not valid"></asp:RegularExpressionValidator>
    <asp:Label ID="Label_TextResult" runat="server" Text="Label"></asp:Label>
    .ascx.vb file 
    Partial Public Class TestAccountUC
    Inherits System.Web.UI.UserControl

    Private sMessage As String
    Public Property
    message() As String
    Get
    Return
    sMessage
    End Get
    Set
    (ByVal value As String)
    sMessage = value
    End Set
    End Property

    Protected Sub
    Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
    loadUser()
    End Sub

    Protected Sub
    Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub

    Private Sub
    loadUser()
    TextBox_Email.Text = "123@123.com"
    Me.Label_TextResult.Text = sMessage
    End Sub


    '' this function is disabled to prevent the post back
    'Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
    'End Sub


    End Class
    .asmx file 
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel

    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class UpdateWebService
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function GetDataFromService(ByVal sData As String) As String
    ' we can do the similar funtion about saving Data or geting other value
    Return "The Data From Service is " + sData + "!"
    End Function

    End Class
    Have my suggestions helped?

    Best regards,

    Zhi-Qiang Ni

     

    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
Page 1 of 2 (16 items) 1 2 Next >