FilteredTextBoxExtender in asp.net - 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>

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

One form tag on page in ASP.NET Issue

I am getting this error when trying to run my project on localhost
Exception Details: System.Web.HttpException: A page can have only one server-side Form tag
This is my ASP.NET form
<form id="consultationform" runat="server">
<%-- Consultation --%>
<%-- Name --%>
<asp:Label ID="namelabel" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="namebox" runat="server"></asp:TextBox><br />
<%-- email --%>
<asp:Label ID="emaillabel" runat="server" Text="Email"></asp:Label>
<asp:TextBox ID="emailbox" runat="server"></asp:TextBox><br />
<%-- restaurant --%>
<asp:Label ID="restaurantlabel" runat="server" Text="Restaurant"></asp:Label>
<asp:TextBox ID="restaurantbox" runat="server"></asp:TextBox><br />
<%-- Address --%>
<asp:Label ID="addresslabel" runat="server" Text="Address"></asp:Label>
<asp:TextBox ID="addressbox1" runat="server"></asp:TextBox><br />
<asp:TextBox ID="addressbox2" runat="server"></asp:TextBox><br />
<asp:TextBox ID="addressbox3" runat="server"></asp:TextBox><br />
<%-- County --%>
<asp:Label ID="countylabel" runat="server" Text="County"></asp:Label>
<asp:DropDownList ID="countrydropdown" runat="server"
DataSourceID="CountySqlDataSource" DataTextField="CountyName"
DataValueField="CountyName" AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Value="" Text="Select a County"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="CountySqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [CountyName] FROM [Counties]"></asp:SqlDataSource>
<br />
<%-- Telephone --%>
<asp:Label ID="telephonelabel" runat="server" Text="Telephone"></asp:Label>
<asp:TextBox ID="telephonebox" runat="server"></asp:TextBox><br />
<%-- Calendar --%>
<asp:Label ID="datelabel" runat="server" Text="Date (What suits you?)"></asp:Label><asp:Calendar
ID="Calendar1" runat="server"></asp:Calendar><br />
<%-- Additional Info --%>
<asp:Label ID="infolabel" runat="server" Text="Additional Information (That may help us further)"></asp:Label><br />
<asp:TextBox ID="infobox" runat="server"></asp:TextBox><br />
<%-- Menu Upload --%>
<asp:Label ID="menulabel" runat="server" Text="Menu"></asp:Label><asp:FileUpload
ID="FileUpload1" runat="server" /><br />
<%-- Book Button --%>
<asp:Button ID="bookbtn" runat="server" Text="Book Now" />
</form>
I don't understand why it is happening. I only have one form tag on the page. Any help on the matter is much appreciated.
Should note, page renders completely fine if I remove the form tag.
If you are using a masterpage, remove the form from your webform and inside the masterpage form add a content placeholder.
Use that content placeholder within your webforms like you were trying to use the form.
You may need to click the little arrow to the right of the content placeholder in the form in order to enable it.
in your master page, you should have at least:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs"Inherits="WebApplication1.Site1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
you can then remove all other form tags from your project, beacuse the master has one in use.
remember to add content pages properly also, in the body, contentplaceholder.

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.

ASP.NET trying to compile commented lines of code. Why?

I have code like this on some aspx page in my Web Application:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<asp:Label ID="lbl1" runat="server" Text="Laber1" />
<asp:Button ID="btn1" runat="server" Text="Button1" onclick="btn1_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
<!--
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:TextBox ID="txtAjaxToolkit" runat="server" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtAjaxToolkit" />
-->
</asp:Content>
When I compile the project and go to this page in the browser, I get this exception:
[InvalidOperationException: На страницу можно добавить только один экземпляр ScriptManager.]
(translate: "There only one instance of ScriptManager allowed on this page").
Why do I get this message if second script manager (ToolkitScriptManager) is located in comments?
I am using ASP.NET 4 and ASP.NET Ajax Control Toolkit.
P.S: Sorry for my english.
Use server side comment syntax.
<%---
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:TextBox ID="txtAjaxToolkit" runat="server" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtAjaxToolkit" />
---%>
You've used HTML comments, but the markup engine doesn't care about that - it'll still try to generate the content of that HTML comment. After all, you might want something like a timestamp in a comment.
You want a server-side comment, like this;
<%--
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/>
<asp:TextBox ID="txtAjaxToolkit" runat="server" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server"
TargetControlID="txtAjaxToolkit" />
--%>
It's all a matter of working out what's going to figure out that something is a comment - the HTML <!-- comment --> is aimed at a browser; the `<%-- comment --%> is aimed at the ASPX processor.

ASP.NET - ViewState: empty placeholder generates view state

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>

Resources