//正常换行
        GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //下面这行是自动换行
        GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script type="text/C#" runat="server">
int start_index;
ICollection CreateDataSource() 
{
  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("CurrencyValue"typeof(double)));

  
for (int i = start_index; i < start_index + ItemsGrid.PageSize; i++
  
{
    dr 
= dt.NewRow();    
    dr[
0= i;
    dr[
1= @"我是中文文字,I am English words,我不想换行,
            I don't wanna have new lines,欢迎访问 
            <a href='http://dotnet.aspx.cc/'>http://dotnet.aspx.cc/</a>,
            有好料啊:)
";
    dr[
2= 1.23 * (i+1);
    
    dt.Rows.Add(dr);
  }

    
  DataView dv 
= new DataView(dt);
  
return dv;
}


void Page_Load(Object sender, EventArgs e) 
{
  
//对于没有数字的内容,下面这行完全满足要求,但加了数字就不行,必须调用OnItemDataBound
  ItemsGrid.Attributes.Add("style","word-break:keep-all;word-wrap:normal");
    
  
//下面这行是自动换行
  
//ItemsGrid.Attributes.Add("style","word-break:break-all;word-wrap:break-word");
    
  
if (!IsPostBack)
  
{
    BindGrid();  
  }

}


void BindGrid() 
{
  ItemsGrid.DataSource
=CreateDataSource();
  ItemsGrid.DataBind();     
}


void Item_DataBound(Object sender, DataGridItemEventArgs e) 
{
  
if( e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType 
== ListItemType.AlternatingItem)
   e.Item.Cells[
1].Text = "<nobr>" + e.Item.Cells[1].Text + "</nobr>"
}

</script>
<body>
<form runat="server">
<asp:DataGrid id="ItemsGrid" runat="server" BorderColor="black"
    OnItemDataBound
="Item_DataBound" AutoGenerateColumns="false">

<AlternatingItemStyle BackColor="#DEDEDE"></AlternatingItemStyle>
<HeaderStyle BackColor="#EEEEFF" HorizontalAlign="Center"></HeaderStyle>

<Columns>
  
<asp:BoundColumn HeaderText="序号" DataField="IntegerValue"/>
  
<asp:BoundColumn HeaderText="文字" DataField="StringValue"/>
  
<asp:BoundColumn HeaderText="价格" DataField="CurrencyValue" DataFormatString="{0:c}">
  
<ItemStyle HorizontalAlign="right"></ItemStyle>
  
</asp:BoundColumn>
</Columns>

</asp:DataGrid>
</form>
</body>
</html>
posted @ 2007-12-29 17:12 Fernando 阅读(772) 评论(0) 编辑
posted @ 2007-12-29 15:25 Fernando 阅读(417) 评论(0) 编辑

如果我们想在3列布局的最后加一行页脚,放版权之类的信息。就遇到必须对齐3列底部的问题。在table布局中,我们用大表格嵌套小表格的方法,可以很方便对齐三列;而用div布局,三列独立分散,内容高低不同,就很难对齐。其实我们完全可以嵌套div,把三列放进一个div中,就做到了底部对齐。下面是实现例子(白色背景框模拟一个页面):

body
这里是#header{ margin: 0px; border: 0px; background: #ccd2de; width: 580px; height: 60px;}
这里是#mainbox { margin: 0px; width: 580px; background: #fff; }包含了#menu,#sidebar和#content
这里是#menu{ float: right; margin: 2px 0px 2px 0px; padding:0px 0px 0px 0px; width: 400px; background: #ccd2de; }

这里是#sidebar{ float: left; margin: 2px 2px 0px 0px; padding: 0px; background: #f2f3f7; width: 170px; },背景颜色用的是#main的背景色

这里是#content{ float: right; margin: 1px 0px 2px 0px; padding:0px; width: 400px; background: #e0efde;}

这里是主要内容,根据内容自动适应高度

这里是主要内容,根据内容自动适应高度

这里是主要内容,根据内容自动适应高度

这里是#footer{ clear: both; margin: 0px 0px 0px 0px; padding: 5px 0px 5px 0px; background: #ccd2de; height: 40px; width: 580px; }。

这个例子的页面主要代码如下:

<div id="header"></div>
<div id="mainbox">
    <div id="menu"></div>
    <div id="sidebar"></div>
    <div id="content"></div>
</div>
<div id="footer"></div>

具体样式表都写在相应版块里了。重点在于#mainbox层嵌套了#menu,#sidebar和#content三个层。当#content的内容增加,#content的高度就会增高,同时#mainbox的高度也会撑开,#footer层就自动下移。这样就实现了高度的自适应。

另外值得注意的是:#menu和#content都是浮动在页面右面"float: right;",#sidebar是浮动在#menu层的左面"float: left;",这是浮动法定位,还可以采用绝对定位来实现这样的效果。

这个方法存在另一个问题,就是侧列#sidebar的背景无法百分之百。一般的解决办法就是用body的背景色来填充满。(不能使用#mainbox的背景色,因为在mozilla等浏览器中#mainbox的背景色失效。)

好了,主要的框架已经搭建完毕,剩下的工作只是往里面添砖加瓦。

posted @ 2007-12-29 15:22 Fernando 阅读(146) 评论(0) 编辑

Hi Paula,

You can assign the ID for each row in the GridView via the sample code below. The code belows will give each row a unique ID from a global variable.

Dim
intGRID as Integer = 1 'Global declaration

Protected Sub SampleGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles SampleGridView.RowDataBound

e.Row.ID = "sampleGridRow_" & Cstr(intGRID)
intGRID += 1 'Increase intGRID by one

End Sub

Hope this helps.


Hi all!

emptycan, thank you very much! Your solution is excellent! Yes

If you don't want use additional variables, you can do this:

protected void SomeGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.ID = Guid.NewGuid().ToString();
}

Henk, not quite.  The Guid would be different for each postback, recreating the original problem.  Emptycan's solution works because his code will recreate the exact same IDs on each postback.

-Jason 

posted @ 2007-12-29 11:37 Fernando 阅读(778) 评论(3) 编辑