[問題] 請問一下關於asp.net postback與數字累加
最近碰一些問題, 想不清楚, 請問一下各位大大, 這是一個數字累加的頁面,
每點擊按鈕一次會加一, 我的程式碼如下
-- aspx --
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="0_Page_Load_1.aspx.cs"
Inherits="Sample_Ch01" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="" rel="nofollow">http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server"><title></title></head>
<body>
<form id="form1" runat="server">
<div>
點擊次數:<asp:Label ID="Label1" runat="server" Text="0">
</asp:Label>
<asp:Button ID="Button1" runat="server"
Text="Button_按下滑鼠,畫面的點擊次數就會累加"
onclick="Button1_Click" />
<div>
</form>
</body>
</html>
-- cs程式碼 --
public partial class Sample_Ch01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(Label1.Text) + 1;
Label1.Text = i.ToString();
}
}
碰到兩個問題一直想不通, 求解 ~
1. 關於postback, 為什麼當我click button控制項(Button1)後, 他會自動submit
到server原來的頁面, 這不是要javascript才做的到嗎,
例如onclick="form.submit()", 請問asp.net底層是怎麼做的啊怎麼運作的?
2. 請問為什麼我將Label1設定Label1EnableViewState : true, 我每點擊一次
button控制項(Button1), Label1他就會累加0,1,2,3...., 他是每次post的時
候有把目前label1.text的值送到server端嗎?
這些問題跟下面自動產生出的hidden有關聯嗎, 是裡面會帶有label1.text的值嗎?
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPzlw==" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" val
ue="A7E09E08" />
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/w
EdJdhKPjy" />
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.216.89.1
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1456217108.A.CB6.html
→
02/23 17:00, , 1F
02/23 17:00, 1F
→
02/23 17:01, , 2F
02/23 17:01, 2F
※ 編輯: herbacin (61.216.89.1), 02/23/2016 17:14:47
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章