Refresh single Column in ASP.Net DataList on Button Click using C# and VB.Net

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Any way i can refresh a column data in datalist using refresh button click?

Posted 7 months ago Modified on 7 months ago

Hi micah,

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

HTML

<asp:ScriptManager runat="server" />
<asp:DataList ID="dlDetails" runat="server">
    <ItemTemplate>
        <table>
            <thead>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Country
                    </th>
                    <th>
                        Time
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <asp:Label ID="lblId" Text='<%# Eval("Id")%>' runat="server" />
                    </td>
                    <td>
                        <asp:Label ID="lblName" Text='<%# Eval("Name")%>' runat="server" />
                    </td>
                    <td>
                        <asp:Label ID="lblCountry" Text='<%# Eval("Country")%>' runat="server" />
                    </td>
                    <td>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                            <ContentTemplate>
                                <asp:Label ID="lblTime" Text='<%# DateTime.Now%>' runat="server" />
                                <asp:Button ID="Button1" Text="Refresh" runat="server" OnClick="Refresh" />
                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </td>
                </tr>
            </tbody>
        </table>
    </ItemTemplate>
</asp:DataList> 

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindDataList();
    }
}

private void BindDataList()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { 
                        new DataColumn("Id", typeof(int)),
                        new DataColumn("Name", typeof(string)),
                        new DataColumn("Country",typeof(string)) });
    dt.Rows.Add(1, "John Hammond", "United States");
    dt.Rows.Add(2, "Mudassar Khan", "India");
    dt.Rows.Add(3, "Suzanne Mathews", "France");
    dt.Rows.Add(4, "Robert Schidner", "Russia");
    dlDetails.DataSource = dt;
    dlDetails.DataBind();
}

protected void Refresh(object sender, EventArgs e)
{
    (((sender as Button).NamingContainer as DataListItem).FindControl("lblTime") as Label).Text = DateTime.Now.ToString();
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        BindDataList()
    End If
End Sub

Private Sub BindDataList()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {
                        New DataColumn("Id", GetType(Integer)),
                        New DataColumn("Name", GetType(String)),
                        New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, "John Hammond", "United States")
    dt.Rows.Add(2, "Mudassar Khan", "India")
    dt.Rows.Add(3, "Suzanne Mathews", "France")
    dt.Rows.Add(4, "Robert Schidner", "Russia")
    dlDetails.DataSource = dt
    dlDetails.DataBind()
End Sub

Protected Sub Refresh(ByVal sender As Object, ByVal e As EventArgs)
    TryCast((TryCast((TryCast(sender, Button)).NamingContainer, DataListItem)).FindControl("lblTime"), Label).Text = DateTime.Now.ToString()
End Sub

Screenshot