Hi alya14,
I have created a sample which full fill your requriement you need to modify the code according to your need.
HTML
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkboxid" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Query_Click" Text="QUERY" />
<asp:Button ID="Button2" OnClick="SendMail" runat="server" Text="SEND" />
<br />
</div>
C#
protected void Query_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
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");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void SendMail(object sender, EventArgs e)
{
System.Web.UI.WebControls.CheckBox checkbox = new System.Web.UI.WebControls.CheckBox();
foreach (GridViewRow row in GridView1.Rows)
{
System.Web.UI.WebControls.CheckBox chkboxid = row.FindControl("chkboxid") as System.Web.UI.WebControls.CheckBox;
if (chkboxid.Checked)
{
checkbox = chkboxid;
break;
}
}
if (checkbox.Checked)
{
DialogResult result = MessageBox.Show("Do you Want to Start the service", "Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
ClientScript.RegisterStartupScript(this.GetType(), "Start", "alert('Your Service Started')", true);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "Stop", "alert('Your Service Stopped ')", true);
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "Error", "alert('Please Select AtLeast one checkbox from GridView')", true);
}
}
VB.Net
Protected Sub Query_Click(sender As Object, e As EventArgs)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
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")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub SendMail(sender As Object, e As EventArgs)
Dim checkbox As New System.Web.UI.WebControls.CheckBox()
For Each row As GridViewRow In GridView1.Rows
Dim chkboxid As System.Web.UI.WebControls.CheckBox = TryCast(row.FindControl("chkboxid"), System.Web.UI.WebControls.CheckBox)
If chkboxid.Checked Then
checkbox = chkboxid
Exit For
End If
Next
If checkbox.Checked Then
Dim result As DialogResult = MessageBox.Show("Do you Want to Start the service", "Confirmation", MessageBoxButtons.YesNo)
If result = DialogResult.Yes Then
ClientScript.RegisterStartupScript(Me.[GetType](), "Start", "alert('Your Service Started')", True)
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "Stop", "alert('Your Service Stopped ')", True)
End If
Else
ClientScript.RegisterStartupScript(Me.[GetType](), "Error", "alert('Please Select AtLeast one checkbox from GridView')", True)
End If
End Sub
Screenshot