void ValidateBtn_OnClick(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblOutput.Text = "Page is valid.";
}
else
{
lblOutput.Text = "Page is not valid!";
}
}
void ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
int i = int.Parse(args.Value);
args.IsValid = ((i%2) == 0);
}
catch
{
args.IsValid = false;
}
}
</script>
</head>
<body>
<form runat="server">
<h3>CustomValidator Example</h3>
<asp:Label id=lblOutput runat="server"
Text="Enter an even number:"
Font-Name="Verdana"
Font-Size="10pt" /><br>
<p>
<asp:TextBox id="Text1"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1" ErrorMessage="Not an even number!"
OnServerValidate="ServerValidate" runat="server"/>
<body>
<form runat="server">
<h3>RangeValidator Example</h3>
Enter a number from 1 to 10:
<br>
<asp:TextBox id="TextBox1"
runat="server"/>
<br>
<asp:RangeValidator id="Range1"
ControlToValidate="TextBox1"
MinimumValue="1"
MaximumValue="10"
Type="Integer"
EnableClientScript="false"
Text="The value must be from 1 to 10!"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
<br><br>
<asp:Button id="Button1"
Text="Submit"
OnClick="ButtonClick"
runat="server"/>
</form>
</body>
</html>
public void BindGrid(String sortfield)
{
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT employeeid, firstname, lastname, title, country FROM Employees", conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "MyList");
10.5 AdRotator控件
Web 页上的广告通常采用广告条(小图片)的形式,单击时使用户重定向到广告商的 Web 页。使用 AdRotator Web 服务器控件能够显示广告条并在一系列广告条间循环。AdRotator 自动进行循环处理,在每次刷新页面时更改显示的广告。使用 AdRotator 控件显示广告步骤:
(1) 创建一个Web应用程序框架,项目名为UseAdRotator。
(2) 新建一个XML文件。单击菜单项”项目/添加新项”,弹出标题为添加新项的窗口,在窗口中选中XML文件,文件名为ads.xml,单击打开按钮,增加一个XML文件。文件如下:
<Advertisements>
<Ad>
<ImageUrl>d:\\asp\\bookExample\\p2.JPG</ImageUrl>
<NavigateUrl>http://www.sohu.com</NavigateUrl>
<AlternateText>search anything</AlternateText>
<Impressions>10</Impressions>
<Keyword>Topic1</Keyword>
<Caption>This is the caption for Ad#1</Caption>
</Ad>
<Ad>
<ImageUrl>d:\\asp\\bookExample\\baobao048.jpg</ImageUrl>
<NavigateUrl>http://www.sina.com</NavigateUrl>
<AlternateText>sina main site</AlternateText>
<Impressions>10</Impressions>
<Keyword>Topic2</Keyword>
<Caption>This is the caption for Ad#2</Caption>
</Ad>
</Advertisements>
(3) 在窗体中放置控件AdRotator,其属性Name=AdRotator1。
(4) 修改控件AdRotator的属性AdvertisementFile,单击其后的按钮,出现选择XML文件对话框,在对话框中URL(U)处填入ads.xml。
(5) 运行,可以看到一幅图,鼠标移到图中,变为手形,单击可以转到搜狐网站。刷新,可以看到另一幅图。
XML 文件包含以下预定义的属性。只有 ImageUrl 属性是必需的:
ImageURL 要显示的图像的 URL
NavigateURL 单击 AdRotator 控件时定位到的页面的 URL。
AlternateText 图像不可用时显示的文本(常为图片浏览工具提示)。
Keyword 可用于筛选特定广告的广告类别。通过设置AdRotator的KeywordFilter属性以筛选出 XML 文件中特定类别的广告。
Impression 指示广告的可能显示频率的数值。在 XML 文件中,所有 impression 值的总和不能超过 2,048,000,000 - 1
10.6 Calender控件
Calendar Web 服务器控件在 Web 窗体页上显示一个传统的单月份日历(包含该月在内的6周)。用户可使用该日历查看和选择日期。Calendar Web 服务器控件最简单的用法如下:(C7-2A.aspx)
<html>
<head>
<script language="C#" runat="server">
void Date_Selected(object s, EventArgs e) {
Label1.Text = "Selected date is: " + Calendar1.SelectedDate.ToShortDateString();
}
</script>
</head>
<body>
<h3><font face="Verdana">Calendar Example</font></h3>
<form runat=server>
<asp:Calendar id=Calendar1 onselectionchanged="Date_Selected" runat="server" />
<p>
<asp:Label id=Label1 runat="server" />
</form>
</body>
</html>
其中事件onselectionchanged="Date_Selected"是用户改变选择日期时产生的事件。SelectionMode属性设定Calendar控件中可选择的时间段,Day:可选择任一天;DayWeek:可选择任一天或一周;DayWeekMonth:可选择任一天、一周或一月;None:不能选择日期。C7-2A.aspx网页显示了SelectionMode属性选择不同质的效果。
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e) {
Calendar1.SelectionMode = (CalendarSelectionMode)lstSelMode.SelectedIndex;
if (Calendar1.SelectionMode == CalendarSelectionMode.None)
Calendar1.SelectedDates.Clear();
}
void Date_Selected(object s, EventArgs e) {
switch (Calendar1.SelectedDates.Count) {
case (0): //None
Label1.Text = "No dates are currently selected";
break;
case (1): //Day
Label1.Text = "The selected date is " + Calendar1.SelectedDate.ToShortDateString();
break;
case (7): //Week
Label1.Text = "The selection is a week beginning " + Calendar1.SelectedDate.ToShortDateString();
break;
default: //Month
Label1.Text = "The selection is a month beginning " + Calendar1.SelectedDate.ToShortDateString();
break;
}
}
</script>
</head>
<body>
<h3><font face="Verdana">Date Selection Modes</font></h3>
<p>
<form runat=server>
Choose a Selection Mode:
<asp:DropDownList id="lstSelMode" runat=server
AutoPostBack=true>
<asp:ListItem Value="None" >None</asp:ListItem>
<asp:ListItem Selected Value="Day" >Day</asp:ListItem>
<asp:ListItem Value="DayWeek" >DayWeek</asp:ListItem>
<asp:ListItem Value="DayWeekMonth" >DayWeekMonth</asp:ListItem>
</asp:DropDownList>
2. Asp.NET web窗口的测试
方法与上面的一模一样,添加引用,建立service1的实例
在此不在细说。
3.在VB中测试
这个就要相对来说复杂一些
首先在vb中建立一个”标准EXE”的项目。添加引用:Microsoft Soap Type library。注意:如果没有安装Microsoft Soap Toolkit,是没有这个类型库的。
可以在http://www.ourfly.com中下载。
添加一个text
Private Sub Form_Load()
Text1.Text = add()
End Sub
Public Function Add() As String
Dim objSoapClient As New SoapClient
objSoapClient.ClientProperty("ServerHTTPRequest") = True
Call objSoapClient.mssoapinit("http://localhost/webserver/service1.asmx?WSDL", "Service1", "Service1Soap")
这句也可以
objSoapClient.mssoapinit("http://localhost/webserver/service1.asmx?WSDL")