后台代码部分。
WebForm5.aspx.vb
**********************************************************************************
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControlsPublic Class WebForm5
Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents PersonImage As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Image1 As System.Web.UI.WebControls.Image '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim imgID As Int16 = Request.QueryString("ImgID") Dim myConnection As New SqlConnection("Data Source=localhost;Initial Catalog=test;User Id=sa;Password=;")
Dim myCommand As New SqlCommand If imgID > 0 Then
myCommand.CommandText = "Select * from Person where personId =" & imgID
Else
myCommand.CommandText = "Select * from Person"
End If
myCommand.Connection = myConnection
Dim myDataReader As SqlDataReader
Try
myConnection.Open()
If imgID > 0 Then
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
If myDataReader.Read() Then
' Do While (myDataReader.Read())
Response.ContentType = "image/pjpeg"
'Response.ContentType = myDataReader.Item("PersonImageType")
'Image1.DataBind = myDataReader.Item("prosnimage")
Response.BinaryWrite(myDataReader.Item("PersonImage"))
' Loop
End If
Else
DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
DataGrid1.DataBind()
End If
myConnection.Close() Catch SQLexc As SqlException
Response.Write("Read Failed : " & SQLexc.ToString())
End Try
'Dim DB As Container()
End Sub
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then ' e.Item.Attributes.Add("onclick", "this.style.backgroundColor='#FFFFFF';alert('您点击的是:\n\n第" _
'+ e.Item.Cells(0).Controls(0).ToString() + "行\n\n文章标题是:" + e.Item.Cells(0).Text.Replace(",", "\'") + "')")
Dim Hpl As HyperLink = CType(e.Item.Cells(0).Controls(0), HyperLink)
e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(Hpl, "DataNavigateUrlFormatString") 'Dim button1 As LinkButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
'e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(button1, "") ' If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
' e.Item.Attributes.Add("onclick", "this.style.backgroundColor='#FFFFFF';alert('您点击的是:\n\n第" _
'+ e.Item.ItemIndex.ToString() + "行\n\n文章标题是:" + e.Item.Cells(0).Text.Replace(",", "\'") + "')")
''e.Item.Cells(1).Text = e.Item.Cells(1).Text.ToString()
'If e.Item.Cells(0).Text.Length > 30 Then
' e.Item.Attributes.Add("Title", e.Item.Cells(0).Text)
' e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 28) + "…"
'End If
End If
End SubPublic Sub Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim intImageSize As Int64
Dim ImageStream As Stream
Dim strImageType As String ' 获得图片的大小
intImageSize = PersonImage.PostedFile.ContentLength
'获得图片类型()
strImageType = PersonImage.PostedFile.ContentType
Response.Write(strImageType.ToString)
'If strImageType.ToString() <> "image/pjpeg" Then
' Response.Write("<script language=javascript>alert('对不起请选择图片类型文件!')</script>")
' Exit Sub
'End If '读取图片
ImageStream = PersonImage.PostedFile.InputStream
'建立图片长度的字节数组
Dim ImageContent(intImageSize) As Byte
'从图片源读入到ImageContent字节数组中
ImageStream.Read(ImageContent, 0, intImageSize) ' 创建Connection和Command对象
Dim strCnn As String = "Data Source=localhost;Initial Catalog=test;User Id=sa;Password=;"
Dim myConnection As New SqlConnection(strCnn)
'Dim myCommand As New SqlCommand("sp_person_isp", myConnection) Dim strCmd As String = "insert into person (PersonImage) values (@imgPerson) "
Dim myCommand As New SqlCommand(strCmd, myConnection)
myCommand.Parameters.Add("@imgPerson", SqlDbType.Image).Value = ImageContent
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
'myConnection.Close()
Response.Write("<script language=javascript>alert('添加成功!')</script>")
'Response.Write("添加成功!")
Catch SQLexc As SqlException
'Response.Write("<li> 添加失败,Message原因:" & SQLexc.Message())
Response.Write("<li> 添加失败,ToString原因:" & SQLexc.ToString())
Finally
myConnection.Close()
End Try
End Sub
End Class
WebForm5.aspx.vb
**********************************************************************************
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControlsPublic Class WebForm5
Inherits System.Web.UI.Page#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub
Protected WithEvents PersonImage As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Image1 As System.Web.UI.WebControls.Image '注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim imgID As Int16 = Request.QueryString("ImgID") Dim myConnection As New SqlConnection("Data Source=localhost;Initial Catalog=test;User Id=sa;Password=;")
Dim myCommand As New SqlCommand If imgID > 0 Then
myCommand.CommandText = "Select * from Person where personId =" & imgID
Else
myCommand.CommandText = "Select * from Person"
End If
myCommand.Connection = myConnection
Dim myDataReader As SqlDataReader
Try
myConnection.Open()
If imgID > 0 Then
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
If myDataReader.Read() Then
' Do While (myDataReader.Read())
Response.ContentType = "image/pjpeg"
'Response.ContentType = myDataReader.Item("PersonImageType")
'Image1.DataBind = myDataReader.Item("prosnimage")
Response.BinaryWrite(myDataReader.Item("PersonImage"))
' Loop
End If
Else
DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
DataGrid1.DataBind()
End If
myConnection.Close() Catch SQLexc As SqlException
Response.Write("Read Failed : " & SQLexc.ToString())
End Try
'Dim DB As Container()
End Sub
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then ' e.Item.Attributes.Add("onclick", "this.style.backgroundColor='#FFFFFF';alert('您点击的是:\n\n第" _
'+ e.Item.Cells(0).Controls(0).ToString() + "行\n\n文章标题是:" + e.Item.Cells(0).Text.Replace(",", "\'") + "')")
Dim Hpl As HyperLink = CType(e.Item.Cells(0).Controls(0), HyperLink)
e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(Hpl, "DataNavigateUrlFormatString") 'Dim button1 As LinkButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
'e.Item.Attributes("onclick") = Page.GetPostBackClientHyperlink(button1, "") ' If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
' e.Item.Attributes.Add("onclick", "this.style.backgroundColor='#FFFFFF';alert('您点击的是:\n\n第" _
'+ e.Item.ItemIndex.ToString() + "行\n\n文章标题是:" + e.Item.Cells(0).Text.Replace(",", "\'") + "')")
''e.Item.Cells(1).Text = e.Item.Cells(1).Text.ToString()
'If e.Item.Cells(0).Text.Length > 30 Then
' e.Item.Attributes.Add("Title", e.Item.Cells(0).Text)
' e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 28) + "…"
'End If
End If
End SubPublic Sub Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim intImageSize As Int64
Dim ImageStream As Stream
Dim strImageType As String ' 获得图片的大小
intImageSize = PersonImage.PostedFile.ContentLength
'获得图片类型()
strImageType = PersonImage.PostedFile.ContentType
Response.Write(strImageType.ToString)
'If strImageType.ToString() <> "image/pjpeg" Then
' Response.Write("<script language=javascript>alert('对不起请选择图片类型文件!')</script>")
' Exit Sub
'End If '读取图片
ImageStream = PersonImage.PostedFile.InputStream
'建立图片长度的字节数组
Dim ImageContent(intImageSize) As Byte
'从图片源读入到ImageContent字节数组中
ImageStream.Read(ImageContent, 0, intImageSize) ' 创建Connection和Command对象
Dim strCnn As String = "Data Source=localhost;Initial Catalog=test;User Id=sa;Password=;"
Dim myConnection As New SqlConnection(strCnn)
'Dim myCommand As New SqlCommand("sp_person_isp", myConnection) Dim strCmd As String = "insert into person (PersonImage) values (@imgPerson) "
Dim myCommand As New SqlCommand(strCmd, myConnection)
myCommand.Parameters.Add("@imgPerson", SqlDbType.Image).Value = ImageContent
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
'myConnection.Close()
Response.Write("<script language=javascript>alert('添加成功!')</script>")
'Response.Write("添加成功!")
Catch SQLexc As SqlException
'Response.Write("<li> 添加失败,Message原因:" & SQLexc.Message())
Response.Write("<li> 添加失败,ToString原因:" & SQLexc.ToString())
Finally
myConnection.Close()
End Try
End Sub
End Class
解决方案 »
- asp.net 服务器出错时把错误信息写入系统文本,遇到SecurityException
- |zyciis| 现何让Button点击后执行后台代码后,在前台打开一个指定的新窗口 谢谢 急
- 问个小问题
- 怎样取得存储过程的返回值呢?
- GridView的FindControl问题(搞了半天了,急)
- 敬求,从查询结果到内存表的程序。
- 菜鸟提问:调用某个类的方法异常时我想弹出窗口提示怎么做?
- 请问怎样获取浏览器的关闭事件呢?
- 最近总在外出差,一来身体都要吃不消了,二来分也少挣了,不爽,散分
- sql语句可以对DataSet中的表执行查询吗?
- 急求解决办法! 在线急等...数据替换
- 一个DataGrid的问题,关于插入文字的修改
if e.item.itemindex>=0 then
e.item.Attributes("onclick")="this.style='background-image: url(image/a.gif);'"
end if
end sub
写在DATAGRID外的Image如何使用ImageUrl来读取DATAGRID中鼠标点击的记录ID号呢?
//得到你的id号
鼠标点击时e.Item.Attributes["ondblclick"] = "wondow.location.href('本页面?id='+id)";
本页pageload再接收
http://expert.csdn.net/Expert/topic/1944/1944075.xml?temp=.9450952