Visible or Hide Label and HyperLink Based on Text length on OnItemDataBound event in DataList in ASP.Net using C# or VB.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

This example below only shows detial hyperlink but it does not limit the number of characters in the text, how can i make that happen. 

 

<asp:DataList ID="Getpost" runat="server" OnItemDataBound="OnItemDataBound" BorderStyle="Solid" BorderWidth="1">
    <ItemTemplate>
        <asp:Label ID="Label96" Text='<%#Eval("content")%>' runat="server" CssClass="" />
        <asp:HyperLink ID="lnkRedirect" NavigateUrl='<%# string.Format("~/Details.aspx?Id={0}",Eval("Id")) %>'
            runat="server" Text="Details..." />
        <hr />
    </ItemTemplate>
</asp:DataList>

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new System.Data.DataColumn[2] {
            new System.Data.DataColumn("Id"), new System.Data.DataColumn("Content") });
        dt.Rows.Add(1, "How do i make a long text in datalist show detail link");
        dt.Rows.Add(2, "I want to make a text that is more than 200 characters display Detail link which when clicked redirects to another page can you give example is it like code behind or jquery based on the text length show hide the controls and on hyperlink click redirect to destination page");
        dt.Rows.Add(3, "Test");
        Getpost.DataSource = dt;
        Getpost.DataBind();
    }
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if ((e.Item.FindControl("Label96") as Label).Text.Length > 200)
        {
            (e.Item.FindControl("Label96") as Label).Visible = false;
            (e.Item.FindControl("lnkRedirect") as HyperLink).Visible = true;
        }
        else
        {
            (e.Item.FindControl("Label96") as Label).Visible = true;
            (e.Item.FindControl("lnkRedirect") as HyperLink).Visible = false;
        }
    }
}

see example

https://www.aspforums.net/Threads/113177/Show-Hide-Label-and-HyperLink-inside-ASPNet-DataList-based-on-the-Text-length-using-C-and-VBNet/

 

 

this one limites text,so how do i modify it ?

 

 <asp:Label ID="Label11" runat="server" Text='<%# Eval("Comments").ToString().Length >180 ? Eval("Comments").ToString().Substring(0,180)+"...(Read More)" : Eval("Comments").ToString()%>' Font-Bold="False" Font-Strikeout="False" ForeColor="#333333" CssClass="" Font-Names="Arial" />

 

Posted 6 months ago

Hi micah,

Check this example. Now please take its reference and correct your code.

HTML

<asp:DataList ID="Getpost" runat="server" OnItemDataBound="OnItemDataBound" BorderStyle="Solid"
    BorderWidth="1">
    <ItemTemplate>
        <asp:Label ID="Label96" Text='<%#Eval("content")%>' runat="server" CssClass="" />
        <asp:HyperLink ID="lnkRedirect" NavigateUrl='<%# string.Format("~/Details.aspx?Id={0}",Eval("Id")) %>'
            runat="server" Text='<%# (Eval("Content").ToString().Length > 100 ? Eval("Content").ToString().Substring(0,100)+"..." : Eval("Content").ToString().Substring(0,Eval("Content").ToString().Length)) %>' />
        <hr />
    </ItemTemplate>
</asp:DataList>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new System.Data.DataColumn[2] { 
        new System.Data.DataColumn("Id"), new System.Data.DataColumn("Content") });
        dt.Rows.Add(1, "How do i make a long text in datalist show detail link");
        dt.Rows.Add(2, "I want to make a text that is more than 200 characters display Detail link which when clicked redirects to another page can you give example is it like code behind or jquery based on the text length show hide the controls and on hyperlink click redirect to destination page");
        dt.Rows.Add(3, "Test");
        Getpost.DataSource = dt;
        Getpost.DataBind();
    }
}
protected void OnItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if ((e.Item.FindControl("Label96") as Label).Text.Length > 200)
        {
            (e.Item.FindControl("Label96") as Label).Visible = false;
            (e.Item.FindControl("lnkRedirect") as HyperLink).Visible = true;
        }
        else
        {
            (e.Item.FindControl("Label96") as Label).Visible = true;
            (e.Item.FindControl("lnkRedirect") as HyperLink).Visible = false;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As System.Data.DataTable = New System.Data.DataTable()
        dt.Columns.AddRange(New System.Data.DataColumn(1) {New System.Data.DataColumn("Id"), New System.Data.DataColumn("Content")})
        dt.Rows.Add(1, "How do i make a long text in datalist show detail link")
        dt.Rows.Add(2, "I want to make a text that is more than 200 characters display Detail link which when clicked redirects to another page can you give example is it like code behind or jquery based on the text length show hide the controls and on hyperlink click redirect to destination page")
        dt.Rows.Add(3, "Test")
        Getpost.DataSource = dt
        Getpost.DataBind()
    End If
End Sub

Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        If (TryCast(e.Item.FindControl("Label96"), Label)).Text.Length > 200 Then
            TryCast(e.Item.FindControl("Label96"), Label).Visible = False
            TryCast(e.Item.FindControl("lnkRedirect"), HyperLink).Visible = True
        Else
            TryCast(e.Item.FindControl("Label96"), Label).Visible = True
            TryCast(e.Item.FindControl("lnkRedirect"), HyperLink).Visible = False
        End If
    End If
End Sub

Screenshot