2010年9月17日 星期五

判斷中文字

'判斷字串中的中文字數和中文字
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ex As New Regex("[一-龥]")
Dim isMatched As Boolean = ex.IsMatch(TextBox4.Text)
Dim m As MatchCollection = ex.Matches(TextBox4.Text)
Dim chi As String = ""
Dim chiCount As Int16 = 0
For i As Int16 = 0 To m.Count - 1
chi &= m.Item(i).Value
chiCount = chiCount + 1
Next
MessageBox.Show("中文字數:" & chiCount & ",中文字:" & chi)
End Sub

'顯示字元碼
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MessageBox.Show(AscW(TextBox1.Text))
End Sub

2010年5月29日 星期六

javascript取checkboxlist值

=====網頁======
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Javascript_Checkbox.aspx.cs" Inherits="Javascript_Checkbox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Javascript Selectted Item From CheckBoxList</title>

<script type="text/javascript">
function Get_Selected_Value()
{
var ControlRef = document.getElementById('<%= CheckBoxList1.ClientID %>');
var CheckBoxListArray = ControlRef.getElementsByTagName('input');
var spanArray=ControlRef.getElementsByTagName('span');
var checkedValues = '';
var nIndex=0;
var sValue='';

for (var i=0; i<CheckBoxListArray.length; i++)
{
var checkBoxRef = CheckBoxListArray[i];

if ( checkBoxRef.checked == true )
{
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');


if ( labelArray.length > 0 )
{
if ( checkedValues.length > 0 )
{
checkedValues += ', ';
nIndex += ', ';
sValue += ', ';
}
checkedValues += labelArray[0].innerHTML;
nIndex +=i;
sValue +=spanArray[i].alt;
}
}
}
document.getElementById('<%= lbl_SelectedValue.ClientID %>').innerHTML='<b>Selected Value:</b> '+ sValue;
document.getElementById('<%= lbl_SelectedText.ClientID %>').innerHTML='<b>Selected Text:</b> '+checkedValues;
document.getElementById('<%= lbl_SelectedIndex.ClientID %>').innerHTML='<b>Selected Index:</b> '+nIndex;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" onclick="Get_Selected_Value();" OnDataBound="CheckBoxList1_DataBound">
</asp:CheckBoxList>
<hr />
<label id="lbl_SelectedValue" runat="server"></label><br />
<label id="lbl_SelectedText" runat="server"></label><br />
<label id="lbl_SelectedIndex" runat="server"></label>
</div>
</form>
</body>
</html>

=====程式碼======
using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Javascript_Checkbox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connectionString);
using (conn)
{
SqlDataAdapter ad = new SqlDataAdapter(
"SELECT ID,Title from Article", conn);
ad.Fill(dt);
}
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataTextField = "Title";
CheckBoxList1.DataValueField = "ID";
CheckBoxList1.DataBind();
}

protected void CheckBoxList1_DataBound(object sender, EventArgs e)
{
CheckBoxList chkList = (CheckBoxList)(sender);
foreach (ListItem item in chkList.Items)
item.Attributes.Add("alt", item.Value);
}
}

2010年4月20日 星期二

由ItemTemplate中的Button取得GridView的SelectedRow

方法一:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName ="111" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" CommandName ="222" />
</ItemTemplate>
</asp:TemplateField>

Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

Dim index As Integer = DirectCast(DirectCast(e.CommandSource, Button).NamingContainer, GridViewRow).RowIndex

If e.CommandName = "111" Then
Response.Write(gv.Rows(index).Cells(1).Text)
End If

If e.CommandName = "222" Then
Response.Redirect("1.aspx")
End If

End Sub

方法二:
以下的Eval("ID")為GridView中的DataKeyNames

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName ="111" CommandArgument ='<%# Eval("ID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Button" CommandName ="222" CommandArgument ='<%# Eval("ID") %>'/>
</ItemTemplate>
</asp:TemplateField>


Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

If e.CommandName = "111" Then
Response.Write(e.CommandArgument)
Response.Write(gv.Rows(e.CommandArgument).Cells(1).Text)
End If

If e.CommandName = "222" Then
Response.Redirect("1.aspx")
End If

End Sub

2009年12月24日 星期四

比對Excel某兩欄欄位值


格式-->設定格式化條件-->如下圖。
由格式設定所要顯示的格式。

2009年5月21日 星期四

同一個 IIS 處理序中不能執行兩個不同版本的 ASP.NET

在Windows2003中,若有Web應用程式要在不同版本的.NET FrameWork(1.1、2.0)執行,則需設定不同的應用程式集區,否則該Web應用程式無法執行!事件檢視器會出現『同一個 IIS 處理序中不能執行兩個不同版本的 ASP.NET。請使用 IIS 系統管理工具重新設定伺服器使用不同的處理序來執行應用程式』。

2009年4月23日 星期四

居然不能貼Code

要貼上一篇文章的Code,一直貼不上去,Google一下才發現,原來這個Blog不能貼Code,但可由這個網站centricle,將字元『<』轉換成『&lt』就可以啦!

執行Ajax網頁出現'Sys'未被定義

在<System.Web>加入以下程式碼:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>

<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>