Live Chat Icon For mobile
Live Chat Icon

How to hide and show data in the DataList

Platform: ASP.NET| Category: DataList

<asp:Datalist runat='server' OnItemCommand ='ItemCmd' id='Datalist1' Font-Size='10pt' Font-Name='Verdana'>
	<ItemTemplate>
		<asp:LinkButton style='text-decoration:none' runat='server' id='btnDetails' Text='+' CommandName='Show' Font-Name='Verdana' />
		<b>
		<%# DataBinder.Eval(Container.DataItem, 'employeeid') %>
		</b>
		<br />
		<asp:label id='lblEID' Visible='False' runat='Server' Text=’<%# DataBinder.Eval(Container.DataItem, 'Employeeid') %>’ />
		<asp:DataGrid runat='server' id='Datagrid1' Font-Name='Verdana' Font-Size='10pt' HorizontalAlign='Center'
				Visible='False' Width='85%'>
	</ItemTemplate>
</asp:Datalist>

VB.NET


Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
            ’Bind data to DataList
End If
End Sub

Private Sub Datalist1_ItemCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) 
If e.CommandName = 'Show' Then
            Dim EmpIDlabel As Label = e.Item.FindControl('lblEID')
            Dim strEmpID As String = EmpIDlabel.Text
            CType(e.Item.FindControl('Datagrid1'), DataGrid).DataSource = GetEmpDetails(strEmpID)
            CType(e.Item.FindControl('Datagrid1'), DataGrid).DataBind()
            CType(e.Item.FindControl('Datagrid1'), DataGrid).Visible = True
            CType(e.Item.FindControl('btnDetails'), LinkButton).Text = '-'
            CType(e.Item.FindControl('btnDetails'), LinkButton).CommandName = 'Hide'
End If
If e.CommandName = 'Hide' Then
            CType(e.Item.FindControl('Datagrid1'), DataGrid).Visible = False
            CType(e.Item.FindControl('btnDetails'), LinkButton).Text = '+'
            CType(e.Item.FindControl('btnDetails'), LinkButton).CommandName = 'Show'
End If
End Sub

Function GetEmpDetails(ByVal Employeeid As String) As SqlDataReader
        Const strConnString As String = 'server=localhost;uid=sa;pwd=;database=northwind'
        Dim objConn As New SqlConnection(strConnString)
        Dim strSQL As String
        strSQL = 'SELECT FirstName , LastName ,Title, Address FROM Employees ' & _
                 'WHERE Employeeid = @Employeeid'
        Dim objCmd As New SqlCommand(strSQL, objConn)
        Dim paramEmployeeid As SqlParameter
        paramEmployeeid = New SqlParameter('@Employeeid', SqlDbType.VarChar, 10)
        paramEmployeeid.Value = Employeeid
        objCmd.Parameters.Add(paramEmployeeid)
        objConn.Open()   ’Open the connection
        Return objCmd.ExecuteReader(CommandBehavior.CloseConnection)
End Function

C#


private void Page_Load(object sender, System.EventArgs e)
{
	// Put user code to initialize the page here
	if(!Page.IsPostBack )
	{
		//Bind data to Datalist1
	}
}

protected void ItemCmd(object source  ,  System.Web.UI.WebControls.DataListCommandEventArgs e ) 
{ 
	if( e.CommandName == 'Show' )
	{
		Label EmpIDlabel =(Label) e.Item.FindControl('lblEID');
		string strEmpID = EmpIDlabel.Text;
		((DataGrid)e.Item.FindControl('Datagrid1')).DataSource = GetEmpDetails(strEmpID);
		((DataGrid)e.Item.FindControl('Datagrid1')).DataBind();
		((DataGrid)e.Item.FindControl('Datagrid1')).Visible = true;
		((LinkButton)e.Item.FindControl('btnDetails')).Text = '-';
		((LinkButton)e.Item.FindControl('btnDetails')).CommandName = 'Hide';
	}
	if( e.CommandName == 'Hide' )
	{
		((DataGrid)e.Item.FindControl('Datagrid1')).Visible = false;
		((LinkButton )e.Item.FindControl('btnDetails')).Text = '+';
		((LinkButton)e.Item.FindControl('btnDetails')).CommandName = 'Show';
	}
}

protected  SqlDataReader GetEmpDetails(string Employeeid )
{
	string strConnString  = 'server=localhost;uid=sa;pwd=;database=northwind';
	SqlConnection objConn = new SqlConnection(strConnString);
	string strSQL   ;
	strSQL = 'SELECT FirstName , LastName ,Title, Address FROM Employees WhERE Employeeid = @Employeeid';
	SqlCommand objCmd = new SqlCommand(strSQL, objConn);
	SqlParameter paramEmployeeid ; 
	paramEmployeeid = new SqlParameter('@Employeeid', SqlDbType.VarChar, 10);
	paramEmployeeid.Value = Employeeid;
	objCmd.Parameters.Add(paramEmployeeid);
	objConn.Open()  ;// Open the connection;
	return objCmd.ExecuteReader(CommandBehavior.CloseConnection);
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.