Wednesday, March 28, 2012

LinkButton & UpdatePanel

I have dynamically added a linkbutton inside of an updatepanel. For some reason that I don't understand when I click on the linkbutton the entire page refreshes. I can't figure out why. It's in a content page which uses a master page. I've tried using Conditional & Always. I've tried adding anAsyncPostBackTrigger for the linkbutton Click, but no matter what the entire page refreshes. If I use a non-dynamically added linkbutton (see LinkButton1 below) it only updates the updatepanel. Any ideas?

Here are some code snippets:

<asp:UpdatePanel ID="FlyoutUpdate" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlSingleCalItem" runat="server" Height="270px" Width="350px">
<asp:Label ID="lblDateHeader" runat="server" Text="test"></asp:Label>
<br /><br />
<table width="100%">
<tr><td align="left" style="background-color:#E6E7E8">
<asp:Label ID="lblCalTitle" Font-Bold="true" Font-Size="13px" Font-Names="Arial" ForeColor="#96B96E" runat="server" Text="Title"></asp:Label>
<br />
<asp:Label ID="lblCalLocation" runat="server" Font-Size="12px" Font-Names="Arial" ForeColor="#BA906C" Text="Location"></asp:Label></td></tr>
<tr><td align="left"><asp:TextBox BorderWidth="0px" BorderStyle="none" Font-Size="11px" Font-Names="Arial" ID="txtCalMessage" runat="server" Width="350px" Height="200px" TextMode="multiLine"></asp:TextBox></td></tr>
</table>
</asp:Panel>
<asp:Panel ID="pnlMultipleCalItems" runat="server" Height="270px" Width="350px">
<asp:Label ID="lblDateHeaderMulti" runat="server" Text="test"></asp:Label>
<br /><br />
<asp:Table ID="tblMultipleCalItems" BorderWidth="0px" BorderStyle="none" runat="server">
</asp:Table>
<br /><br />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</asp:Panel>

</ContentTemplate>
</asp:UpdatePanel>

Dim hypAs New LinkButtonhyp.Text = varNameAddHandler hyp.Click,New EventHandler(AddressOf hyp_Click)hyp.Visible = varVisibleDim trAs New TableRow()Dim td1As New TableCell()td1.Controls.Add(hyp)Dim tblAs Tabletbl = pnlMultipleCalItems.FindControl("tblMultipleCalItems")tr.Cells.Add(td1)tbl.Rows.Add(tr)

Hi

When you add something to the page dynamically,you should add it everytime the page been posted back.

If you don't add it again when the page been Ayncpostbacked,the contorl is null,and it's click event handler method isn't executed.

Thanks


Hi Jim-Yu,

That is a good idea, but NO... LinkButton as a child control of a custom control causes a PostBack not a Callback. Here's some sample code the shows the bug in LinkButton. Put this custom control on an UpdatePanel and notice the "button" does a nice callback, and the "link" does a postback.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace testing
{
/// <summary>
/// Summary description for Test
/// </summary>
public class Test : Control, INamingContainer
{
public Test()
{
//
// TODO: Add constructor logic here
//
}

protected override void CreateChildControls()
{
CreateThem();
base.CreateChildControls();
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}

private void CreateThem()
{
LinkButton lnk = new LinkButton();
lnk.Text = "link";
Button btn = new Button();
btn.Text = "button";

this.Controls.Add(btn);
this.Controls.Add(lnk);
}
}
}


Hincornillon,

Thank you for your feedback,

You can change your code to:

private void CreateThem()
{
LinkButton lnk = new LinkButton();
this.Controls.Add(lnk);
lnk.Text = "link";
ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(lnk);

Button btn = new Button();
this.Controls.Add(btn);
btn.Text = "button";
// ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(btn);
}

Best Regards


I'm afraid it's still not working. When I dynamically create the controls I'm putting them in a Session array. Then in page_init I'm re-generating them like this below. When I'm looping through the Request.Form, I never see the link button.

If Session("ServerControls")IsNotNothingThen
aServerControls =TryCast(Session("ServerControls"), ArrayList)
Else
aServerControls =New ArrayList()
EndIf

ForEach strAsStringIn aServerControls
Dim hypAsNew LinkButton
Dim hypNameAsString = str
hypName = Replace(hypName,"/","_")
hyp.Text = hypName
AddHandler hyp.Click,New EventHandler(AddressOf hyp_Click)
pnlMultipleCalItems.Controls.Add(hyp)
ScriptManager.GetCurrent(Me.Page).RegisterAsyncPostBackControl(hyp)
Next

'Detect what caused the postback
Dim obj
ForEach objIn Request.Form

Next


Since there is a limited amount of controls to create, I manually created them and am just setting them either visible or not. It was faster. Thanks for the help!

No comments:

Post a Comment