项目中的一点积累
1 前台的隐藏控件 用于存一些需要用到的变量
<asp:HiddenField ID="getbrd" runat="server" Value="0" />
VB当中是用<input visible>来存储的
2 Is Not IsPostBack 判断页面是否是第一次进入
3 VB。net 中
If cus IsNot Nothing Then
If cus Is Nothing Then
4 Tirm是去掉字符串两边的多于的空格 中间的不会
5 除了textbox以外的控件,
当输入的文本中有htm标签,例如<br>和分号时 提交的时候会倒掉(一般为登录页面 需要存入数据的页面)在页面前台加入
<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="MasterPageForMap.Master"
Codebehind="OtherInfo.aspx.vb" Inherits="CustomerEntitySln.OtherInfo_aspx" Title="その他設置" ValidateRequest="false" %>
这样处理后存入数据不会出错,不然会不识别<br>的字符串。
而在girdview绑定数据的时候会解析成htm标签导致显示错误例如<br>会解析成换行,所以在绑定数据的时候应该作处理
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Configuration
Imports System.Data
Imports System
Imports System.Web
Imports System.Collections.Generic
Public Class HtmlEncodeOpation
Inherits System.Web.UI.Page
' <summary>
' function : HtmlEncodeFunciton
' date/version : 2008/08/18/v0.1
' changer :
' note : HtmlEncodeFunciton
' </summary>
Public Function HtmlEncodeFunciton(ByVal ob As Object)
Dim Properties() As Reflection.PropertyInfo = ob.GetType.GetProperties
For Each pi As Reflection.PropertyInfo In Properties
pi.SetValue(ob, Convert.ChangeType(StringOpation(pi.GetValue(ob, Nothing).ToString()), pi.PropertyType), Nothing)
Next
Return ob
End Function
' <summary>
' function : StringOpation
' date/version : 2008/08/18/v0.1
' changer :
' note : StringOpation
' </summary>
Public Function StringOpation(ByVal strtemp As String) As String
If strtemp.Length <> 0 Then
strtemp = strtemp.Replace("'", "''")
Return strtemp
Else
Return strtemp
End If
End Function
End Class
第一个函数处理实体,第二个函数处理字符串 这样处理后的文本就会正确形式显示
绑定函数
Protected Sub CoSearchList_DataRowBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.Header) Then
For Each cell As TableCell In e.Row.Cells
cell.Attributes.Add("style", "FONT-WEIGHT:normal")
Next
End If
If (e.Row.RowType = DataControlRowType.DataRow) Then
If (e.Row.DataItem Is Nothing) Then
Return
End If
'実体を初期化
Dim CoList As Model.CustomerCoSearchList = CType(e.Row.DataItem, Model.CustomerCoSearchList)
'一覧の要素を初期化
Dim lnkbtnName As LinkButton = CType(e.Row.FindControl("lnkbtnName"), LinkButton)
Dim lblAddress As Label = CType(e.Row.FindControl("lblAddress"), Label)
Dim lblcode As Label = CType(e.Row.FindControl("lblcode"), Label)
Dim lblTel As Label = CType(e.Row.FindControl("lblTel"), Label)
Dim lblclass1 As Label = CType(e.Row.FindControl("lblclass1"), Label)
Dim lblclass2 As Label = CType(e.Row.FindControl("lblclass2"), Label)
Dim lblclass3 As Label = CType(e.Row.FindControl("lblclass3"), Label)
Dim lblEdit As LinkButton = CType(e.Row.FindControl("lblEdit"), LinkButton)
lnkbtnName.Text = Server.HtmlEncode(CoList.Name)
lnkbtnName.PostBackUrl = "./CustomerCoView.aspx?Id=" + CoList.Id
lblAddress.Text = Server.HtmlEncode(CoList.Address)
lblcode.Text = CoList.Code
lblTel.Text = Server.HtmlEncode(CoList.TelNum)
lblclass1.Text = Server.HtmlEncode(CoList.ClassName1)
lblclass2.Text = Server.HtmlEncode(CoList.ClassName2)
lblclass3.Text = Server.HtmlEncode(CoList.ClassName3)
lblEdit.PostBackUrl = "./CustomerCoEdit.aspx?Id=" + CoList.Id
End If
End Sub