头疼问题,如何固定GridView模板列的宽度?

annjoy303 2010-01-30 03:29:53
前台代码:

<asp:GridView ID="GVPublish" runat="server" CssClass="GridViewStyle" AutoGenerateColumns="False" Width="100%" EmptyDataText='<p style="text-align: center"><span style="color: #ff6600">*无数据*</span></p>' AllowPaging="True" OnPageIndexChanging="GVPublish_PageIndexChanging" PageSize="2" OnRowCommand="GVPublish_RowCommand" >
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />

<AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="标题" />
<asp:BoundField DataField="ColumnID" HeaderText="所属栏目" />

<asp:TemplateField HeaderText="编辑">
<HeaderStyle Width="40px" />
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" ImageUrl="~/Images/btpaste.gif" CommandName="btnEdit" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.InfoID") %>'
/>

</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="40px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<HeaderStyle Width="40px" />
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="btnDel" CommandArgument='<%#DataBinder.Eval(Container,"DataItem.InfoID") %>'
ImageUrl="~/Images/btdelete.gif" OnClientClick="return confirm('是否确定删除此文章?')" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="40px" />
</asp:TemplateField>

</Columns>
<PagerTemplate>
<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev" ></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" ></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" ></asp:LinkButton>
<br />
</PagerTemplate>
</asp:GridView>



显示效果:
效果图1(GridView第一页,模板列编辑和删除显示正常--宽度40px):


效果图2(GridView前2列较短,模板列编辑和删除显示不正常):


效果图3(GridView前2列较长,模板列编辑和删除显示正常):



将GridView的宽度设置成固定宽度的方法我已经试过了,同样无效!如何固定此模板列的宽度??

谢谢!!
...全文
1213 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyichang 2010-11-18
  • 打赏
  • 举报
回复
dddddddddddd
kujjgt 2010-01-31
  • 打赏
  • 举报
回复
后台Page_load事件也可以设置..
this.GridView1.HeaderRow.Cells[0].Width = 500;
ronaltn 2010-01-30
  • 打赏
  • 举报
回复
CSS
wuyq11 2010-01-30
  • 打赏
  • 举报
回复
GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
//下面这行是自动换行
GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");

或protected void GridView1_DataRowBound(object o, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//设置要换行的模板列
e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word");
}
}

设置ItemStyle 固定宽度
缪军 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 annjoy303 的回复:]
引用 23 楼 microtry 的回复:
表格的宽度100%,那么留下一个column不要设置绝对值,也设置为100%,其他的column都设置绝对值


这样其他列不就没地待了么?
[/Quote]

错了,是留一个column不设宽度
zhouzangood 2010-01-30
  • 打赏
  • 举报
回复
up
kingcsx666 2010-01-30
  • 打赏
  • 举报
回复
Width="100px"
annjoy303 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 microtry 的回复:]
表格的宽度100%,那么留下一个column不要设置绝对值,也设置为100%,其他的column都设置绝对值
[/Quote]

这样其他列不就没地待了么?
天下在我心 2010-01-30
  • 打赏
  • 举报
回复
<ItemStyle CssClass="item"></ItemStyle>

这样试下,在CSS里面设定宽度。
缪军 2010-01-30
  • 打赏
  • 举报
回复
表格的宽度100%,那么留下一个column不要设置绝对值,也设置为100%,其他的column都设置绝对值
annjoy303 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 jason_dct 的回复:]
我下了, 是在不行 就晚上 qq 吧  。
ok  see you then .
[/Quote]
qq?
段传涛 2010-01-30
  • 打赏
  • 举报
回复
我下了, 是在不行 就晚上 qq 吧 。
ok see you then .
段传涛 2010-01-30
  • 打赏
  • 举报
回复
---------------------------------
1.width: 99%; 改成一个定长 如 width: 320px;
-----------------------------------
如果1不行 就修改2

2可以先这样试一下。 把CssClass="GridViewStyle" 先剪切。
然后设置 列的属性 ----选中gridview 编辑列---样式-- 编辑他们的宽
段传涛 2010-01-30
  • 打赏
  • 举报
回复
哦 知道了 你的gridview的 外观设置 都在css里了。所以在属性里 设置的没有作用。
那就把我刚才 贴的代码 放在css里吧
annjoy303 2010-01-30
  • 打赏
  • 举报
回复
gridView外面的div样式:
#man_zone {
height: 445px;
width: 99%;
border: 1px solid #B4C9C6;
margin: 6px 0 0 0;
background-color:#FFFFFF;
padding: 5px 0 5px 0;
overflow: auto;
}
annjoy303 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 jason_dct 的回复:]
这个gridview 放在div里 还试table
这个样式表也要 设置一下。
[/Quote]

放在div中
annjoy303 2010-01-30
  • 打赏
  • 举报
回复
css:

.GridViewStyle
{
border-right: 1px solid #798a9c;
border-bottom: 1px solid #798a9c;
border-left: 1px solid #798a9c;
border-top: 1px solid #798a9c;
padding: 4px;
}

/* .GridViewStyle a
{
color: #FFFFFF;
}*/

.GridViewHeaderStyle th
{
border-left: 1px solid #EBE9ED;
border-right: 1px solid #EBE9ED;
}

.GridViewHeaderStyle
{
background-image:url('../Images/gridview_header.gif');
background-repeat:repeat-x;
font-weight: bold;
color: black;
height:20px;
}

.GridViewFooterStyle
{
background-color: #5D7B9D;
font-weight: bold;
color: White;
}

.GridViewRowStyle
{
background-color: #F7F6F3;
color: black;
}

.GridViewAlternatingRowStyle
{
background-color: #FFFFFF;
color: balck;
}

.GridViewRowStyle td, .GridViewAlternatingRowStyle td
{
border: 1px solid #EBE9ED;
}

.GridViewSelectedRowStyle
{
background-color: #E2DED6;
font-weight: bold;
color: #333333;
}

.GridViewPagerStyle
{
background-color: #284775;
color: #FFFFFF;
}

.GridViewPagerStyle table /**//* to center the paging links*/
{
margin: 0 auto 0 auto;
}
段传涛 2010-01-30
  • 打赏
  • 举报
回复
这个gridview 放在div里 还试table
这个样式表也要 设置一下。
段传涛 2010-01-30
  • 打赏
  • 举报
回复
CssClass="GridViewStyle"

-----------把这个css (GridViewStyle) 样式表的代码复制出来 我看看。
annjoy303 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 jason_dct 的回复:]
试试这个 不行的话 再留言
                                              <ControlStyle Width="80px" />
                                        <FooterStyle Width="80px" />
                                        <HeaderStyle Width="80px" />
                                        <ItemStyle Width="80px" />
[/Quote]

无效
加载更多回复(12)

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧