C# 如何使用Read More链接限制GridView中的标签字符串长度?
现在我用的是这样的C# 如何使用Read More链接限制GridView中的标签字符串长度?,c#,asp.net,string,gridview,C#,Asp.net,String,Gridview,现在我用的是这样的 <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label ID="lblDescription" runat="server" Text='<%# Limit(Eval("Description"),40) %>' > </asp:Label> </ItemTempla
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
Text='<%# Limit(Eval("Description"),40) %>' >
</asp:Label>
</ItemTemplate>
但是我不知道如何在标签后面添加一个不可见的HtmlAnchor控件。试一试
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
Text='<%# Limit(Eval("Description"),40) %>' >
</asp:Label>
<a href="TheReadMorePage" ID="aReadMore" runat="server" Visible="false">[Read More]</a>
</ItemTemplate>
if(strDesc.Length > length)
{
var anchor = ((Label)Desc).NamingContainer.FindControl("aReadMore");
anchor.Visible = true;
return strDesc.ToString().Substring(0, length) + "...";
}
如果(标准长度>长度)
{
var anchor=((Label)Desc.NamingContainer.FindControl(“aredmore”);
anchor.Visible=true;
返回strDesc.ToString().Substring(0,长度)+“…”;
}
这样做
加价
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
Text='<%# Limit(Eval("Description"),40) %>'
Tooltip='<%# Eval("Description") %>'>
</asp:Label>
<asp:LinkButton ID="ReadMoreLinkButton" runat="server"
Text="Read More"
Visible='<%# SetVisibility(Eval("Description"), 40) %>'
OnClick="ReadMoreLinkButton_Click">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
和代码隐藏
protected bool SetVisibility(object desc, int maxLength)
{
var description = (string)desc;
if (string.IsNullOrEmpty(description)) { return false; }
return description.Length > maxLength;
}
protected void ReadMoreLinkButton_Click(object sender, EventArgs e)
{
LinkButton button = (LinkButton)sender;
GridViewRow row = button.NamingContainer as GridViewRow;
Label descLabel = row.FindControl("lblDescription") as Label;
button.Text = (button.Text == "Read More") ? "Hide" : "Read More";
string temp = descLabel.Text;
descLabel.Text = descLabel.ToolTip;
descLabel.ToolTip = temp;
}
protected string Limit(object desc, int maxLength)
{
var description = (string)desc;
if (string.IsNullOrEmpty(description)) { return description; }
return description.Length <= maxLength ?
description : description.Substring(0, maxLength)+ "...";
}
protected bool SetVisibility(object desc,int maxLength)
{
变量描述=(字符串)描述;
if(string.IsNullOrEmpty(description)){return false;}
返回说明。长度>最大长度;
}
受保护的无效ReadMoreLinkButton\u单击(对象发送者,事件参数e)
{
LinkButton按钮=(LinkButton)发送方;
GridViewRow行=button.NamingContainer作为GridViewRow;
Label descLabel=row.FindControl(“lblDescription”)作为标签;
button.Text=(button.Text=“阅读更多”)?“隐藏”:“阅读更多”;
字符串温度=descLabel.Text;
descLabel.Text=descLabel.ToolTip;
descLabel.ToolTip=温度;
}
受保护的字符串限制(对象描述,int maxLength)
{
变量描述=(字符串)描述;
if(string.IsNullOrEmpty(description)){return description;}
返回说明。长度您可以使用模式弹出对话框:
在ASP.Net页面中:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
$('#btnReadMore').click(function() {
$("#popupdiv").dialog({
title: "jQuery Popup from Server Side",
width: 430,
height: 250,
modal: true,
buttons: {
Close: function() {
$(this).dialog('close');
}
}
});
});
})
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
Text='<%# Limit(Eval("Description"),40) %>'
Tooltip='<%# Eval("Description") %>'>
</asp:Label>
<input type="button" id="btnReadMore" value="Show Modal Popup" />
</ItemTemplate>
</asp:TemplateField>
<div>
<div id="popupdiv" title="Basic modal dialog" style="display: none">
</div>
$(函数(){
$('#btnredmore')。单击(函数(){
$(“#popupdiv”)。对话框({
标题:“从服务器端弹出jQuery”,
宽度:430,
身高:250,
莫代尔:是的,
按钮:{
关闭:函数(){
$(this.dialog('close');
}
}
});
});
})
我想为你的答案投赞成票……但声誉不够。:(现在这一个已经读了很多了,rite?Hide怎么样。感谢当前上下文中不存在“Limit”这个名称?@Azzy:从问题中获取Limit函数。将函数签名从public static string Limit(object Desc,int length)更改为public static string Limit(object Desc,int length)
到受保护的字符串限制(object Desc,int length)
谢谢你的建议…你的答案可能会有帮助,但我没有Admore页面的链接。因此,你希望它保持在同一页面上,但如果它们单击,只需展开?将“锚定”设置为一个按钮(或链接按钮,如果你仍然想要[阅读更多]
查看)相反,在这种情况下,如果单击按钮,则返回完整字符串。是,单击按钮时希望保持在同一页面中…如何更改您的代码…thx以获得您的答复
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
$('#btnReadMore').click(function() {
$("#popupdiv").dialog({
title: "jQuery Popup from Server Side",
width: 430,
height: 250,
modal: true,
buttons: {
Close: function() {
$(this).dialog('close');
}
}
});
});
})
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server"
Text='<%# Limit(Eval("Description"),40) %>'
Tooltip='<%# Eval("Description") %>'>
</asp:Label>
<input type="button" id="btnReadMore" value="Show Modal Popup" />
</ItemTemplate>
</asp:TemplateField>
<div>
<div id="popupdiv" title="Basic modal dialog" style="display: none">
</div>