Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a link button inside datalist, i need to change the name of link if user clicks that .
ASP.NET
      <asp:DataList ID="dlist" runat="server"
          onselectedindexchanged="dlist_SelectedIndexChanged" Width="192px"
          Height="80px" onitemcommand="dlist_ItemCommand">

          <ItemTemplate>

<li> <asp:LinkButton id="OpenClose" runat="server" OnClick="OpenClose_Click" AutoPostBack="true" Text="Close" CommandName="lnkreset"></asp:LinkButton></li>
    </ItemTemplate>

      </asp:DataList>


What I have tried:

protected void OpenClose_Click(object sender, EventArgs e)
{
if (OpenClose.Text == "Close")
{
OpenClose.Text = "Open";
}
else
{
OpenClose.Text = "Close";
}
}
Posted
Updated 7-Apr-16 0:39am

Take out the
OnClick="OpenClose_Click"
from your LinkButton definition, and instead do what you need within the DataList's onitemcommand function.
Within that, check for
if )e.coomandName = "lnkreset")
and then find the LinkButton control (e.Item.FindControl) and use that to change the Text property.
 
Share this answer
 
Comments
Member 12119075 7-Apr-16 6:01am    
Am not able to change link button text separately can u help plz .
Wombaticus 7-Apr-16 6:40am    
see below
ASP.NET
<%@ Page Language="VB" debug="true" ValidateRequest="false"  %>

<script runat="server">
    
    Friend Class clsUser

        Private _ID As Integer
        Public Property ID() As Integer
            Set(ByVal value As Integer)
                _ID = value
            End Set
            Get
                Return _ID
            End Get
        End Property

        Public Sub New()
            _ID = 0
        End Sub

    End Class
    
    Private Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)
        If Not IsPostBack Then
            BindList()
        End If
    End Sub
    
    Private Sub BindList()
        Dim coll As New Collection
        Dim cls As clsUser
        Dim i As Integer
        For i = 1 To 9
            cls = New clsUser
            cls.ID = i
            coll.Add(cls)
        Next
        dlist.DataSource = coll
        dlist.DataBind()
    End Sub
    

    Private Sub dlist_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
        Select Case e.CommandName.ToLower
            Case "lnkreset"
                Dim SelectedID As Integer = CInt(dlist.DataKeys(e.Item.ItemIndex))
                Dim lb As LinkButton = CType(e.Item.FindControl("OpenClose"), LinkButton)
                If lb.Text = "Close" Then
                    lb.Text = "Open"
                Else
                    lb.Text = "Close"
                End If
            Case Else
                '
        End Select
    End Sub
    
</script>
<html>
<head>
    <title></title> 
</head>
<body>
    <form id="Form1" runat="server">  
       <p><asp:Literal runat="server" ID="litX" EnableViewState="false"></asp:Literal></p>
       <asp:DataList ID="dlist" runat="server" DataKeyField="ID" Width="192px" Height="80px" OnItemCommand="dlist_ItemCommand">
          <ItemTemplate>
            <li>
               <asp:LinkButton id="OpenClose" runat="server" Text="Close" CommandName="lnkreset"></asp:LinkButton>
            </li>
          </ItemTemplate>
      </asp:DataList>	
    </form>
</body>
</html>
 
Share this answer
 
Comments
Member 12119075 7-Apr-16 6:41am    
sorry sir i need to solve in asp.net
Wombaticus 7-Apr-16 6:44am    
That is asp.net! But it's using VB, not C#. It's not hard to convert, you'll have to do that. There are online converters if you get stuck. Google "vb to c#"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900