常常用到卻也常常忘記,例如我要如何驗證他是整數,或是只能輸入英文數字,以下提供好用的語法,下列是做成CLASS的方式,當然你也可以直接CALL最後面的正規表示式放在ASP.NET的驗證控制項規則:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Sky.Framework.Text.RegularExpressions;
namespace Sky.Components
{
public class ValidatorHelper
{
/// <summary>
/// 判斷字串是否與指定正則運算式匹配
/// </summary>
/// <param name="input">要驗證的字串</param>
/// <param name="regularExp">正則運算式</param>
/// <returns>驗證通過返回true</returns>
public static bool IsMatch(string input, string regularExp)
{
return Regex.IsMatch(input, regularExp);
}
/// <summary>
/// 驗證非負整數(正整數 + 0)
/// </summary>
/// <param name="input">要驗證的字串</param>
/// <returns>驗證通過返回true</returns>
public static bool IsUnMinusInt(string input)
{
return Regex.IsMatch(input, RegularExp.UnMinusInteger);
}
/// <summary>
/// 驗證正整數
/// </summary>
/// <param name="input">要驗證的字串</param>
/// <returns>驗證通過返回true</returns>
public static bool IsPlusInt(string input)
{
return Regex.IsMatch(input, RegularExp.PlusInteger);
}
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(8,374)
用ReadOnly是個不錯的用法,問題是如果你要傳給後端去抓取值得時候就抓不到了,
例如日曆用法以下提供一個簡單好用的方式,請加在pageload
textbox1.attributes.add("readonly","true");
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(184)
先在GridView的DataKeyNames屬性中加入你要使用的欄位名稱,例如:DataKeyNames="ID,STUDENT_ID"
接下來在RowDataBound中即可很快的取出你的DataKey的值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Response.Write(GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString());
Response.Write(GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString());
}
}
0代表第一個參數,1代表第二個參數
法蘭克不要怕 發表在 痞客邦 留言(1) 人氣(4,861)
protected void GridViewHeadText1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//先宣告Row來源
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
//接下來取DataKeys的值,0表示第一個,用逗號在GridView中的DataKeys屬性分開
int RID = Convert.ToInt32(GridViewHeadText1.DataKeys[row.RowIndex].Values[0]);
int TID = Convert.ToInt32(GridViewHeadText1.DataKeys[row.RowIndex].Values[1]);
}
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(1,379)
protected void GridViewHeadText1_RowCommand(object sender, GridViewCommandEventArgs e) {
先宣告
GridViewRow row= (GridViewRow)((Control)e.CommandSource).Parent.Parent;
接下來就可以去取控制項
if (((Button)(row.FindControl("Label_ID1"))).Text.Equals("開放"))
{
}
}
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(375)
private string FN(string num)
{
if (num == null) throw new ArgumentNullException("num");
string newstr = string.Empty;
Regex r = new Regex(@"(\d+?)(\d{3})*(\.\d+|$)");
Match m = r.Match(num);
newstr += m.Groups[1].Value;
for (int i = 0; i < m.Groups[2].Captures.Count; i++)
{
newstr += "," + m.Groups[2].Captures[i].Value;
}
newstr += m.Groups[3].Value;
return newstr;
}
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(121)
當RequiredFieldValidator的EnableClientScript屬性被設置成true時,FCKEditor不能支持RequiredFieldValidator,
為了解除這個限制,你必須把這個屬性設置成為false 如果你希望使用用戶端驗證,你必須使用Custom Validator製作一個
非空驗證來替換RequiredFieldValidator,
在其中使用FCKeditor JavaScript API即可。 解決方法: 用CustomValidator代替RequiredFieldValidator
進行驗證首先添加Javascript:
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(83)
在用內容頁的時候常常會要控制到MasterPage頁面中的控制項,以下為取得方式
((Image)(Page.Master.FindControl("ImgTitle")))
範例
((Image)(Page.Master.FindControl("ImgTitle"))).ImageUrl = "/images/topbanner_tit/topbanner_tit03.jpg";
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(1,007)
public static string SubString(string strDD, int startIndex, int length) //解決字串擷取超過範圍的問題
{
int intLen = strDD.Length;
int intSubLen = intLen - startIndex;
string strReturn;
if (length == 0)
strReturn = "";
else
{
if (intLen <= startIndex)
strReturn = "";
else
{
if (length > intSubLen)
length = intSubLen;
strReturn = strDD.Substring(startIndex, length);
}
}
return strReturn;
}
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(249)
字串轉換 double 的時候,用以下方式轉
,直接用Convert.toDouble 去塞值會爆掉,會一直出現字串格式不符合,真是見鬼了,
還好有以下方法可用,可與大家共勉之....微軟我真是搞不懂你啊
M_Point = Point.Text;
double M_Pointspread = 0;
法蘭克不要怕 發表在 痞客邦 留言(0) 人氣(153)