如何在Gridview的boundfield中实现文本换行?
我有一张桌子,桌子上有几个按钮和一个网格视图。我试图将文本包装在 Gridview 的 boundfield
之一中。
我尝试在 Gridview 属性中设置 RowStyle
Wrap="true"
并设置 ItemStyle
Wrap="true"< /code> 和boundfield 属性中的
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>
I have a table, in the table there are couple of buttons and a Gridview. I am trying to wrap the text in one of the boundfield
of the Gridview.
I have tried to set the RowStyle
Wrap="true"
in the Gridview properties and set the ItemStyle
Wrap="true"
and the Width
in the boundfield properties, but didn't work.
Following is my 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将文本包装在具有固定长度的 gridview 列中。
首先在 gridview 中创建列,其中文本将被包装为
ItemTemplate
。这可以通过以下方式完成:
在源代码中,您将看到以下代码:
以像素为单位给出列的宽度限制为:
现在要添加样式:
如果要将列中的文本换行应用于所有列或整个网格视图,则在 < 中编写以下代码code>page_load() 事件:
如果列中的文本换行仅应用于网格视图的特定列,则在
GridView1_RowDataBound()
事件中编写以下代码:检查单元格编号网格视图的。
工作完成!
Wrapping text in a gridview column having fixed length.
First make the column in gridview, where text is to be wrapped as
ItemTemplate
.This can be done by:
In source you will see the following code:
Give width limit to the column in pixels as :
Now styles are to be added:
If wrapping of text in column is to be applied to all columns or entire grid view then write following code in
page_load()
event:If wrapping of text in column is to be applied only to a particular column of grid view then write following code in
GridView1_RowDataBound()
event:Check the cell number of the gridview.
Work Done!
在项目模板 Works Gr8 中使用 Div
Using a Div in Item Template Works Gr8
是的,您可以通过每个“x”字符来解析它,但也许将列标题放入固定大小并定义换行“true”是更好的解决方案。
通过该选项,您每次都会获得相同的 gridview 大小以及更好、更清晰的用户界面。
使用代码:
Yes, you can parse it by every "x" caracters, but maybe it is better solution to put into header of columns to be fixed size and define wrap "true".
by that option you'll get same gridview size every time and better and more clearly user interface.
Use code: