Execute code based on Yes No Message Box Button click using C# and VB.Net in ASP.Net

Last Reply on Mar 30, 2017 07:49 AM By AnandM

Posted on Mar 30, 2017 01:53 AM

hi;

I have a button that filled griedview and I have a 2. button that executing something .

now , I want to dialog box when clicked 2. button

if checkbox checked then start the dialox box otherwise throw alert mesaj

and if checxbo checked throw dialog box and if clicked yes button on dialog box then start process if clicked no button the process not start

Thanks in advance .

You are viewing reply posted by: AnandM on Mar 30, 2017 07:49 AM.
Posted on Mar 30, 2017 07:49 AM Modified on on Mar 30, 2017 07:49 AM

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" />
    &nbsp;&nbsp;&nbsp;&nbsp;
    <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");
    //Bind your Gridview with database records.
    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)
        {
            //Proceed with your further Process.
            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")
    'Bind your Gridview with database records.
    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
            'Proceed with your further Process.
            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