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