Linkbutton in ASP.NET MasterPage and child Webform data control creating extra href="" - asp.net

EDIT Final: Just in case someone else sees this behavior, I wanted to explain my work around. I was using a placeholder on my Master page for the webform described below. I added a server control to the placeholder in the Master page OnInit event. Through a process of elimination, I figured out that the behavior described below only happens when I add this server control to the Master page.
titlebarPlaceHolder.Controls.Add(sctitlebar)
I re-wrote the Master Page to not need the server control added and the behavior described below went away. I have no idea what caused it. It was a simple server control, but this is my work around.
EDIT 2: The same behavior happens when the container is a table in the repeater control:
<asp:Repeater ID="rptAuditList" runat="server">
<ItemTemplate>
<tr class="odd">
<td><asp:LinkButton ID="lnkOpenAudit" runat="server" Text='<%# Eval("auditname") %>'></asp:LinkButton> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
Here is the HTML output:
<td>Demo PreClose July 2012 </td>
EDIT: In my testing I just noticed that if I run the extact same control OUTSIDE of a MasterPage, it worked correctly, but if I run it inside a MasterPage, it behaves in the way described below.
I have tried this with a Repeater, DataList and Listview and the results are always the same.
Here is the HTML:
<asp:ListView ID="lvwAuditList" runat="server" >
<LayoutTemplate>
<ul><li runat="server" id="itemPlaceholder"></li></ul>
</LayoutTemplate>
<ItemTemplate>
<li><asp:LinkButton ID="lnkAudit" runat="server" Text='<%# Eval("auditname") %>' >
</asp:LinkButton></li>
</ItemTemplate>
</asp:ListView>
Here is the output:
<ul>
<li>Demo PreClose July 2012</li>
<li><a id="contentMain_lvwAuditList_lnkAudit_1" href="javascript:__doPostBack('ctl00$contentMain$lvwAuditList$ctrl1$lnkAudit','')">Demo PostClose Audit June 2012</a></li>
</ul>
The first row always has an extra href="" added. I have never seen this behavior before. I have stripped down the html and code behind to its most basic, yet I still get this extra href="". The code behind just sets the datasource and binds it, nothing else.
Thank you.

If you are using the first list tag as a placeholder and replacing the content in the code behind I would use a slightly different approach adding an ASP.NET PlaceHolder inside the list tag as follows:
<ul><li><asp:PlaceHolder runat="server" ID="itemPlaceholder" /></li></ul>
I have experienced some unusual artefacts appearing in Response code when using ASP.NET tags in a manner for which they were not particularly intended. This may well be causing your problem as well!

Related

asp:Menu stops working after a different control performs partial AJAX postback

This issue occurs in ASP.NET 4.6 and I've seen a few similar posts, but they usually referred not to the same control (built-in here) or ended up with a conclusion "just use a different/external control here: html link", which is not really an option for me.
First, some code
Site.Master
<div id="HeaderProper">
<div id="HeaderProperTitle">
<asp:Menu ID="HeaderProperMenu" runat="server" DataSourceID="HeaderProperSiteMap" Orientation="Horizontal"
BackColor="#ff2400"
RenderingMode="List"
StaticEnableDefaultPopOutImage="false"
StaticDisplayLevels="2"
StaticHoverStyle-BackColor="#000000"
StaticMenuItemStyle-HorizontalPadding="15px"
StaticMenuItemStyle-Height="42px"
DynamicHoverStyle-BackColor="#000000"
DynamicMenuItemStyle-HorizontalPadding="5px"
DynamicMenuItemStyle-BackColor="#ff2400"
DynamicMenuItemStyle-Font-Size="24px"/>
<asp:SiteMapDataSource ID="HeaderProperSiteMap" runat="server" />
</div>
SomePage.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h1>Complete List</h1>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="SortOrderSelection">
Sort by
<asp:DropDownList ID="cbxSortBy" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="cbxSortBy_SelectedIndexChanged" />
</div>
<asp:Panel ID="SortedList" CssClass="top-margin five-columns" runat="server" />
<asp:Panel ID="Summary" CssClass="top-margin" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</asp:Content>
How to reproduce:
Choose an item in the DropDownList, which causes partial postback. The menu then stops working, that is - the drop down/hover menu doesn't open, but the first level links seem to be functional. Refreshing the whole page fixes the problems (duh?).
And, contrary to what I've found:
1) Menu is NOT inside an UpdatePanel, which I acknowledge is unsupported solution
2) Menu works fine when RenderingMode is set to Table, but generates a very ugly html code, which I would like to avoid. Not mentioning additional quirks in margins that have to be adjusted with ugly fixes.
3) I tried setting z-index: 1000...0 !important as suggested by some sources (on most menu related styles), but to no avail.
I would be grateful for any suggestions how this can be resolved while still using asp:Menu control in List rendering mode, possibly with as least intervention as possible. My point here is to use built-in functionality and keep the code clean from unnecessary JS, jQuery (if possible at all; otherwise I'd rather open a Connect case for this issue).
Thank you in advance.
Putting your menu into an update panel should work because it will indicate to the server to update it after the postback. Without this, any repost can create the risk of losing some events in your element. Refreshing works because you are refreshing the whole page and not only some element of it.

Nested UpdatePanel causes parent to postback?

I'm under the impression that a control in a nested UpdatePanel will cause the top level UpdatePanel to refresh (thus refreshing both UpdatePanels) because any events on that control act as an "implicit" trigger. Is that correct?
I've been trying to wire something like this up-
UserControl
Parent UpdatePanel
"Show" button
ASP:Panel
Dynamically added UserControls, each with UpdatePanels
When the Show button is clicked, the ASP:Panel becomes visible and starts adding UserControls to itself dynamically, based on some back-end logic.
Each of the dynamically added controls (henceforth: UserControls) have their own Atlas-enabled buttons and links, so they have UpdatePanels too. Currently when I click on a link in one of the UserControls, the entire contents of the ASP:Panel disappear, as if it's being re-rendered. All of my dynamically-added controls disappear, and none of their click events are caught in the debugger.
I'm assuming what's happening here is that controls that reside in nested update panels are causing the parent UpdatePanel to post back because they're firing "implicit" triggers. Is there a way for my UserControls to operate autonomously and not mess with the ASP:Panel that contains them?
If not, what strategy should I be pursuing here? If I have to re-render the entire ASP:Panel every time an event happens on one of the (possibly many) UserControls, that means I'll have to recreate the UserControls, which take a bit of effort to create. I'll also have to preserve some kind of view state to recreate them. I'm somewhat new to ASP.NET and that sounds intimidating. I'd rather never refresh the top leve UserControl and ASP:Panel if I can avoid it, and let each of the dynamically-added UserControls fire and handle their own events asynchronously.
EDIT: Instead of adding the controls dynamically, I added them to the markup(not a bad solution). So got rid of the controls disappearing problem, because now the controls are not added dynamically but instead exist in the markup. But still the parent UpdatePanel posting is a big performance hit, because all the UserControls are getting posted instead of one. How do I make only one UserControl postback? Also, I would like to know how to get rid of the problem of controls disappearing if added dynamically?
First off, keep in mind: UpdatePanels do not alter the lifecycle of a page.
All of the Control Tree (including the UpdatePanels) must be reconstructed just as with a Normal Postback Life-cycle.1 2 The UpdatePanels ensure that only a portion of the rendered (HTML) view is returned. Removing all UpdatePanels should result in the same behavior, except with a full postback. For instance, only the HTML representing a nested UpdatePanel (presumably because data changed) might be sent back in the XHR response.
To get "true" AJAX consider Page Methods. Alternatively, DevExpress (and perhaps Telerik and others?) offer their own form of "Callback Panels", which are similar to UpdatePanels, but can bypass parts of the life-cycle (and, as a result often do not support the ViewState model entirely or may introduce their own quirks).
While not understanding the above is the most likely reason for the controls "disappearing", here is my rule: Do Not Let [Nested] UpdatePanels Work "Automatically".
Edge cases with dynamic controls and nested UpdatePanels will be encountered. There might be a nice way to handle this, but I have failed on multiple different attempts.
Instead, for every update panel, run with:
UpdateMode="Conditional"
ChildrenAsTriggers="False"
With the "Conditional" UpdateMode, make sure to manually specify the Trigger control or call panel.Update() (although this hard-wires the Control) as required. Depending on needs ChildrenAsTriggers="True" might work as well. The big thing is that UpdateMode is not "Always" which is the default.
After switching to this approach, I have no problem -- well, almost none -- with nested UpdatePanels.
Happy coding!
1 If the page doesn't render correctly where Partial Rendering (in the ScriptManager) is disabled (e.g. all requests are full postbacks) then there is no reason to expect/believe it will work correctly with UpdatePanels.
2 There are times when it's warranted to "cheat" expensive recomputations in the control tree for controls that will not be re-rendered. However, I would consider these advanced cases that should only be done when performance analysis indicates there is a specific need.
I had a similar issue with a heavy ajax Gridview control, and a HTML page with multiple UpdatePanels, some nested, some not.
It took me over 3 weeks of trial and error, reading, testing, debugging and prototyping to finally resolve all the post backs and partial postbacks.
However I did notice a pattern, which worked for me.
Like the #user above's response, make sure your UpdatePanels are set Conditional and only specificy asp:PostBackTrigger on the final or decisive control that you want the page to do a full postback on. I always use asp:AsyncPostBackTrigger where ever possible.
So for example, if you're using UpdatePanels inside a GridView and you want a popup inside a cell of the gridview's row, then you need to use a nested UpdatePanel.
ie
<asp:TemplateField HeaderText="Actions & Communications">
<ItemTemplate>
<asp:UpdatePanel ID="upAction1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnActionOK" />
</Triggers>
<ContentTemplate>
...
...
<ajaxToolkit:ModalPopupExtender ID="ajaxMPE" runat="server"
BackgroundCssClass="modalBackground"
PopupControlID="upAction2"
TargetControlID="btnADDaction">
</ajaxToolkit:ModalPopupExtender>
<asp:UpdatePanel ID="upAction2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlACTION" runat="server" CssClass="pnlACTION">
<div id="divHDR">
<asp:Label ID="lblActionHdr" runat="server">** header **</asp:Label>
</div>
<div id="divBOD">
<table style="width: 98%; text-align:left">
<tr>
<td colspan="2">
Please enter action item:<br />
<asp:TextBox ID="txtAction" runat="server" ValidationGroup="vgAction" TextMode="MultiLine" Rows="3" Width="98%"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAction" runat="server" ControlToValidate="txtAction" ValidationGroup="vgAction"
ErrorMessage="* Required" SetFocusOnError="True"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td colspan="2">
Select staff assigned to this task:<br />
<asp:DropDownList ID="ddlActionStaff" runat="server" AppendDataBoundItems="true" ValidationGroup="vgAction" />
<asp:RequiredFieldValidator ID="rfvStaff" runat="server" ControlToValidate="ddlActionStaff" InitialValue="0" ValidationGroup="vgAction"
ErrorMessage="* Required" SetFocusOnError="True" Width="98%"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button ID="btnActionOK" runat="server" Text="OK" OnClick="btnActionOK_Click" ValidationGroup="vgAction" CausesValidation="false" />
<asp:Button ID="btnActionCANCEL" runat="server" Text="CANCEL" OnClick="btnActionCANCEL_Click" ValidationGroup="vgAction" CausesValidation="false" />
</td>
</tr>
</table>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
<asp:SqlDataSource ID="sdsTETactions" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
...
...
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
Notice a few things here:
The ModalPopupExtender does not refer to the Ok and Cancel buttons. This is because I want them to trigger a partial postback to the server (code-behind)
The nested UpdatePanel (upAction2) is purely to force a partial postback on that panel only (pnlAction)! Therefore this forces the data validation triggers to work and keep the panel open if the user does not provide data in the requiredfield validators
The main UpdatePanel (upAction1) controls the entire lot and keeps all postbacks held within that panel only and NOT affecting the gridview (not shown entirely).
There is a great article here, which explains it better.
I hope this helps someone

Cannot remove viewstate hidden field

I have a massive viewstate hidden field that is causing my application to be unworkable. I have tried:
EnableViewState="false" on every control
EnableViewState="false" in page directive
Page.EnableViewState = false in Page_Init
<pages enableViewState="false" /> in web.config
The page causing the issue has a single GridView which I want to render once only, so I don't ever need the viewstate.
I examined the hidden field using this tool, and there is apparently hardly any info in it (since I disabled the property in every control probably). For some reason though, the page insists on including a hidden field that is thousands and thousands of lines long.
How can I get rid of this field (or reduce it to a usable size) for good?
Here is an exert from the offending GridView:
<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" EnableViewState="False"
CssClass="my-report">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<span title='title' class="abbr">My ID</span>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("my_id") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<span title='title2' class="abbr">Second col heading</span>
</HeaderTemplate>
<ItemTemplate>
<asp:ListView ID="MyListView" runat="server" EnableViewState="False">
<LayoutTemplate>
<ul>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" EnableViewState="False" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><%# Eval("field_2")%></li>
</ItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The hidden field you see on the page is not only for ViewState, it also contains the ControlState. There is no way to disable the control state so you'll need to find a way to live with it. How many items the grid is displaying?
As a last option you may consider compressing generated viewstate field.
Here you have an MSDN article explaining how ControlState works
If your GridView is non-interactive (that is, it doesn't contain any child controls that post back), then you can reduce the size of view state by waiting until the page's Render method is called to bind the grid:
Protected Overrides Sub Render(writer As HtmlTextWriter)
MyGrid.DataSource = ...
MyGrid.DataBind()
MyBase.Render(writer)
End Sub
In case anyone has a similar problem, it was occuring because I had a ListView inside each row of the grid. I replaced the ListView with a Repeater and the viewstate is no longer a problem.
Another option is to use Flesk.ViewState something.
It can put the viewstate on files, compress it, session, etc.
Like the others say, sometimes is inevitable in ASPNET to live with ViewState.
Thats why your best option is to move to MVC :)

ASP:Login <LayoutTemplate> always generates a <table>, how can I make it stop?

I've just started tinkering with the ASP:Login control, and want to edit its appearance. So I did the following:
<asp:login ID="login" runat="server" onauthenticate="Authenticate">
<LayoutTemplate>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
</LayoutTemplate>
</asp:login>
Despite the fact that I have no <table> tag anywhere in the document, once I preview the page and view the source, it very clearly shows a <table> there. Ah! How in the world do I prevent this crazy behavior, or am I forced to use tables for layout instead of CSS?
You can set it not to use tables without third party extensions, just make sure you use the
<LayoutTemplate>
</LayoutTemplate>
For laying out your HTML/form inside the control, then set the attribute on the Login that controls the outer table to false.
RenderOuterTable="false"
That's it, no tables :)
You can use the CSS Friendly control adapter for the login control to change it.
http://www.asp.net/CSSAdapters/Membership/Login.aspx
There's a number of things you can do.
The easiest would be to use the Css Control Adapters toolkit's version of the Login control, although it hasn't been updated in a while and I haven't used it recently, so maybe it's not a great option anymore, I'm not sure.
Otherwise you could try creating your own ITemplate and setting it as the property of the LayoutTemplate for the Login control.
Alternately you could rewrite the generated HTML with an IHttpHandler, or even redo it on the client with something like jQuery dom replacement.

AJAX.Net - UpdatePanel doesn't delete old content

I'm using AJAX.Net (3.5) inside a usercontrol.
The usercontrol contains an UpdatePanel, and inside the UpdatePanelthere is a MultiView.
The ScriptManager is included in the page that act as container for the usercontrol.
To switch between views the usercontrol contains a simple button.
When I click it, the View is changed so the old content is hidden and new content is displayed.
My problem is that the content isn't hidden at all.
The view changes and the new content is displayed, but the old one remains on the page.
To isolate the problem, I tried changing the multiview and switching visibility of a simple label, but the behavior is the same.
Any ideas?
oh I understand. It's all right then. The problem is not of Ajax here. It's just you cannot embed something in <table> tags. In this case, you can try something different than the <table> control. Maybe a <div> or something else. I don't know exactly what sort of situation you have. Maybe you explain the result you want to achieve so I can give you some advice.
Regards
It seems that AJAX.Net doesn't work very well if you have part of a table outside the UpdatePanel.
On my control I want to show or hide some rows of a table. I included only the tr and td tags inside the updatepanel.
To reproduce the problem:
<table>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<tr>
<td>
<asp:Label ID="lblToShow" runat="server" Text="Label to show" Visible="false" />
<br />
<asp:Label ID="lblToHide" runat="server" Text="Label to hide" />
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
</table>
If you change the visibility using:
lblToShow.Visible = true;
lblToHide.Visible = false;
The text of both labels are shown on the page (lblToHide does not hide)
If you move the table tags inside the UpdatePanel everything works fine.
call
updatepanel.Update()
after you make the changes to your updatepanel
or try
updatepanel.Controls.Clear();

Resources