Click here to Skip to main content
15,890,932 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi friend I am newer in ASP.NET 3.5, I want to show the value of the variable in Alert box so please give me suggestion for that. Here is my code.

public partial class WebForm1 : System.Web.UI.Page
    {
        int sum3;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                DataTable dTable = new DataTable("mainTable");

                DataColumn Name = new DataColumn("Name");
                dTable.Columns.Add(Name);

                DataColumn Marks = new DataColumn("Marks");
                dTable.Columns.Add(Marks);

                DataColumn Marks2 = new DataColumn("Marks2");
                dTable.Columns.Add(Marks2);

                DataColumn Total = new DataColumn("Total");
                dTable.Columns.Add(Total);

                int sum = 0;
                int sum2 = 0;

                DataRow dRow = null;
                for (int i = 0; i < 5; i++)
                {
                    dRow = dTable.NewRow();
                    dRow["Name"] = i + " - Sagar";
                    dRow["Marks"] = i;
                    sum += i;
                    dRow["Marks2"] = i + 5;
                    sum2 += i + 5;
                    dRow["Total"] = i + i + 5;
                    sum3 += i + i + 5;
                    dTable.Rows.Add(dRow);
                }

                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "showAlert(" + sum3.ToString() + ");", true);

                //dRow = dTable.NewRow();
                //dRow["Name"] = "Total";
                //dRow["Marks"] = sum;
                //dRow["Marks2"] = sum2;
                //dRow["Total"] = sum3;
                //dTable.Rows.Add(dRow);

                aspDataGrid.DataSource = dTable;
                aspDataGrid.DataBind();
            }
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            marks1.Text = "Total Marks : " + sum3.ToString();
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "showAlert(" + sum3.ToString() + ");", true);
        }
}



Thanks in advance.
Posted

You can try This Using Session Like...

In Your Page_Load Event write
Session["sum3"]=sum3 After For Loop And Then You Can write This Code in
Button Submit Button

Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "showAlert(" + Session["sum3"] + ");", true);
 
Share this answer
 
Try this link :

A Simple ASP.NET Server Control: Message Box & Confirmation Box

I hope it will help you. :)
 
Share this answer
 
Comments
Sagar Rawal 1-Dec-11 1:32am    
Thanks for the suggestion.

there is also a simple way to do this for showing the value of sum3 variable in alert box.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900