[習題]簡單的搜尋引擎 + CheckBoxList(條件可複選)
http://www.dotblogs.com.tw/mis2000lab/archive/2008/11/18/searcheengine_checkboxlist_081118.aspx
修改後的 C# 程式如下:
01
protected void Button1_Click(object sender, EventArgs e)
02
{
03
string Search_String = "";
04
Boolean u_select = false;
05
int word_length = 0;
06![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
07
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
08
{
09
if (CheckBoxList1.Items[i].Selected)
10
{
11
Search_String = Search_String + " [class] LIKE '%" + CheckBoxList1.Items[i].Text + "%' or ";
12
u_select = true; //使用者有點選任何一個CheckBoxList子選項
13
}
14
}
15![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
16
if (u_select)
17
{
18
word_length = Search_String.Length; //計算 Search_String的字串長度,VB語法為Len(Search_String)
19
Search_String = Left(Search_String, (word_length - 3));
20
//因為C#語法沒有 Left()函數,所以要自己寫!請看最下方。
21
//刪去最後三個字 「or 」
22
23
Label1.Text = Search_String;
24
}
25
else
26
{
27
Label1.Text = "您尚未點選任何一個CheckBoxList子選項";
28
//Response.End(); //建議改成 return跳離。
29
return;
30
}
31![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
32
//=======================================
33
//== SqlDataSource1 資料庫的連接字串 ConnectionString,
34
//== 已事先寫在「HTML畫面的設定」裡面 ==
35
//=======================================
36
SqlDataSource1.SelectCommand = "SELECT [test_time], [id], [class], [title] FROM [test] WHERE " + Search_String;
37
//這次不使用 SqlDataSource提供的@參數
38
}
39![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
40![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
41
//因為C#語法沒有 Left()函數,所以要自己寫!
42
public static string Left(string param, int length)
43
{
44
//we start at 0 since we want to get the characters starting from the
45
//left and with the specified lenght and assign it to a variable
46
string result = param.Substring(0, length);
47
//return the result of the operation
48
return result;
49
}
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
C# 補充說明:
1. 如果使用者未點選任何一個子選項,建議以 return跳開程式,並給予警告訊息。會比上述的 Response.End()來得好。
2. 下方的 Left()函數,因為是單一程式使用,可以免去 宣告時的 Static字眼。
3. 因為在HTML畫面,已經設定好GridView的 DataSourceID = "SqlDatasource1",所以不用寫 GridView.DataBind()。
VB版 程式如下:
01
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
02
Dim Search_String As String = ""
03
Dim u_select As Boolean = False
04
Dim i, word_length As Integer
05![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
06
For i = 0 To (CheckBoxList1.Items.Count - 1)
07
If (CheckBoxList1.Items(i).Selected) Then
08
Search_String = Search_String & " [class] LIKE '%" & CheckBoxList1.Items(i).Text & "%' or "
09
u_select = True '--使用者有點選任何一個CheckBoxList子選項
10
End If
11
Next
12![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
13
If (u_select = True) Then
14
word_length = Len(Search_String) '--計算 Search_String的字串長度,VB語法為Len(Search_String)
15
Search_String = Left(Search_String, (word_length - 3))
16
'--刪去最後三個字 「or 」
17![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
18
Label1.Text = Search_String
19
Else
20
Label1.Text = "您尚未點選任何一個CheckBoxList子選項"
21
'--Response.End() '--改成 Exit Sub 跳開這個副程式
22
Exit Sub
23
End If
24![](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_u3ALLC4dv8NA9GJw_6qta32-3rpIawXTcvlmY5SqelVCt3OZEhqMfVBoosn5klFJQp7gcCZU0r9rU0R2Wy-07iqw0YNGsTz6Mw8qnVxCDkbNj9unvPakftHglgFTEZq4YRQ6N9GUDSTQmMZB-8_DTJqibIMTFpEMgG5zNY-AaIekHd8D8BgCxZ7YHsGdrdipkB=s0-d)
25
'=======================================
26
'== SqlDataSource1 資料庫的連接字串 ConnectionString,
27
'== 已事先寫在「HTML畫面的設定」裡面 ==
28
'=======================================
29
SqlDataSource1.SelectCommand = "SELECT [test_time], [id], [class], [title] FROM [test] WHERE " & Search_String
30
'--這次不使用 SqlDataSource提供的@參數
31
End Sub
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
沒有留言:
張貼留言