[英]Why does my float variable is not taking the correct value from the textbox?
[英]Why does my save use the initial value of my TextBox and not the entered value?
我的網站上有一個文本框:
<asp:TextBox ID="Latitude" runat="server" ClientIDMode="Static" ></asp:TextBox>
在頁面加載時,我用數據庫中的東西填充該文本框:
protected void Page_Load(object sender, EventArgs e)
{
Latitude.Text = thisPlace.Latitude;
}
當我想在該文本框中使用新值更新我的數據庫時,它仍然使用放在頁面加載中的數據庫更新數據庫:
protected void Save_Click(object sender, EventArgs e)
{
setCoordinates(Latitude.Text);
}
如何確保setCoordinates()
從文本框中檢索新值,而不是從Latitude.Text = thisPlace.Latitude;
數據庫中的初始值Latitude.Text = thisPlace.Latitude;
?
我認為這是因為PostBack
如果您在某個按鈕的單擊事件文本框上調用setCoordinates()
,則新值將丟失。 如果這是正確的改變Page_Load
就像這樣
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Latitude.Text = thisPlace.Latitude;
}
}
這是因為Page_Load
事件在調用方法setCoordinates
之前發生。 這意味着Latitude.Text值與之前相同。
您應該更改加載函數,以便它不總是設置文本框的初始值。
通過使用!Page.IsPostBack
更改page_load事件,給出初始值的唯一時間是頁面首次加載。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Latitude.Text = thisPlace.Latitude;
}
}
每次加載頁面時都會執行Page_Load
。 添加IsPostBack
檢查以僅在第一頁加載時重置文本:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Latitude.Text = thisPlace.Latitude;
}
}
檢查頁面是否處於回發狀態,否則將在保存之前替換該值
If(!IsPostBack){
Latitude.Text = thisPlace.Latitude;
}
您需要從請求中獲取信息,而不是使用以下屬性:
var theValue = this.Context.Request[this.myTextBox.ClientID];
如果再次加載初始值,則會發生這種情況。
if (!IsPostBack)
{
//call the function to load initial data into controls....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.