这个问题在这里已有答案:
我有一张桌子,桌子上有几个按钮和一个Gridview。我试图将文本包装在Gridview的一个boundfield
中。
我试图在Gridview属性中设置RowStyle
Wrap="true"
并在boundfield属性中设置ItemStyle
Wrap="true"
和Width
,但是没有用。
以下是我的aspx
。
<table align="center" border="0" cellpadding="0" cellspacing="2" >
<tr>
<td></td>
<td align="right">
<asp:Button ID="btnAdd" runat="server" Text="Add Subscription"
onclick="btnAdd_Click" CausesValidation="False" />
</td>
</tr>
<tr>
<td colspan="2">
<p align="center" style="font-family: Arial, Helvetica, sans-serif; font-size: 14px" >
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</p>
</td>
</tr>
<tr>
<td align="left" colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="SubscriptionID,UserID"
DataSourceID="SqlDSEmailSubscriptions" Width="90%" CellPadding="4"
EnableViewState="False" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="SubscriptionName" SortExpression="SubscriptionName">
<ItemTemplate>
<asp:LinkButton ID="lbtnSubscription" runat="server" CausesValidation="false"
Text='<%# Eval("SubscriptionName")%>' OnClick="lbtnSubscription_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SubscriptionName" HeaderText="SubscriptionName"
SortExpression="SubscriptionName" Visible="false" />
<asp:BoundField DataField="SubscriptionID" HeaderText="SubscriptionID"
ReadOnly="True" SortExpression="SubscriptionID" />
<asp:BoundField DataField="ProductList" HeaderText="ProductList"
SortExpression="ProductList" />
<asp:BoundField DataField="DivisionList" HeaderText="DivisionList"
SortExpression="DivisionList" />
<asp:BoundField DataField="DisciplineList" HeaderText="DisciplineList"
SortExpression="DisciplineList" />
<asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True"
SortExpression="UserID" Visible="false" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDSEmailSubscriptions" runat="server"
ConnectionString="<%$ ConnectionStrings:SPRConnectionString %>"
SelectCommand="SELECT [SubscriptionID], [SubscriptionName], [ProductList], [DivisionList], [DisciplineList], [UserID] FROM [sprEmailSubscriptions] WHERE ([UserID] = @UserID) ORDER BY [SubscriptionName]">
<SelectParameters>
<asp:SessionParameter Name="UserID" SessionField="userID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
在具有固定长度的gridview列中包装文本。
首先在gridview中创建列,其中文本将被包装为ItemTemplate
。
这可以通过以下方式完成:
在源代码中,您将看到以下代码:
<asp:TemplateField HeaderText="name" SortExpression="name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name")%>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
为列提供宽度限制,以像素为单位:
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>' Width="200px"></asp:Label>
</ItemTemplate>
现在要添加样式:
如果要将列中的文本换行应用于所有列或整个网格视图,请在page_load()
事件中编写以下代码:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");
}
如果将列中的文本包装仅应用于网格视图的特定列,则在GridView1_RowDataBound()
事件中编写以下代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Attributes.Add("style", "word-break:break-all;word-wrap:break-word;");
}
}
检查gridview的单元格编号。
完成工作!
在项目模板中使用Div工作Gr8
<ItemTemplate>
<div style="word-wrap: break-word; width: 530px;>
<asp:Label ID="lblTermName" runat="server" Text='<%# Eval("TermName") %>' />
</div>
</ItemTemplate>
是的,您可以通过每个“x”字符解析它,但也许更好的解决方案是将列标题放入固定大小并定义换行“true”。
通过该选项,您每次都可以获得相同的gridview大小,并且用户界面更清晰,更清晰。
使用代码:
<asp:BoundField DataField:="Your_data_field" HeaderText="Your_text" sordExpression="Your_sort_expression" ItemStyle-wrap="true" ItemStyle-with="50" />