So, I have a usercontrol with an update panel.
I also put a button for updating the panel on my control.
I include this control 2 times (or more) in a page.
I want update only one of them but when I use the button, both panel is updated.
control ascx
<script type="text/javascript">
function bt_click()
{
__doPostBack('UpdatePanel1', 'post');
return false;
}
</script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:HiddenField runat="server" ID="HiddenField1" Value="false" />
<div>
<asp:Label ID="Label1" runat="server" Text="Val"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
control .vb
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = DateTime.Now.Ticks.ToString();
}
}
webform.aspx
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="bt" OnClientClick="bt_click" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Label ID="Label1" runat="server" Text="Val"></asp:Label>
<div>
<My:MyControl ID="MyControl1" runat="server" />
</div>
<br />
<div>
<My:MyControl ID="MyControl2" runat="server" />
</div>
</div>
</form>
webform.vb
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Label1.Text = DateTime.Now.Ticks.ToString();
}
EDIT FOR WORKING SOLUTION:
Here’s what the error is: Your
Button1is outside of the UpdatePanels, thus posting back your page and all the controls with it. Hence both of them being updated. You can’t add the button in theTriggerssection of your control, because obviously the control doesn’t know about it. So what you have to do is register that button as an Asynchronous PostBack Control. Here’s what I did:TestControl.ascx:
Note: I had to add
UpdateMode="Conditional"to the UpdatePanel, which yours did not have. By default, UpdateMode is set to Always, and that would cause us further issues.TestControl.ascx.cs:
Note: I added UpdatePanel() as a property to get around needing to use your
__doPostBack()calls in jQuery and I can easily access this from the parent page in the click event handler.Test.aspx:
Test.aspx.cs:
Note: Here is where I registered
Button1in thePage_LoadwithScriptManager1.RegisterAsyncPostBackControl(Button1);