您可以通過下面的例子做到這一點: -
<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>";
}
編輯: -
從標籤如果只使用JavaScript可以添加/調用JavaScript功能,可以刪除 「的OnClick =」從itemtemplate中的ReadMoreLinkButton_Click「」。
是在jQuery中的功能嗎?不管我嘗試是錯的! –
嘗試編輯'jquery'部分 –