关于数据邦定的问题,,还有代码优化的问题。。。虚心学习!!!
在HTML控件中,我用数据邦定
<img src="/pic/<%#DataBinder.Eval(Container.DataItem,"pic")% />.gif">
在WEB控件中,我用数据邦定
<asp:Image runat="server" ID="Image1" ImageUrl='/pic/<%#DataBinder.Eval(Container.DataItem,"pic")%>.gif'></asp:Image>
我发现一个是用到了单引号,一个是用双引号,这是为什么呢?差别在哪呢?
还有,我上面的那两个控件都是放在datalist的itemtemplate模版中的,,如果我想在page_load中写
ImageUrl=xxxxxxxxxxxxxxxxx 这个时候应该怎么写呢?
如果我用HTML,可以在page_load中设置<img>的src= 的值吗?或者说如何得到以前在ASP中<input>返回的值呢?
关于代码优化的问题:下面是我写的代码
if e.CommandName="update" then
dim id as string
id=DataList1.DataKeys(e.Item.ItemIndex)
dim box as new textbox
dim boxtext=box.text
box = e.Item.FindControl("TextBox1")
dim myconn as new oledbconnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.mappath(".") & "/db1.mdb")
dim myAdapter as new oledbdataadapter("select * from guestbook",myconn)
dim objcmdbld as new oledbcommandbuilder(myAdapter)
dim ds = new dataset()
dim dv = new dataview()
myAdapter.fill(ds,"guestbook")
datalist1.DataSource=ds.Tables("guestbook").DefaultView
datalist1.DataBind()
dim myobject as string
dv = ds.tables("guestbook").defaultview
dv.sort = "id"
myobject = id
dim rowindex = dv.find(myobject)
if rowindex<>-1 then
ds.Tables("guestbook").Rows(rowindex)("reply")= "reply: "+box.text
myAdapter.Update(ds, "guestbook")
datalist1.edititemIndex=-1
datalist1.databind()
end if
end if
我自己都感觉非常乱,我在处理每一个事件时都打开一次数据库。该怎么做才能将代码写的标准一些呢?听说大公司都会要求程序员按一定的规范写代码,可不可以介绍一下这方面的知识阿?
还有一个问题,,,当做完添加,或更新按钮的功能后,都需要重新邦定数据,这个时候应该怎么重新邦定呢?我的做法就是把整个打开数据库的过程粘贴过来。这么做应该是不对的吧。但是如果直接写一句datalist1.databind() 却不能正常执行。应该怎么做呢?我没办法,用了个response.redirect
sub enter_click(sender As Object,e As EventArgs)
if name.text="" then
label1.text="write your name please"
elseif title.text="" then
label1.text="title is null"
elseif message.text="" then
label1.text="leave your message please"
else
Dim strConn,strSQL As String
Dim myConn As OleDBConnection
Dim myCommand As OleDbCommand
Dim dr As OleDbDataReader
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath(".") & "/db1.mdb"
strSQL="insert into guestbook (author,title,message,pic) values ('"+name.text+"','"+title.text+"','"+message.text+"',1)"
myConn = New OleDbConnection(strConn)
myCommand = New OleDbCommand(strSQL,myConn)
myConn.Open()
myCommand.ExecuteNonQuery()
myconn.close()
response.redirect("admin.aspx")
end if