Hi makumbi,
You need find for closest tr not div.
Please refer below sample.
SQL
CREATE TABLE [TextToSpeech]
(
[Account] VARCHAR(20),
[Name] VARCHAR(20),
[RoomNo] VARCHAR(20)
)
INSERT INTO [TextToSpeech] VALUES('05-00009','NASSANGA FLAVIA',1)
INSERT INTO [TextToSpeech] VALUES('05-000012','KILUNGI CHRISTINE',2)
INSERT INTO [TextToSpeech] VALUES('05-000010','AKATURINDA MACKLINE','None')
SELECT * FROM [TextToSpeech]
DROP TABLE [TextToSpeech]
HTML
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Account" HeaderText="Account" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
<asp:LinkButton ID="lblVoice" runat="server" CssClass="voice"><i class="fa fa-volume-down"></i></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RoomNo">
<ItemTemplate>
<asp:Label ID="lblRoomNo" runat="server" Text='<%# Eval("RoomNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript">
$(function () {
if (!('speechSynthesis' in window)) {
alert("You don't have speechSynthesis");
}
$('[id*=gvDetails]').on('click', function () {
debugger;
var term = $(this).closest('tr').find('[id*=lblName]').html();
var termroom = $(this).closest('tr').find('[id*=lblRoomNo]').html();
var message = "Name:" + term;
message += "\nRoom No:" + termroom;
var voices = window.speechSynthesis.getVoices();
var speech = new SpeechSynthesisUtterance();
speech.voice = voices[0];
speech.rate = 1;
speech.pitch = 1;
alert(message);
speech.text = message;
speechSynthesis.speak(speech);
return false;
});
});
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM TextToSpeech", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
}
}
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 constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM TextToSpeech", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvDetails.DataSource = dt
gvDetails.DataBind()
End Using
End Using
End Using
End Using
End Sub
Screenshot