Ajax实现DataGrid/DataList动态ToolTip
分类:
文章
•
2025-07-03 18:03:34

1.建立一aspx页面,html代码2.cs代码
usingSystem.Data.SqlClient;
usingSystem.IO;
protectedvoidPage_Load(objectsender,EventArgse)


{
if(!Page.IsPostBack)


{
BindData();

}
if(ID!="")


{
GetDescriptionByID(ID);
}

}


property#regionproperty
privatestringID


{
get


{
if(Request["ID"]!=null&&Request["ID"].ToString()!="")


{
returnRequest["ID"];
}
else


{
return"";
}
}
}
#endregion


GetDescriptionByID#regionGetDescriptionByID
privatevoidGetDescriptionByID(stringID)


{
stringconnStr=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnectionconn=newSqlConnection(connStr);
stringsql="select*fromtestimagewhereuserid='"+ID+"'";
SqlCommandcmd=newSqlCommand(sql,conn);
conn.Open();
SqlDataReaderdr=cmd.ExecuteReader();

strings=@"<tablecellspacing='0'cellpadding='4'width='300'height='200'border='0'id='GridView1'style='color:#333333;border-collapse:collapse;'>";
if(dr.Read())


{
s+="<trstyle='color:#333333;background-color:#FFFBD6;'>";
s+="<tdwidth='50'>名称:</td>";
s+="<td>"+dr["UserName"]+"</td>";
s+="</tr>";
s+="<trstyle='color:#333333;background-color:White;'>";
s+="<tdscope='col'>描述:</td>";
s+="<td>"+dr["Description"]+"</td>";
s+="</tr>";
}
s+="</table>";
dr.Close();
conn.Close();
this.Response.Write(s);
this.Response.End();
}
#endregion


saveimage#regionsaveimage
protectedvoidButton2_Click(objectsender,EventArgse)


{
StreamImageStream;
stringPath=FileUpload1.PostedFile.FileName;//文件名称
intSize=FileUpload1.PostedFile.ContentLength;//文件大小
stringType=FileUpload1.PostedFile.ContentType;//文件类型
ImageStream=FileUpload1.PostedFile.InputStream;
byte[]Content=newbyte[Size];
intStatus=ImageStream.Read(Content,0,Size);

SqlConnectionconn=newSqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommandcomm=newSqlCommand("insertintotestimage(UserName,Image,Path,Type,Description)values(@UserName,@Image,@Path,@Type,@Description)",conn);

comm.CommandType=CommandType.Text;
comm.Parameters.Add("@UserName",SqlDbType.VarChar,255).Value=txtUserName.Text;
comm.Parameters.Add("@Image",SqlDbType.Image).Value=Content;
comm.Parameters.Add("@Path",SqlDbType.VarChar,255).Value=Path;
comm.Parameters.Add("@Type",SqlDbType.VarChar,255).Value=Type;
comm.Parameters.Add("@Description",SqlDbType.VarChar,2000).Value=this.TextBox1.Text;

conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
#endregion


BindData#regionBindData
privatevoidBindData()


{
stringsql="select*fromtestimage";
DataSetds=GetDataSet(sql);
this.DataList1.DataSource=ds;
this.DataList1.DataBind();
}
#endregion


GetDataSet#regionGetDataSet
privateDataSetGetDataSet(stringsql)


{
stringconstring=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdaptersda=newSqlDataAdapter(sql,constring);
DataSetds=newDataSet();
sda.Fill(ds);
returnds;
}
#endregion
3.数据库脚本
ifexists(select*fromdbo.sysobjectswhereid=object_id(N'[dbo].[TestImage]')andOBJECTPROPERTY(id,N'IsUserTable')=1)
droptable[dbo].[TestImage]
GO

CREATETABLE[dbo].[TestImage](
[UserID][int]IDENTITY(1,1)NOTNULL,
[UserName][nvarchar](500)COLLATEChinese_PRC_CI_ASNULL,
[Image][image]NULL,
[Path][nvarchar](500)COLLATEChinese_PRC_CI_ASNULL,
[Type][nvarchar](20)COLLATESQL_Latin1_General_CP1_CI_ASNULL,
[Description][nvarchar](2000)COLLATEChinese_PRC_CI_ASNULL
)ON[PRIMARY]TEXTIMAGE_ON[PRIMARY]
GO
<html>
<head>
<title>WebForm1</title>

<styletype="text/css">
.logo{
}{POSITION:absolute}.dek{
}{Z-INDEX:200;VISIBILITY:hidden;POSITION:absolute}</style>
</head>
<body>
<Formrunat="server">
<DIVclass="dek"id="dek"></DIV>

<scriptlanguage="javascript">
Xoffset=-20;
Yoffset=20;
varnav,yyy=-1000;
varskn=dek.style;
document.onmousemove=get_mouse;

//ajax
varxmlHttp;
functioncreateXMLHttpRequest()


{
if(window.ActiveXObject)


{
xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
}
elseif(window.XMLHttpRequest)


{
xmlHttp=newXMLHttpRequest();
}
}

functionstartRequest(id)


{
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","?ID="+id,true);
xmlHttp.send(null);
}
varcontent;
functionhandleStateChange()


{
if(xmlHttp.readyState==4)


{
if(xmlHttp.status==200)


{
content