我想在Alert框中显示变量的值,所以请给我建议。这是我的代码。
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);
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);
}
}
提前致谢。
答案 0 :(得分:3)
您可以尝试使用会话... ...
在你的Page_Load事件中写
会话[“sum3”] =在for循环之后的sum3然后你可以写入此代码
按钮提交按钮
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "showAlert(" + Session["sum3"] + ");", true);
答案 1 :(得分:1)
正确的行是:
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert(" + sum3.ToString() + ");", true);
在btn_Submit_Click和Page_Load中替换它。它会工作!