GridView comes with a new feature that's not present in DataGrid. This new feature, namely, HtmlEncode, is used to prevent cross-site scripting and is applied to the BoudField element. The default setting for this property is set to be on(HtmlEncode="true"), and what it does is to html-encode the contents of gridviews so they are treated like plain text and therefore any malicious code won't get executed.
Hence if we try to output html contents to some of the gridview columns, we have to do either of the followings:
1. in the RowDataBound event handler, use Server.HTMLDecode to decode all the contents
2. set the HTMLEncode property of the BoundField to false for those columns
As a side note, if we use HTMLEncode="false", then all the contents will be treated as html, and some of the original format might be lost. For example, the new line character (char(13) + char(10)) might be converted into a blank space. In this event, it has to be replaced by '<br>' for the effects to show.
2008年7月23日 星期三
2007年11月20日 星期二
.NET GridView EmptyDataTemplate
在.NET 2.0裡面,GridView的EmptyDataTemplate是用來指定GridView內容為0比資料時的顯示結果,當葉面執行以後,GridView會被render為以下形式:
<div>
<table cellspacing="2" border="0" id="ctl00_ContentPlaceHolder1_gv_Authpat">
......
</table>
</div>
而EmptyDataTemplate則會被render為以下形式:
<tr class="Theader" align="center">
<td colspan="8">
~ EmptyDataTemplate的內容 ~
</td>
</tr>
在EmptyDataTemplate裡面,我們除了可以使用一般的server control之外,也可以加入一般的html語法,因為在EmptyDataTemplate裡面的html語法並不需要通過editor的驗證,所以即使是不合乎定義的html語法,也可以被加進來,例如:
專利件編號</td>
<td style="width:8%">單位</td>
<td style="width:10%">申請國別</td>
<td style="width:25%">專利名稱</td>
<td style="width:10%">成果來源</td>
<td style="width:20%">獲證日及証號</td>
<td style="width:10%">申請狀態
綜合以上的例子,所render出來的html結果如下:
<div>
<table cellspacing="2" border="0" id="ctl00_ContentPlaceHolder1_gv_Authpat">
<tr class="Theader" align="center">
<td colspan="8">
專利件編號</td>
<td style="width:8%">單位</td>
<td style="width:10%">申請國別</td>
<td style="width:25%">專利名稱</td>
<td style="width:10%">成果來源</td>
<td style="width:20%">獲證日及証號</td>
<td style="width:10%">申請狀態
</td>
</tr>
</table>
</div>
<div>
<table cellspacing="2" border="0" id="ctl00_ContentPlaceHolder1_gv_Authpat">
......
</table>
</div>
而EmptyDataTemplate則會被render為以下形式:
<tr class="Theader" align="center">
<td colspan="8">
~ EmptyDataTemplate的內容 ~
</td>
</tr>
在EmptyDataTemplate裡面,我們除了可以使用一般的server control之外,也可以加入一般的html語法,因為在EmptyDataTemplate裡面的html語法並不需要通過editor的驗證,所以即使是不合乎定義的html語法,也可以被加進來,例如:
專利件編號</td>
<td style="width:8%">單位</td>
<td style="width:10%">申請國別</td>
<td style="width:25%">專利名稱</td>
<td style="width:10%">成果來源</td>
<td style="width:20%">獲證日及証號</td>
<td style="width:10%">申請狀態
綜合以上的例子,所render出來的html結果如下:
<div>
<table cellspacing="2" border="0" id="ctl00_ContentPlaceHolder1_gv_Authpat">
<tr class="Theader" align="center">
<td colspan="8">
專利件編號</td>
<td style="width:8%">單位</td>
<td style="width:10%">申請國別</td>
<td style="width:25%">專利名稱</td>
<td style="width:10%">成果來源</td>
<td style="width:20%">獲證日及証號</td>
<td style="width:10%">申請狀態
</td>
</tr>
</table>
</div>
2007年9月4日 星期二
.NET GridView hidden column problem
在.NET 1.1的DataGrid裡面,column的visible屬性可以被設成false,造成hidden column的效果,同時在codebehind裡面,hidden column的值還是可以被取得
但是在NET 2.0的GridView裡面,如果將visible設成false,那麼在codefile裡面,這個欄位的值將是空字串,這是因為只要visible是false,GridView在DataBind時,便會自動略過去此欄位
解決方法為:
在GridView的RowCreated event裡面,再設定隱藏的欄位,例如:
protected void gv_Coorgcd_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[4].Visible = false;
}
因為這個event handler是在data bound之後才會被觸發的,所以column裡面才會有資料
但是在NET 2.0的GridView裡面,如果將visible設成false,那麼在codefile裡面,這個欄位的值將是空字串,這是因為只要visible是false,GridView在DataBind時,便會自動略過去此欄位
解決方法為:
在GridView的RowCreated event裡面,再設定隱藏的欄位,例如:
protected void gv_Coorgcd_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[4].Visible = false;
}
因為這個event handler是在data bound之後才會被觸發的,所以column裡面才會有資料
2007年8月24日 星期五
.NET 2.0 GridView RowCommand
我們常常需要再GridView裡面加入Controls,例如LinkButton, ImageButton之類的,
,只要設定CommandName屬性,便可使區分各種不同的Control所引發的事件
加入Button的方法可能有:
1. 使用TemplateField
<asp:TemplateField HeaderText="規劃案號" SortExpression="planno" ItemStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("planno") %>' CommandName="Select"
CommandArgument='<%# Eval("planno") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
2. 使用ButtonField
<asp:ButtonField HeaderText="規劃案號" ButtonType="Link" DataTextField="planno"
SortExpression="planno"
CommandName="Select" ItemStyle-Width="20%" />
問題:
.NET 1.x的DataGrid: 可以使用ItemCommand event handler的e.Item.ItemIndex這樣的語法來獲得目前的RowIndex
.NET 2.0的GridView: 其RowCommand event handler並沒有相對應於ItemIndex這樣的屬性
解決方法:
1. 使用第一種方法(assuming ImageButton Control is used)
int rowIndex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
或是(assuming LinkButton is used)
int rowindex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex
便可取得所需的row index了
2. 使用第二種方式
則e.CommandArgument便可取得RowIndex了,不過缺點是這個方法就沒辦法另外加CommandArgument
另外一個解決方式:
在RowCreated event handler中將屬於每一行的RowIndex給指定到CommandArgument中,便可以在之後的RowCommand event handler裡面使用e.CommandArgument來直接取得RowIndex了,這樣做的缺點是,如果你有其他的參數想要指定給CommandArgument的話,就沒辦法了
參考網頁:
http://www.cnblogs.com/oomusou/archive/2007/06/26/796094.html
http://msdn2.microsoft.com/zh-tw/library/system.web.ui.webcontrols.gridview.rowcreated(VS.80).aspx
,只要設定CommandName屬性,便可使區分各種不同的Control所引發的事件
加入Button的方法可能有:
1. 使用TemplateField
<asp:TemplateField HeaderText="規劃案號" SortExpression="planno" ItemStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("planno") %>' CommandName="Select"
CommandArgument='<%# Eval("planno") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
2. 使用ButtonField
<asp:ButtonField HeaderText="規劃案號" ButtonType="Link" DataTextField="planno"
SortExpression="planno"
CommandName="Select" ItemStyle-Width="20%" />
問題:
.NET 1.x的DataGrid: 可以使用ItemCommand event handler的e.Item.ItemIndex這樣的語法來獲得目前的RowIndex
.NET 2.0的GridView: 其RowCommand event handler並沒有相對應於ItemIndex這樣的屬性
解決方法:
1. 使用第一種方法(assuming ImageButton Control is used)
int rowIndex = ((GridViewRow)((ImageButton)e.CommandSource).NamingContainer).RowIndex;
或是(assuming LinkButton is used)
int rowindex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex
便可取得所需的row index了
2. 使用第二種方式
則e.CommandArgument便可取得RowIndex了,不過缺點是這個方法就沒辦法另外加CommandArgument
另外一個解決方式:
在RowCreated event handler中將屬於每一行的RowIndex給指定到CommandArgument中,便可以在之後的RowCommand event handler裡面使用e.CommandArgument來直接取得RowIndex了,這樣做的缺點是,如果你有其他的參數想要指定給CommandArgument的話,就沒辦法了
參考網頁:
http://www.cnblogs.com/oomusou/archive/2007/06/26/796094.html
http://msdn2.microsoft.com/zh-tw/library/system.web.ui.webcontrols.gridview.rowcreated(VS.80).aspx
2007年8月23日 星期四
.NET 2.0在GridView沒有資料時顯示表頭的方法
DataGrid在沒有資料時,會顯示出表頭,但是在GridView沒有資料時,不會顯示任何東西
這時我們可以使用EmptyDataTemplate來製作要顯示的內容, 例如:
這時我們可以使用EmptyDataTemplate來製作要顯示的內容, 例如:
<asp:GridView ID="gv_Emper" runat="server">
<EmptyDataTemplate>
<asp:Table runat="server">
<asp:TableHeaderRow runat="server" CssClass="Theader" HorizontalAlign="center">
<asp:TableHeaderCell runat="server" Text="檢視"></asp:TableHeaderCell>
<asp:TableHeaderCell runat="server" Text="姓名"></asp:TableHeaderCell>
<asp:TableHeaderCell runat="server" Text="工號"></asp:TableHeaderCell>
<asp:TableHeaderCell runat="server" Text="分機"></asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</EmptyDataTemplate>
~~~~略~~~~
</asp:GridView>
訂閱:
文章 (Atom)