• 2703阅读
  • 0回复

不走寻常路 设计ASP.NET应用程序的七大绝招 [复制链接]

上一主题 下一主题
离线toxxj
 
只看楼主 倒序阅读 0楼 发表于: 2005-06-08
3. DataList使用不同风格的模板

  这招也非常实用,你可以制作两个不同的模板或表现形式,分别以.ascx控件的形式保存,运行时根据某个条件动态的选择使用其中的一个模板,另外ScottGu认为ItemDataBound方法也可以定制你显示的表现,比如加亮某个元素或是加一个促销广告图等等。

Dim theme As String
theme = DropDownList1.SelectedValue

DataList1.ItemTemplate = Page.LoadTemplate(theme & ".ascx") ---Cool
DataList1.DataSource = DS
DataList1.DataBind()

  4. 设置服务器端控件的焦点

Private Sub SetFocus(ByVal controlToFocus As Control)
Dim scriptFunction As New StringBuilder
Dim scriptClientId As String

scriptClientId = controlToFocus.ClientID
scriptFunction.Append("<script language='javascript'>")
scriptFunction.Append("document.getElementById('" & scriptClientId & "').focus();")
scriptFunction.Append("</script>")
RegisterStartupScript("focus", scriptFunction.ToString())
End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If (Page.IsPostBack = False) Then
SetFocus(TextBox1)
End If
End Sub

  5. 滚动DataGrid

  这招就更简单了,有时候你的页面只有一个固定的地方,但是需要显示非常多的数据,亦或是也不定,但是只有固定的一个地方给你显示它了。这时你就可以用下面这招,自动出滚动条,而且适用许多控件。很简单将你的控件放在一个DIV中将overflow属性设置成auto

<div style=“height:400px;width:200px;overflow:auto”>
<asp:datagrid id=“MyGrid” runat=“server”/>
</div>

  6. 动态创建控件

  利用PlaceHolder控件,这东西在ASP.NET 2.0 Mutil-View和Master Page中运用的就更加多了。

Sub Page_Load()
Dim i as Integer
For i=0 to 4
Dim myUserControl as Control
myUserControl = Page.LoadControl(“foo.ascx”)
PlaceHolder1.Controls.Add(myUserControl)
PlaceHolder1.Controls.Add(New LiteralControl(“<br>”))
Next i
End Sub

  7. 客户端代码的使用

  1). 可以使用客户端的事件代码,但两者不能同名,服务器端代码的名是你可以控制的。对于非ASP.NET的标准控件的自定义控件必须实现IAttributeAccessor接口或从WebControl派生并且可用expando属性

asp:ImageButton id=“foo”
ImageUrl=“start.jpg”
onMouseOver=“rollover(this);”
onMouseOut=“rollout(this)”
rolloversrc=“myrollover.jpg”
rolloutsrc=“myrollout.jpg”
runat=“server”/>

<input type=Button onClick=“return clientHandler()”
onServerClick=“Button1_Click” … />

  2). 使用可以在Postback之前执行客户端代码,当然也可以取消这次Postback,另外也可以访问客户端该页所有的客户端控件。

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles MyBase.Load
 RegisterOnSubmitStatement("foo", "return confirm('Are you sure you want to submit the order?');")
End Sub

  3). 还有更复杂的我认为不实用,大家可以自己去看,主要是运用RegisterStartupScript和JavaScript的技术

  以上文章介绍了一些ASP.NET中常用而且比较实用的技巧,希望能对大家的实际开发有所裨益!
执著甘于寂寞
快速回复
限100 字节
 
上一个 下一个