Hi democloud
You need to pass unique key for displaying all messages, So pass unique key in ScriptManager.RegisterStartupScript.
Refer below code and check according to you.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = BindGrid();
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
private DataTable BindGrid()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
return dt;
}
}
}
}
}
protected void Insert(object sender, EventArgs e)
{
DataTable dt = BindGrid();
if (dt.Rows.Count > 0)
{
int i = 1;
foreach (DataRow row in dt.Rows)
{
string key = "SweetAlert" + i.ToString();
if (row["Country"].ToString() == "India")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), key, "alert('Available ');", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), key, "alert('Not available ');", true);
}
i++;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = BindGrid()
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End If
End Sub
Private Function BindGrid() As DataTable
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = BindGrid()
If dt.Rows.Count > 0 Then
Dim i As Integer = 1
For Each row As DataRow In dt.Rows
Dim key As String = "SweetAlert" & i.ToString()
If row("Country").ToString() = "India" Then
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), key, "alert('Available ');", True)
Else
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), key, "alert('Not available ');", True)
End If
i += 1
Next
End If
End Sub
Screenshot