Get controls value from FormView on Button Click using C# and VB.Net in ASP.Net

tanweeruddinbeeharry
 
on Oct 11, 2021 05:01 AM
Sample_471495.zip
189 Views

The formView consists of 4 labels. How to find control of each label in the formview on button onclick?

<asp:FormView ID="FV_quiz_summary_Stu_Details" runat="server">
    <ItemTemplate>
        <asp:Label ID="Lbl_TimeTaken" runat="server" Text='  <%#Eval("date_taken","{0:MMM-dd-yyyy}")%>' />
        <asp:Label ID="lblStu_score" runat="server" Text=' <%#Eval("stu_score")%>' />
    </ItemTemplate>
</asp:FormView>
<div class="row">
    <div class="col-md-12">
        <br />
        <asp:Button ID="Btn_Continue" CssClass="btn btn-success" runat="server" Text="Click to Continue" />
    </div>
</div>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 14, 2021 02:22 AM

Hiii tanweeruddinbeeharry

  Kindly follow below Code.

HTML

<asp:FormView ID="FV_quiz_summary_Stu_Details" runat="server">
    <ItemTemplate>
        <asp:Label ID="Lbl_TimeTaken" runat="server" Text='<%#Eval("date_taken","{0:MMM-dd-yyyy}")%>' />
        <asp:Label ID="lblStu_score" runat="server" Text='<%#Eval("stu_score")%>' />
    </ItemTemplate>
</asp:FormView>
<div class="row">
    <div class="col-md-12">
        <br />
        <asp:Button ID="Btn_Continue" CssClass="btn btn-success" runat="server" Text="Click to Continue" OnClick="OnContinue"/>
    </div>
</div>

Namespace

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

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

private void BindGrid()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("date_taken", typeof (DateTime)),
                                            new DataColumn("stu_score", typeof (int))});
    dt.Rows.Add("OCT-14-2021 ", 80);
    dt.Rows.Add("OCT-15-2021 ", 81);
    dt.Rows.Add("OCT-16-2021 ", 82);
    dt.Rows.Add("OCT-17-2021 ", 83);
    dt.Rows.Add("OCT-18-2021 ", 84);
    dt.Rows.Add("OCT-19-2021 ", 85);
    FV_quiz_summary_Stu_Details.DataSource = dt;
    FV_quiz_summary_Stu_Details.DataBind();
}

protected void OnContinue(object sender, EventArgs e)
{
    string time = (FV_quiz_summary_Stu_Details.FindControl("Lbl_TimeTaken") as Label).Text;
    string score = (FV_quiz_summary_Stu_Details.FindControl("lblStu_score") as Label).Text;
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Date: " + time + "\\nScore: " + score + "');", true);
}

VB.Net

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

Private Sub BindGrid()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(1) {New DataColumn("date_taken", GetType(DateTime)), New DataColumn("stu_score", GetType(Integer))})
    dt.Rows.Add("OCT-14-2021 ", 80)
    dt.Rows.Add("OCT-15-2021 ", 81)
    dt.Rows.Add("OCT-16-2021 ", 82)
    dt.Rows.Add("OCT-17-2021 ", 83)
    dt.Rows.Add("OCT-18-2021 ", 84)
    dt.Rows.Add("OCT-19-2021 ", 85)
    FV_quiz_summary_Stu_Details.DataSource = dt
    FV_quiz_summary_Stu_Details.DataBind()
End Sub

Protected Sub OnContinue(ByVal sender As Object, ByVal e As EventArgs)
    Dim time As String = (TryCast(FV_quiz_summary_Stu_Details.FindControl("Lbl_TimeTaken"), Label)).Text
    Dim score As String = (TryCast(FV_quiz_summary_Stu_Details.FindControl("lblStu_score"), Label)).Text
    ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Date: " & time & "\nScore: " & score & "');", True)
End Sub

Screenshot