2011-11-24 104 views
0

我的情景,如何在提交後保留已編輯的文本框值?

我重新加載頁面紐約與以前的數據,這是適合我的login.Now我嘗試編輯文本框和更新。它要麼更新,要麼犯規保留在文本框中編輯值。

objConn.Open() 
Dim myControl As TextBox = FindControl("txtName") 
Dim cmd As New SqlCommand("sp_UpdateNewmember", objConn) 
cmd.CommandType = CommandType.StoredProcedure 
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username 
cmd.Parameters.Add(New SqlParameter("@FirstName", DirectCast(myControl, TextBox).Text)) 
cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = txtlastname.Text 

任何人都可以幫忙。

回答

1

當第一次請求頁面時,您必須從table中獲取一行併爲控件屬性指定值。

protect Sub Page_Load() 
    IF Not IsPostBack Then 
    //Retrieve a record 
    End If 
End sum 

更新記錄,代碼應該是這樣的:

Dim myControl As TextBox = FindControl("txtName") 

Dim cmd As New SqlCommand("sp_UpdateNewmember", objConn) 
cmd.CommandType = CommandType.StoredProcedure 

cmd.Parameters.Add("@UserName", SqlDbType.VarChar,30).Value = Username 
cmd.Parameters.Add("@FirstName",SqlDbType.VarChar,30).Value= myControl.Text 
cmd.Parameters.Add("@LastName", SqlDbType.VarChar,30).Value = txtlastname.Text 
objConn.Open() 
cmd.ExecuteNonQuery() 
objConn.Close() 
+0

對不起,令人不安的代碼..現在,當我登錄到我的網頁,我看到我在各自領域的記錄。當我編輯我的領域和更新我的形式它帶來相同的值在文本框update.I檢查通過breakpoint.How可以使這個錯誤清除。 – Sammy

+0

@Sammy - 您必須在IsPostBack塊中寫入page_load代碼。 – adatapost

+0

非常感謝您的幫助。它工作得很好,謝謝。 – Sammy

0

請寫出這裏面

if (!ispostback) 
    { 
     //code 
    }