Ajax运用之Webserver篇---全国车次时间查询服务
效果图:
前台布局Html:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<div>
<table cellpadding="0" cellspacing="0" style="width:480px">
<tr>
<td style="width:18%;">起始站:</td>
<td>
<asp:TextBox ID="txtStart" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
ServiceMethod="GetCompletionList" UseContextKey="True"
TargetControlID="txtStart" MinimumPrefixLength="1">
</cc1:AutoCompleteExtender>
<br />
<span style="font-size:6pt">可以输入拼音首字母</span>
</td>
<td style="width:18%;">终点站:</td>
<td>
<asp:TextBox ID="txtEnd" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender
ID="AutoCompleteExtender2" runat="server" ServiceMethod="GetCompletionList2"
UseContextKey="True" TargetControlID="txtEnd" MinimumPrefixLength="1">
</cc1:AutoCompleteExtender>
<br />
<span style="font-size:6pt">可以输入拼音首字母</span>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Button runat="server" ID="btnSearch" Text="查询" onclick="btnSearch_Click" />
</td>
</tr>
<tr>
<td colspan="4">
<asp:Panel ID="pnlTrain" runat="server" Visible="false">
<asp:GridView ID="gdvTrain" runat="server" AutoGenerateColumns="False" EmptyDataText="没有直达车次">
<Columns>
<asp:BoundField DataField="TrainCode" HeaderText="车次" />
<asp:BoundField DataField="FirstStation" HeaderText="始发站" />
<asp:BoundField DataField="LastStation" HeaderText="终点站" />
<asp:BoundField DataField="StartTime" HeaderText="发车时间" />
<asp:BoundField DataField="ArriveTime" HeaderText="到站时间" />
<asp:BoundField DataField="UseDate" HeaderText="运行时间" />
<asp:BoundField DataField="KM" HeaderText="总里程" />
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
</div>
</form>
</body>
cs代码:
public static string[] stations;
public static void GetStations()
{
if (stations != null)
return;
TrainTimeWebService tts = new TrainTimeWebService();
DataSet ds = tts.getStationNameDataSet();
stations = new string[ds.Tables[0].Rows.Count];
int i = 0;
foreach (DataRow row in ds.Tables[0].Rows)
{
string str = string.Format("{0}_{1}",row["station_shortcode"].ToString(),row["station_name"].ToString());
stations[i]=str;
i++;
continue;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetStations();
}
}
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
Regex regEx = new Regex(string.Format("{0}[\\w]*[_][\\w]*",prefixText),RegexOptions.IgnoreCase| RegexOptions.Multiline| RegexOptions.IgnorePatternWhitespace);
Regex regEx1 = new Regex(string.Format("[\\w]*[_]{0}[\\w]*", prefixText), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
var stas = from s in stations where (regEx.IsMatch(s) || regEx1.IsMatch(s)) select s;
return stas.ToArray<string>();
}
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList2(string prefixText, int count, string contextKey)
{
Regex regEx = new Regex(string.Format("{0}[\\w]*[_][\\w]*", prefixText), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
Regex regEx1 = new Regex(string.Format("[\\w]*[_]{0}[\\w]*", prefixText), RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
var stas = from s in stations where regEx.IsMatch(s) || regEx1.IsMatch(s) select s;
return stas.ToArray<string>();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
//获取起始站的文本
string start = this.txtStart.Text.Substring(this.txtStart.Text.LastIndexOf("_")+1);
//获取终点站的文本
string end = this.txtEnd.Text.Substring(this.txtEnd.Text.LastIndexOf("_")+1);
//实例化Web服务对象
TrainTimeWebService tts = new TrainTimeWebService();
//调用Web服务方法获取车次信息
DataSet ds = tts.getStationAndTimeByStationName(start,end,"");
this.pnlTrain.Visible = true;
this.gdvTrain.DataSource = ds;//显示车次数据
this.gdvTrain.DataBind();
}
广积粮,筑高墙,缓称王