當先在程式碼注冊Java Script後,後面的程式碼就不會執行了。
//ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientScript", "<script laguage='javascript'>alert('修改成功');</script>"); '無論是用
response.write 還是 clientscript, 這裡的code 都是在client side 執行的
url("reppro.aspx"); '但這裡用response.redirect, load 到這裡已redirect 去第二頁, 那怎可看得見前一頁的東東?
alen1985 發表在 痞客邦 留言(0) 人氣(3,752)
class LogWirter
{
/// <summary>
/// 事件源名称
/// </summary>
private string eventSourceName;
EventLogEntryType eventLogType;
public LogWirter()
{
eventSourceName = "測試事件";
eventLogType = EventLogEntryType.Information;
}
alen1985 發表在 痞客邦 留言(0) 人氣(194)
/// <summary>
/// 加密函式
/// </summary>
/// <param name="string_secretContent">欲加密字串</param>
/// <param name="string_pwd">鹽(Salt)</param>
/// <returns></returns>
private string encrypt(string string_secretContent, string string_pwd)
{
//密碼轉譯一定都是用byte[] 所以把string都換成byte[]
byte[] byte_secretContent = Encoding.UTF8.GetBytes(string_secretContent);
byte[] byte_pwd = Encoding.UTF8.GetBytes(string_pwd);
alen1985 發表在 痞客邦 留言(0) 人氣(2,718)

有時想在GridView顯示流水號如下圖排列方式
可在GridView中加入一TemplateField並在其ItemTemplate裡加入<%#Container.DataItemIndex + 1%>,如以下片段
alen1985 發表在 痞客邦 留言(0) 人氣(680)

前言:在GRIDVIEW的使用中,我们经常会碰到这样的需求:GRIDVIEW虽然自带了“编辑”的功能,但是有时候我们不愿意在girdview直接进行修改 ,这时就得自己通过RowCommand事件来做这个编辑的功能
例子如下:这个是我项目中的教室的占用情况表,我要修改一天内几个时间段的使用情况,因为有特殊的需求,那些信息我是用模板列里面用label控件绑定的,修改时前几列是不需要动的,综合考虑,我没有用gridview自带的编辑功能,我通过一个按钮(放在隐藏列里,我通过双击或者右健菜单激发该事件)
这是双击第一条记录后出现的编辑界面:
那么这些数据已经在GRIDVIEW读出来了,编辑时我们就没有必要再从数据库中去读数据,直接将GRIDVIEW里的数据给拿过来绑定上就是,那么如何实现呢,后台代码如下:
alen1985 發表在 痞客邦 留言(0) 人氣(379)
NamingContainer
使用時機: 譬如 GridView裏的TemplateField 放了1個TextBox,1個CheckBox(設定AutoPostBack = "true")
畫面上的GridView假設有5筆資料,當其中一筆的CheckBox的事件CheckedChanged觸發時,可以藉由事件的參數 object sender,找到他是畫面上的哪一列,及該列的其它控制項。
aspx
alen1985 發表在 痞客邦 留言(0) 人氣(1,359)
FindControl 是 ASP.NET 工程師十分常用的 Method,但初學者應該常常會遇到使用 FindControl 卻找不到 Control 的狀況!
首先,在 ASP.NET 中有所謂 NamingContainer (命名容器) 的觀念,在使用 Data-bound Control ( 資料控制項 ) 時,會用到 ItemTemplate 之類(ITemplate)的標籤,裡面還會包含許多 Control,這些包含在 Template 裡面的 Control 其實是跟原本頁面(Page)中的控制項是不同階層的!而這些被 ITemplate 包含的 Controls 其 NamingContainer 就是這個 Data-bound Control 的每一個 ItemTemplate!
在這種情況下,若要在 Code behind 中使用 Page.FindControl 想直接找到這些 Data-bound Control 的 Template 中的 Control 的話,就沒辦法直接用 Control ID 找到這個 Control。
例如:
[code:html]
<asp:repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text="Text on row" />
</ItemTemplate>
</asp:repeater>
[/code]
在 Code behind 的 Page_Load 事件中要尋找 Repeater1 中第一個 TextBox1 時,使用以下的程式碼就會出錯,因為找不到 TextBox1:
[code:c#]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Collections.ArrayList a = new ArrayList();
a.Add("List 1");
a.Add("List 2");
Repeater1.DataSource = a;
Repeater1.DataBind();
TextBox txt1 = (TextBox)Page.FindControl("TextBox1"); // txt1 會是 null
Response.Write(txt1.Text); // 因為 txt1 是 null 而無法取得 txt1.Text 屬性而發生例外(Exception)
}
}
[/code]
如果要透過 FindControl 找到控制項的話,我是有以下 4 種技巧分享給大家:
1. 透過實做 Repeater1 的 ItemDataBound 事件來取得每一個 ItemTemplate 中 TextBox1 的控制項
這是最常見的用法!
[code:c#]
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
TextBox txt1 = (TextBox)e.Item.FindControl("TextBox1");
Response.Write(txt1.Text);
}
[/code]
2. 透過 Repeater1.Items[0].FindControl("TextBox1") 找到第一個 ItemTemplate 中的 TextBox1 控制項
這是取得第一個 ItemTemplate 中的 TextBox1 的常見用法!
3. 透過 Page.FindControl("Repeater1$ctl00$TextBox1") 找到第一個 ItemTemplate 中的 TextBox1 控制項
這算是使用 FindControl 的小技巧,因為 ASP.NET 的控制項如果沒有 ID 的話,都是從 ctl00 開始算起的。
因為 ItemTemplate 是 NamingContainer 但卻沒有設定 ID,所以第一筆就是 ctl00 第二筆就是 ctl01 依此類推。
4. 透過 FindControl<TextBox>("TextBox1") 找到整個頁面中第一個出現的 TextBox1 控制項(不一定在 Repeater1 裡面)
這是透過一個自訂的泛型遞迴方法(Generic Recursive Method)達成。
要使用這段程式比需將以下的程式碼複製到你的頁面的類別(Code behind)中。
( 以下程式碼參考自:http://blogs.interfacett.com/michael-palermo/2007/4/13/recursive-findcontrolt.html )
[code:c#]
public T FindControl<T>(
string id)
where T : Control
{
return FindControl<T>(Page, id);
}
public static T FindControl<T>(Control startingControl, string id) where T : Control
{
// 取得 T 的預設值,通常是 null
T found = default(T);
int controlCount = startingControl.Controls.Count;
if (controlCount > 0)
{
for (int i = 0; i < controlCount; i++)
{
Control activeControl = startingControl.Controls[i];
if (activeControl is T)
{
found = startingControl.Controls[i] as T;
if (string.Compare(id, found.ID, true) == 0) break;
else found = null;
}
else
{
found = FindControl<T>(activeControl, id);
if (found != null) break;
}
}
}
return found;
}
[/code]
alen1985 發表在 痞客邦 留言(0) 人氣(4,027)
Literal litCss = new Literal();
litCss.Text = @"
<style type=""text/css"">
.text{
display:block;
text-align:right;
}
</style>";
this.Header.Controls.Add(litCss);
alen1985 發表在 痞客邦 留言(0) 人氣(770)

假如想要在導向指定的網頁前,跳出訊息視窗的話,則使用以下方式的話,則訊息視窗會沒有跑出來,且導向指定的頁面
Response.Write("<script>alert('測驗時間到!')</script>")
Response.Redirect("result.aspx")
alen1985 發表在 痞客邦 留言(0) 人氣(3,867)
前端:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RemoteFileDownload.aspx.cs" Inherits="RemoteFileDownload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
alen1985 發表在 痞客邦 留言(0) 人氣(893)