如何添加';更多…';jquery或javascript在Gridview列上的按钮

如何添加';更多…';jquery或javascript在Gridview列上的按钮,javascript,c#,jquery,asp.net,gridview,Javascript,C#,Jquery,Asp.net,Gridview,我在Gridview中有一个模板字段,可能很长。如果它超过100个字符,我想剪切或修剪它,而不是替换一个按钮“更多…”,如果用户点击它,整个文本将通过jquery或javascript显示。 这是我的GridView: 您可以通过以下示例执行此操作:- <asp:TemplateField HeaderText="Description"> <ItemTemplate> <asp:Label ID="lblDescription" runat="se

我在Gridview中有一个
模板字段
,可能很长。如果它超过100个字符,我想剪切或修剪它,而不是替换一个按钮“更多…”,如果用户点击它,整个文本将通过jquery或javascript显示。

这是我的GridView:


您可以通过以下示例执行此操作:-

<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)+ "<a>...</a>";
}
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: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)+ "<a>...</a>";
}
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;}

返回说明.Length在@Anand systemix给出答案后,您可以将其修改为与
jquery一起使用

<asp:TemplateField HeaderText="Description">
 <ItemTemplate>
      <asp:Label ID="lblDescription" runat="server"
                Text='<%# Limit(Eval("Description"),40) %>' 
                Tooltip='<%# Eval("Description") %>'>
      </asp:Label>
 </ItemTemplate>
</asp:TemplateField>

根据@Anand systemix的回答,您可以将其修改为与
jquery一起使用

<asp:TemplateField HeaderText="Description">
 <ItemTemplate>
      <asp:Label ID="lblDescription" runat="server"
                Text='<%# Limit(Eval("Description"),40) %>' 
                Tooltip='<%# Eval("Description") %>'>
      </asp:Label>
 </ItemTemplate>
</asp:TemplateField>

而不是
返回一个
锚定
标记,这将使其成为一个链接。非常感谢,但我只想通过**jquery或javascript**来完成,正如我在标题中提到的,因为由于某些原因,我无法使用Ajax,因此我不想再次加载页面!为此,您可以从标记中添加/调用java脚本函数,并可以删除它“OnClick=“ReadMoreLinkButton\u单击”来自itemtemplate。@zafy不需要进行ajax调用,在
锚定处返回的标记将具有一个id或类,您可以在
jquery
中使用该id或类替换存储在
标题中的文本而不是
返回一个
锚定
标记,这将使其成为一个链接。非常感谢ch,但我只想通过**jquery或javascript**来实现,正如我在标题中提到的,因为某些原因我无法使用Ajax,所以我不想再次加载页面!为此,您可以从标记中添加/调用java脚本函数,并可以删除“OnClick=”ReadMoreLinkButton\u Click“”来自itemtemplate。@zafy它不需要进行ajax调用,返回的
锚定
标记将具有一个id或类,您可以在
jquery
中使用该id或类替换存储在
title
attribute中的文本请问jquery中的函数是什么?无论我尝试什么都是错误的!请问jquery中的函数是什么?什么无论我怎样尝试都是错误的!