2013-07-05 2 views
0

웹 양식이 예상대로 작동하지 않고 약간 엉망입니다. 양식에서 전화 번호가 데이터베이스에서 텍스트 상자로 채워집니다. 이 부분은 잘 작동합니다. 그런 다음 사용자는 텍스트 상자에서 값을 편집하고 텍스트 상자의 값으로 데이터베이스의 값을 업데이트하는 버튼을 누릅니다. 이것은 작동하지 않는 부분입니다.텍스트 상자 값을 가져 오지 못했습니다.

여기 내 코드입니다.

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.Net.Mail; 
using System.Net.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

public partial class contact_edit : System.Web.UI.Page 
{ 
    //sql connection here 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     string phoneNum = ""; 
     cn.Open(); 
     SqlCommand command = new SqlCommand("getContactInfo", cn); 
     command.CommandType = CommandType.StoredProcedure; 

     SqlDataReader myReader; 
     myReader = command.ExecuteReader(); 

     while (myReader.Read()) 
     { 
      phoneNum = myReader.GetString(3).ToString(); 
     } 

     cn.Close(); 

     //If I take this part out and don't populate textbox, 
     //my update works. If I leave it, it does not 
     phone.Text = phoneNum.ToString(); 

    } 
    protected void update_btn_Click(object sender, EventArgs e) 
    { 
     cn.Open(); 

     phone.Text = ""; 

     string updatedPhone = phone.Text; 

     string updateSQLString = "update contact set Phone_num = '" + updatedPhone + 
     "'"; 

     SqlCommand updateCommand = new SqlCommand(updateSQLString, cn); 
     updateCommand.ExecuteNonQuery(); 

     cn.Close(); 
     Response.Redirect("contact_edit.aspx"); 
} 

} 내 업데이트 메서드를 호출 버튼을 누르면

. 전화 텍스트 상자의 값을 가져 오려고 시도합니다. 그러나 폼이로드 된 후 텍스트 상자 값을 변경 한 다음 업데이트 단추를 누르면 폼이로드 될 때 원래 텍스트 상자에 지정된 값이 유지됩니다. 왜 이런 일이 일어나는 걸까요?

+2

포스트 백 포스트 백 포스트 백을! – Learner

+0

당신은 정말로 데이터베이스 호출을하고있는 Page_Load의 코드 특히 리팩토링해야합니다. 이것은'PostBack' 때문에'If (! IsPostBack) '를 나타내는 코드를 수행하지 않기 때문에 제 의견으로는 분리 된 메서드에 있어야합니다. – MethodMan

+0

페이지 포스트 백 때문일 수 있습니다. 이 기사를 읽는 것을 고려하십시오 : http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET – Learner

답변

0

주위 if (!Page.IsPostBack)을 넣어보십시오?

모든 포스트 백에서 값을로드하고 있기 때문에. IsPostBack -check에 랩 :

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     // only load the value on the first load 
    } 
} 
+0

예. 그것은 그것을 얻었다. 고마워요. – chuckw87

0

왜 이런 일이 귀하를 Page_Load 코드

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
     string phoneNum = ""; 
     cn.Open(); 
     SqlCommand command = new SqlCommand("getContactInfo", cn); 
     command.CommandType = CommandType.StoredProcedure; 

     SqlDataReader myReader; 
     myReader = command.ExecuteReader(); 

     while (myReader.Read()) 
     { 
      phoneNum = myReader.GetString(3).ToString(); 
     } 

     cn.Close(); 

     //If I take this part out and don't populate textbox, 
     //my update works. If I leave it, it does not 
     phone.Text = phoneNum.ToString(); 
    } 

    } 
+0

데이터베이스 스키마가 변경되면 어떻게됩니까?이 줄은 여기'phoneNum = myReader.GetString (3) .ToString();'실패합니다. 이름으로 인덱스 된 위치를 기준으로 필드를 잡는 이유가 무엇입니까? – MethodMan

+0

감사합니다. 이것은 제가 놓친 부분입니다 – chuckw87

+0

DJ KRAZE, 이것도 좋은 지적입니다. 나는 스키마가 변하는 것을 보지 못하지만 결코 알지 못합니다. 그리고 그것은 앞으로 많은 headeache를 구할 수 있습니다. – chuckw87

0
protected void Page_Load(object sender, EventArgs e) 
{ 

    if (!Page.IsPostBack) 
    { 
    //access data here 
    //assign value here 
    phone.Text = phoneNum.ToString(); 
    } 

} 
+0

고마워요. 이것은 내가 누락 된 것이다. – chuckw87

관련 문제