我有一个Gridview从数据库中提取描述文本,一旦超过某些文本限制,我希望Gridview让用户点击"阅读更多"从那里他们可以看到全文而无需导航到新的URL。先感谢您。 我的Gridview列如下:
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="Source" HeaderText="Source" SortExpression="Source" />
<asp:BoundField DataField="Message" HeaderText="Message" SortExpression="Message" />
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:dd/MM/yyyy}" SortExpression ="Date" />
答案 0 :(得分:2)
使用Lable Control和LinkButton添加ItemTemplate,如GridView中所示:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label20" runat="server" Text='<%#((string)Eval("ColumnName")).Length<20?Eval("ColumnName"):((string)Eval("ColumnName")).Substring(0,20)+"..." %>' ToolTip='<%#Eval("question") %>' Width="100"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" Visible='<%#SetVisibility(Eval("ColumnName"),20) %>' OnClick="LinkButton1_Click">Read More...</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
这里的Lable文本只包含20个字符和链接按钮,将显示Read More但Lable工具提示包含该列的所有字符。在链接按钮上单击,我们将触发一个事件以显示该标签中的所有数据。
protected void LinkButton1_Click(object sender, EventArgs e)
{
//Read More Link Button from Gridview on Click
LinkButton lb = (LinkButton)sender;
GridViewRow row = lb.NamingContainer as GridViewRow;
//Finding the description Text Lable
Label qst = row.FindControl("Label20") as Label;
// Setting Link Button Text
lb.Text = (lb.Text == "Read More...") ? "Hide..." : "Read More...";
//Swaping tooltip value to text and vices versa as Tooltip has all charecters
string temp = qst.Text;
qst.Text = qst.ToolTip;
qst.ToolTip = temp;
}
protected bool SetVisibility(object Desc, int length)
{
return Desc.ToString().Length > length;
}
希望这可以帮助您找到解决方案。
<强> 更新 强>
似乎你的列有空值来处理这个你可以检查null然后转换:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label20" runat="server" Text='<%#Eval("ColumnName")==DBNull.Value ? string.Empty :(((string)Eval("ColumnName")).Length<20?Eval("ColumnName"):((string)Eval("ColumnName")).Substring(0,20)+"...") %>' ToolTip='<%#Eval("question") %>' Width="100"></asp:Label>
<asp:LinkButton ID="LinkButton1" runat="server" Visible='<%# Eval("ColumnName")==DBNull.Value ? false :SetVisibility(Eval("ColumnName"),20) %>' OnClick="LinkButton1_Click">Read More...</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
答案 1 :(得分:0)
我建议使用linkbutton并绑定ItemTemplate
中的消息:
<asp:TemplateField HeaderText="Message">
<ItemTemplate>
<%# Bind("Message") %> - <asp:linkbutton id="btnReadMore" runat="server" causesvalidation="false" commandname="readmore" commandargument="<%# Bind("MessageId") %>" />
</ItemTemplate>
</asp:TemplateField>
然后,链接按钮的逻辑可以弹出
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "readmore")
{
Session["messageid"] = e.CommandArgument.ToString();
Response.Redirect("popuppage.aspx?id=" + Session["messageid"].ToString() + "");
}
}
在popuppage.aspx中将使用查询字符串中的messageid获取完整消息的逻辑。