ASP.NET - ViewState: empty placeholder generates view state - asp.net

On my web-page I have PlaceHolder, not controls are loaded into it.
<asp:PlaceHolder ID="PlaceHolderStatMain" runat="server">
</asp:PlaceHolder>
I am looking the ViewState generated for the page, it is the following:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTg1NDkyNTUzD2QWAgIDD2QWAgIND2QWAmYPZBYCAgEPZBYCZg9kFgJmD2QWBmYPFQEYL3N0YXRfc3RhZGl1bS9sZWFndWVfV0VGZAIBDxUBGC9zdGF0X3N0YWRpdW0vbGVhZ3VlX0VFRmQCAg8VARgvc3RhdF9zdGFkaXVtL2xlYWd1ZV9GQ1VkZEuSBUr5LFL6WfCehNBJgjrq0GzwWCWN2qlU70V7LAAb" />
When I set EnableViewState to false:
<asp:PlaceHolder ID="PlaceHolderStatMain" runat="server" EnableViewState="false">
</asp:PlaceHolder>
The viewstate content was decreased significantly:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTg1NDkyNTUzZGTTn8Y28VwmpE/K7yPPkLFvhrqMdU8THijFW/BMFzk0tQ==" />
Question: how to remove 'useless' viewstate content without disabling viewstate for placeholder himself (I would like other control loaded into placeholder to has viewstate)?
Is it possible at all?
Any thought are welcome!
P.S. I am using ASP.NET 4.0

In ASP.Net 4.0 you can disable the viewstate for the page with the ViewStateMode parameter, and enable it for the controls which need it. Label1 will have viewstate and Label2 will not since it inherits the Disabled state from the page.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
ViewStateMode="Disabled" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>View State Demo in ASP.NET 4.0</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="One" ViewStateMode="Enabled"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Two"></asp:Label> <br /><br />
<asp:Button ID="Button1" runat="server" Text="PostBack" />
</div>
</form>
</body>
</html>

Related

Asp.net Image Button does not have an onClick event

This code is inside a GridView,
<asp:TemplateField HeaderText="Edit" ItemStyle-VerticalAlign="Middle" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Image ID="imgEdit" ImageUrl="~/edit.png" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
My problem is that if I try to create an onClick event inside the <asp:Image attribute it is not available. What might be the issue?
<asp:Image /> is used to display images only, hence you're not able to use OnClick Event. The control you're looking for is <asp:ImageButton /> which will allow you to use the OnClick Event.
<asp:ImageButton runat="server" OnClick="MyClickFunction" />
You can use an ImageButton like that :
<%# Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>ImageButton Sample</title>
<script language="C#" runat="server">
void ImageButton_Click(object sender, ImageClickEventArgs e)
{
Label1.Text = "You clicked the ImageButton control at the coordinates: (" +
e.X.ToString() + ", " + e.Y.ToString() + ")";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>ImageButton Sample</h3>
Click anywhere on the image.<br /><br />
<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="ImageButton 1"
ImageAlign="left"
ImageUrl="images/pict.jpg"
OnClick="ImageButton_Click"/>
<br /><br />
<asp:label id="Label1" runat="server"/>
</form>
</body>
</html>
If is on Client side you can simply use a ..
more at : https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.imagebutton.onclick(v=vs.110).aspx

Inline Server Tags Re-Eavluated on Partial Postback from UpdatePanel?

I have a web application where I'm trying to use a user control with an UpdatePanel so that the control can be updated on the web page using a partial postback.
I have defined the ScriptManager in the Master page for the site. It is place outside of the ContentPlaceHolder but inside the form tag for the body as represented below:
<head>
<asp:ContentPlaceHolder ID="HeadContent" runat="server" />
</head>
<body>
<form runat="server" class="form-horizontal">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</body>
In the content page I have some HTML that using inline server tags to bind to data and a user control that contains the UpdatePanel as represented below:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ClientDetail.aspx.cs" Inherits="ClientDetail" Async="true" %>
<%# Register src="../controls/ListSelectionControl.ascx" tagname="ListSelectionControl" tagprefix="lsc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
<link href="<%= ResolveClientUrl("~/css/clientdetail.css") %>" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<div>
Client: <%= this.CurrentClient.Id %> - <%= this.CurrentClient.Name %>
</div>
<div>
<lsc:ListSelectionControl ID="ItemsListSelectionControl" runat="server" />
</div>
</asp:Content>
The UpdatePanel in the user control includes a couple of listboxes and buttons to move items from one list to another. The buttons are registered as AsyncPostbackTriggers and are marked as CauseValidation="false" to prevent the validators in other parts of the page from firing. The UpdatePanel is represented below:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="ListSelectionControl.ascx.cs" Inherits="ListSelectionControl" %>
<div>
<asp:UpdatePanel ID="udpListSelection" UpdateMode="Conditional" runat="server" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnRemove" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnAddAll" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="btnRemoveAll" EventName="Click" />
</Triggers>
<ContentTemplate>
<div>
<asp:ListBox ID="lstAvailableItems" runat="server" SelectionMode="Multiple" Rows="15" />
</div>
<div>
<asp:Button ID="btnAdd" CausesValidation="false" runat="server" OnClick="AddItems" Text="Add >" />
<asp:Button ID="btnRemove" CausesValidation="false" runat="server" OnClick="RemoveItems" Text="< Remove" />
<asp:Button ID="btnAddAll" CausesValidation="false" runat="server" OnClick="AddAllItems" Text="Add All >" />
<asp:Button ID="btnRemoveAll" CausesValidation="false" runat="server" OnClick="RemoveAllItems" Text="< Remove All" />
</div>
<div>
<asp:ListBox ID="lstSelectedItems" runat="server" SelectionMode="Multiple" Rows="15" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
The code behind on the buttons simply manages the exchange of the data between the list boxes and I don't believe it's relevant here.
The problem that I'm having is that when the partial postback occurs (breakpointing the code in the Master page and analysing the IsInAsyncPostback property determines that it is a partial postback) the page is processing the inline ASP server code and failing because the CurrentClient object is not being re-populated. I'm looking to understand why this is happening and why the partial postback is not simply updating my UpdatePanel as I expected?
Thank you for your assistance.
Richard
I regretfully have to inform you of some bad news... update panels have a lot of issues inside of UserControls... hopefully you can find some sort of work around.

FilteredTextBoxExtender in asp.net

iam using ajaxtoolkit in asp.net 3.5, using filter texbox extender control
<%# Page Language="C#" %>
<%# Register TagPrefix="ajaxToolkit" Namespace="AjaxControlToolkit"
Assembly="AjaxControlToolkit" %>
<!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>Show Filtered TextBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Label
id="lblNumeric"
Text="Enter a Number:"
AssociatedControlID="txtNumeric"
Runat="server" />
<br />
<asp:TextBox
id="txtNumeric"
Runat="server" />
<ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="txtNumeric" FilterType="Numbers">
</ajaxToolkit:FilteredTextBoxExtender>
<br /><br />
<asp:Label
id="lblProductCode"
Text="Enter a Product Code:"
AssociatedControlID="txtProductCode"
Runat="server" />
<br />
<asp:TextBox
id="txtProductCode"
Runat="server" />
<br />
(A product code can contain only lower-case characters,
![enter image description here][1] underscores, exclamation marks, and no spaces)
</div>
</form>
</body>
</html>
iam using extender control..witfiltertype as numbers..but in doesnt works at runtime,, it accepts all characters..
any help would be appreciated
You are trying to make your code more complex. Actually the FilterTypeExtender is very easy to use. Follow this way. [Note: Close the appropriate controls properly.]
<asp:TextBox ID="txtExperienceYears" runat="server" Width="223px" MaxLength="2"></asp:TextBox>
<%-- Now add the FilterTypeExtender below --%>
<asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="txtExperienceYears" FilterType="Numbers"></asp:FilteredTextBoxExtender>

AjaxControlToolkit problem in asp.net

I am trying to set up ajaxConfirmButton Extender. Here is my code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<script language="javascript" type="text/javascript">
function onCancel()
{
var lblMsg = $get('<%=lblMessage.ClientID%>');
lblMsg.innerHTML = "You clicked the <b>Cancel</b> button of AJAX confirm.";
}
</script>
<asp:Label ID="Label1" runat="server" Text="Click this button to open AJAX Confirm box:" Font-Bold="true" Font-Size="16px"></asp:Label><br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnConfirm" runat="server" Text="Confirm" OnClick="btnConfirm_Click" />
<ajaxToolkit:ConfirmButtonExtender ID="ConfirmButtonExtender1" runat="server" TargetControlID="btnConfirm"
ConfirmText="Are you sure?
You want to run the server code." OnClientCancel="onCancel" ConfirmOnFormSubmit="false">
</ajaxToolkit:ConfirmButtonExtender>
<asp:Label ID="lblMessage" runat="server"></asp:Label><br />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
code behind
protected void btnConfirm_Click(object sender, EventArgs e)
{
lblMessage.Text = "You clicked the <b>OK</b> button of AJAX confirm";
}
But the confirm wondow is not coming. It is just performing the onclick action. Need help to fix this problem
You need to use ToolkitScriptManager instead of ScriptManager.
<ajaxToolkit:ToolkitScriptManager ID="ToolKitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
Hope it will help you

Element 'ToolkitScriptManager' is not a known element

So I have a file called WebParts.aspx which looks like this -
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebParts.aspx.cs" Inherits="e.WebParts" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:TabContainer ID="TabContainer1" runat="server">
<asp:TabPanel ID="TabPanel1" runat="server">
<ContentTemplate>Page One</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel2" runat="server">
<ContentTemplate>Page Two</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel3" runat="server">
<ContentTemplate>Page Three</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</div>
</form>
</body>
</html>
And that produces the desired results of creating 3 tab panels inside a tab container.
However, when I change that page to use a MasterPage.master to look like this -
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebParts.aspx.cs" Inherits="eservice.WebParts" MasterPageFile="~/MasterPage.Master"%>
<asp:Content ID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<p id="backtoblog"></p>
<p> Preferences</p>
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:TabContainer ID="TabContainer1" runat="server">
<asp:TabPanel ID="TabPanel1" runat="server">
<ContentTemplate>Page One</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel2" runat="server">
<ContentTemplate>Page Two</ContentTemplate>
</asp:TabPanel>
<asp:TabPanel ID="TabPanel3" runat="server">
<ContentTemplate>Page Three</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</div>
</div>
</LoggedInTemplate>
<AnonymousTemplate>
You are not logged in.
<br />
Please login to access eservice
</AnonymousTemplate>
</asp:LoginView>
</asp:Content>
VS2008 gives me the following warning:
Element 'ToolkitScriptManager' is not
a known element. This can occur if
there is a compilation error in the
Web site, or the web.config file is
missing.
on the following line:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
Your second file does not contain the line
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
which you have in your first file. Just because the master page knows about the asp: prefix and the assembly/namespace you've associated to it, doesn't mean that the child page does.
A better approach would be to register the assembly/namespace/tag prefix inside your web.config, like so:
<configuration>
<!-- ... -->
<system.web>
<!-- ... -->
<pages>
<controls>
<add tagPrefix="asp"
namespace="AjaxControlToolkit"
assembly="AjaxControlToolkit" />
</controls>
</pages>
</system.web>
</configuration>
Element 'ToolkitScriptManager' is not a known element. This can occur
if there is a compilation error in the Web site, or the web.config
file is missing.
Just in case someone runs across this. The fix for me, was that the imported project properties pointed to 4.5.2 framework. I selected an older framework, and then selected 4.5.2 again. This got rid of the mentioned error along with dozens of others.

Resources