C# 鼠標離開后觸發動作
根據當前輸入內容去DB中查詢,如已存在此資料作出相應提示
.aspx page
<script>
function GetEntInfo() {
var area = document.getElementById("<%=ddlarea.ClientID%>").value;
if (area == "")
{
alert("請選擇地區!");
return false;
}
var ipno = document.getElementById("<%=txtipaddress.ClientID%>").value;
if (ipno == "")
{
alert("請輸入IP地址!");
return false;
}
var tye = getType();
if (tye == "2") {
alert("已存在此IP地址!");
return false;
}
}
function getType() {
var area = document.getElementById("<%=ddlarea.ClientID%>").value;
var ipno=document.getElementById("<%=txtipaddress.ClientID%>").value;
x = window.XMLHttpRequest ? new window.XMLHttpRequest : new ActiveXObject("Msxml2.XMLHTTP")
x.open("GET", "<%= Request.Url.LocalPath %>?area=" + area+"&ipno="+ipno, false);
x.send(null);
if (x.status == 200) {
return x.responseText;
}
else {
}
}
</script>
IP地址:<asp:TextBox ID="txtipaddress" runat="server" Height="21px" onmouseout="GetEntInfo()"></asp:TextBox>
.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["area"] != null && Request.QueryString["ipno"] != null)
{
Response.ClearContent();
Response.Write(GetSType());
Response.End();
}
}
protected string GetSType()
{
#region 判斷IP地址是否已經存在
string area = Request.QueryString["area"].ToString();
string ipno = Request.QueryString["ipno"].ToString();
string sqlip = "SQL查詢語句";
DataTable ds = DBHelper.GetDataSet(sqlip, connectstring);
if (ds.Rows.Count > 0)
{
return "2";
}
else
{
return "1";
}
#endregion
}
本文介绍了如何在C#中实现ASP.NET网页上,当鼠标离开IP地址输入框时,自动触发查询数据库的动作,判断输入的IP地址是否已存在于数据库中。通过JavaScript函数`GetEntInfo()`和后台方法`Page_Load()`、`GetSType()`配合,实现了这一功能。
900

被折叠的 条评论
为什么被折叠?



