09 Nov 2011

AutoGenerateColumns='true'的GridView如何支持htmlencode/htmldecode

我们知道BoundField.HtmlEncode 属性可以设置一个单元格是否能够htmlencode,我今天碰到的问题是:自动生成列的GridView,表头可能是包含html的比如<font color=red>邮件</font>,默认被htmlencode了,而我不希望这样做,所以有了如下的代码:

 

/// <summary>
/// Handles the PreRender event of the gvSource control.
/// </summary>
/// <param name=“sender”>The source of the event.</param>
/// <param name=“e”>The <see cref=“System.EventArgs”/> instance containing the event data.</param>
protected void gvSource_PreRender( object sender , EventArgs e )
{
    GridView gridView = sender as GridView;
    if ( gridView != null && gridView.HeaderRow != null && gridView.HeaderRow.Cells != null )
    {
        for ( int j = 0 ; j < gridView.HeaderRow.Cells.Count ; j++ )
        {
            gridView.HeaderRow.Cells[j].Text = System.Web.HttpUtility.HtmlDecode( gridView.HeaderRow.Cells[j].Text );
        }
    }
}

03 Jul 2006

code-beside方式绑定GridView后EnableSortingAndPagingCallbacks不工作

今天对GridView的学习中发现 当用 code-beside方式绑定GridView后(比如我绑一个DataTable)
EnableSortingAndPagingCallbacks不起作用。
在下面地址找到 一些说法

http://forums.asp.net/thread/1230450.aspx

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=103040
记之…

 1    <asp:GridView id=“Gridviewex1” 
 2    runat=“server” 
 3    AllowSorting=“True” 
 4    AutoGenerateColumns=“False”
 5    EnableSortingAndPagingCallbacks=“True” 
 6    OnSorting=“Gridviewex1_Sorting”>
 7        <Columns>
 8            <asp:BoundField DataField=“ProductId” HeaderText=“ProductId” SortExpression=“ProductId” />
 9            <asp:BoundField DataField=“ProductName” HeaderText=“ProductName” SortExpression=“ProductName” />
10            <asp:BoundField DataField=“QuantityPerUnit” HeaderText=“QuantityPerUnit” SortExpression=“QuantityPerUnit” />
11            <asp:BoundField DataField=“UnitsInStock” HeaderText=“UnitsInStock” SortExpression=“UnitsInStock” />
12            <asp:BoundField DataField=“ReorderLevel” HeaderText=“ReorderLevel” SortExpression=“ReorderLevel” />
13            <asp:BoundField DataField=“Discontinued” HeaderText=“Discontinued” SortExpression=“Discontinued” />
14        </Columns>
15    </aspx:GridView>
 1    protected void Page_Load(object sender, EventArgs e)
 2    {
 3
 4        if (!Page.IsPostBack)
 5        {
 6            this.BindGrid(string.Empty);
 7        }

 8    }

 9
10    private void BindGrid(string sortExpression)
11    {
12        DataTable dt = new DataTable();
13        SqlConnection conn = new SqlConnection();
14        conn.ConnectionString = server=wumeibo\wmb;database=northwind;uid=sa;pwd=sa;;
15        SqlCommand cmd = new SqlCommand();
16        cmd.Connection = conn;
17        cmd.CommandType = CommandType.Text;
18        cmd.CommandText = select * from products;
19        SqlDataAdapter adapter = new SqlDataAdapter();
20        adapter.SelectCommand = cmd;
21        adapter.Fill(dt);
22        DataView dv = dt.DefaultView;;
23        if (!string.IsNullOrEmpty(sortExpression))        
24        {
25            dv = dt.DefaultView;
26            dv.Sort = sortExpression;
27        }

28        this.Gridviewex1.DataSource = dv;
29        this.Gridviewex1.DataBind();
30        
31    }

32    protected void Gridviewex1_Sorting(object sender, GridViewSortEventArgs e)
33    {
34        BindGrid(e.SortExpression);
35    }

经过测试 如下方法 可用

 1     protected void Page_Load(object sender, EventArgs e)
 2     {
 3             xx();
 4         
 5     }
 6 
 7     private void xx()
 8     {
 9         SqlConnection conn = new SqlConnection();
10         conn.ConnectionString = server=wumeibo\wmb;database=northwind;uid=sa;pwd=sa;;
11    
12         SqlDataSource source = new SqlDataSource();
13         source.ConnectionString = server=wumeibo\wmb;database=northwind;uid=sa;pwd=sa;;
14         source.SelectCommand = select * from products;
15 
16         this.Gridviewex1.DataSource = source;
17         this.Gridviewex1.DataBind();
18     }

08 Apr 2006

两个DropDownList在Repeater、DataList、DataGrid内的连动和冒泡事件(BubbleEvent)

两个DropDownList在Repeater、DataList、DataGrid内的连动和冒泡事件(BubbleEvent)
        问题:
http://community.csdn.net/Expert/topic/4670/4670056.xml?temp=3.944033E-02
        刚看第一眼觉得在第一个DDL的SelectedIndexChanged事件内绑定第二个DDL就ok了,页面上写或者ItemDataBound内写事件挂接代码都可以。打开VS写测试却发现有点障碍哦,
在SelectedIndexChanged内要知道去绑定哪个行哪个列的DDL要费些周折
只有从DDL一级一级的向上找Parent最后找到DataGridItem,再利用其ItemIndex定位到行然后FindControl到要绑定的DDL,最终可以实现,好像比较累哦。
        随又想到DataGrid的ItemCommand事件,但发现WebControl内只有Button ImageButton LinkButtonCommandName属性和 CommandArgument属性,并可以将事件上浮(冒泡).DropDownList先天不足!真是郁闷~~~~~ 能不能自己改造呢?动手试试就知道了。

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<!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" xml:lang="gb2312" lang="gb2312">
<head>
<title> ItemDataBoundGetColumnName </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="title" content="" />
<meta name="author" content="活靶子,Huobazi,www.AspxBoy.com" />
<meta name="subject" content="" />
<meta name="language" content="gb2312" />
<meta name="keywords" content="" />
<meta name="Copyright" content="www.AspxBoy.com" />
<meta name="robots" content="all" />

<script language="c#" runat="server">
void BindGrid()
{
        DataTable dt = new DataTable();
        DataRow dr;
        dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
        dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
        dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
        dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
        dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
        for (int i = 0; i < 36; i++) {
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "项 " + i.ToString();
            dr[2] = DateTime.Now;
            dr[3] = (i % 2 != 0) ? true : false;
            dr[4] = 1.23 * (i+1);
            dt.Rows.Add(dr);
        }
  dg.DataSource= dt;
  dg.DataBind();
}
void Page_Load(object o, EventArgs e)
{
     dg.PagerStyle.Mode = PagerMode.NumericPages;
     if(!IsPostBack)
     {
        BindGrid();
     }
}
 
void btnClick(object o , EventArgs e)
{
     Response.Write("页面回发,但是不执行ItemDataBound");
}
 
void PageChange(object o , DataGridPageChangedEventArgs e)
{
     dg.CurrentPageIndex = e.NewPageIndex;
     BindGrid();
}
void ItemDataBound(object o , DataGridItemEventArgs e)
{
     if(e.Item.ItemIndex > -1 )//必须
     {
          DataRowView drv = (DataRowView)e.Item.DataItem;
          for (int i=0; i<drv.Row.Table.Columns.Count ; i++)
          { 
            Response.Write(drv.Row.Table.Columns[i].ColumnName + "&nbsp;&nbsp;");
          }
     }
}
</script>

</head>
<body>
<form runat="server">
  <ASP:DataGrid id="dg" runat="server"      
   HeaderStyle-BackColor="#aaaadd"
   AutoGenerateColumns="true"
   AllowPaging="true"
   PageSize="6"
    OnItemDataBound="ItemDataBound"
    OnPageIndexChanged="PageChange"
  >
  </asp:DataGrid>
  </br>
   <asp:button id="btn" runat="server" onclick="btnClick" Text="我是按钮,按我一下"></asp:button>
在AutoGenerateColumns="false"使用绑定列或者模板列的时候只需要使用 

<font color="blut">YourDataGrid.Columns[编号].HeaderText</font> 去获取
  
</form>
</body>
</html>

http://www.aspxboy.com/Files/71/66/284.Aspx