2013-10-10 42 views
0

我遇到按鈕單擊事件併發回問題。我有一個包含一些文本框和一些下拉列表的頁面。我從數據庫中填充這些文本框和ddls。我也有2個按鈕。其中之一是使用文本框和下拉列表中已更改的數據更新數據庫。第二個按鈕顯示附加數據,具體取決於其中一個下拉列表中的值。我的問題是,當我點擊更新按鈕時,數據庫被更新,文本框和ddls中的數據被更改,但是當我進入地址選項卡並按Enter時,我得到舊數據(在數據庫中,所有內容都被更改爲新值)。我可以添加方法Postback vs Button_Click

如果(的IsPostBack)

和數據將永遠是新鮮的,但在這種情況下,我將無法在下拉列表中的一個,顯示額外的數據變化值(自動回帖會將數據加載到這個ddl中)。有沒有解決這個問題的方法?如果我的描述不清楚,請告訴我。

EDIT1增加C#代碼

public partial class EditStaff : System.Web.UI.Page 
{ 
    Methods methods = new Methods(); 
    IPrincipal p = HttpContext.Current.User; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    string soeid = Convert.ToString(Request["soeid"]); 
    DataSet dsUserDetails = new DataSet(); 
    DataTable dtUserDetails = new DataTable(); 
    DataSet dsDDLs = new DataSet(); 

    if (!IsPostBack) 
    { 
     GetDDLsItems(); 
     FillFields(); 
    } 
    else 
    { 
     //FillFields(); 
    } 
} 

protected void btnUpdate_Click(object sender, EventArgs e) 
{ 
    string update_error = ""; 
    string SOEID = txtSOEID.Text; 
    string firstName = txtFirstName.Text; 
    string lastName = txtLastName.Text; 
    string email = txtEmail.Text.Trim(); 
    int remsCode = Convert.ToInt32(ddlREMS.SelectedItem.ToString()); 
    int active = Convert.ToInt32(ddlActive.SelectedValue); 
    int isGVO = Convert.ToInt32(ddlIsGVO.SelectedValue); 
    int gvoTeamID = Convert.ToInt32(ddlGVOTeams.SelectedValue); 
    int profileID = Convert.ToInt32(ddlProfiles.SelectedValue); 
    int isSOW = Convert.ToInt16(ddlIsSOW.SelectedValue); 
    int headcount = Convert.ToInt32(ddlHeadcount.SelectedValue); 
    string updater_domain = p.Identity.Name.ToString(); 
    string updater = ""; 
    int index = updater_domain.IndexOf("\\"); 
    int email_at_index = email.IndexOf("@"); 

    if (index != -1) 
    { 
     updater = updater_domain.Substring(index + 1, 7); 
    } 
    else 
    { 
     updater = updater_domain; 
    } 

    if (firstName.Length < 2) 
    { 
     update_error = "First Name should have at least 2 characters. "; 
     lblStatus.Text = update_error; 
     lblStatus.ForeColor = System.Drawing.Color.Red; 
     lblStatus.Visible = true; 
    } 
    else if (lastName.Length < 2) 
    { 
     update_error = update_error + "Last Name should have at least 2 characters. "; 
     lblStatus.Text = update_error; 
     lblStatus.ForeColor = System.Drawing.Color.Red; 
     lblStatus.Visible = true; 
    } 
    else if (email_at_index == -1 && email.Length < 5) 
    { 
     update_error = update_error + "Invalid email address."; 
     lblStatus.Text = update_error; 
     lblStatus.ForeColor = System.Drawing.Color.Red; 
     lblStatus.Visible = true; 
    } 
    else 
    { 
     // create ConnectDatabase object to get acces to its methods 
     ConnectDatabase connectDB = new ConnectDatabase(); 
     IDBManager dbManager = connectDB.ConnectDB(); 
     DataSet ds = new DataSet(); 

     try 
     { 
      dbManager.Open(); 
      dbManager.CreateParameters(13); 
      dbManager.AddParameters(0, "@SOEID", SOEID); 
      dbManager.AddParameters(1, "@firstName", firstName); 
      dbManager.AddParameters(2, "@LastName", lastName); 
      dbManager.AddParameters(3, "@Email", email); 
      dbManager.AddParameters(4, "@REMSCode", remsCode); 
      dbManager.AddParameters(5, "@Active", active); 
      dbManager.AddParameters(6, "@IsGVO", isGVO); 
      dbManager.AddParameters(7, "@gvoTeamID", gvoTeamID); 
      dbManager.AddParameters(8, "@profileID", profileID); 
      dbManager.AddParameters(9, "@isSOW", isSOW); 
      dbManager.AddParameters(10, "@headcount", headcount); 
      dbManager.AddParameters(11, "@lastUpdatedBy", updater); 
      dbManager.AddParameters(12, "@status", active); 
      dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "sp_update_user"); 

     } 
     catch (Exception error) 
     { 
      HttpContext.Current.Response.Write(error.ToString()); 
     } 
     finally 
     { 
      dbManager.Close(); 
      dbManager.Dispose(); 
      lblStatus.Visible = true; 
      lblStatus.Text = "User data updated successfully."; 
      lblStatus.ForeColor = System.Drawing.Color.Green; 
      FillFields(); 
     } 
    } 
} 

protected void btnCancel_Click(object sender, EventArgs e) 
{ 
    FillFields(); 
    gvREMSDetails.Visible = false; 
} 

private void FillFields() 
{ 
    string soeid = Convert.ToString(Request["soeid"]); 
    DataSet dsUserDetails = new DataSet(); 
    DataTable dtUserDetails = new DataTable(); 
    DataSet dsDDLs = new DataSet(); 

    dsUserDetails = GetUserDetails(soeid); 
    dtUserDetails = dsUserDetails.Tables[0]; 
    string gvoTeam = dtUserDetails.Rows[0].ItemArray[8].ToString(); 
    string profile = dtUserDetails.Rows[0].ItemArray[10].ToString(); 
    string remsCode = dtUserDetails.Rows[0].ItemArray[4].ToString(); 

    txtSOEID.Text = dtUserDetails.Rows[0].ItemArray[0].ToString(); 
    txtFirstName.Text = dtUserDetails.Rows[0].ItemArray[1].ToString(); 
    txtLastName.Text = dtUserDetails.Rows[0].ItemArray[2].ToString(); 
    txtEmail.Text = dtUserDetails.Rows[0].ItemArray[3].ToString(); 
    ddlREMS.SelectedValue = remsCode.ToString(); 

    txtAddress.Text = dtUserDetails.Rows[0].ItemArray[5].ToString(); 
    //Response.Write((Convert.ToInt16(dtUserDetails.Rows[0].ItemArray[6])).ToString()); 
    ddlActive.SelectedValue = (Convert.ToInt16(dtUserDetails.Rows[0].ItemArray[6])).ToString(); 
    ddlIsGVO.SelectedValue = (Convert.ToInt16(dtUserDetails.Rows[0].ItemArray[7])).ToString(); 
    ddlGVOTeams.SelectedValue = gvoTeam; 
    ddlProfiles.SelectedValue = profile; 
    ddlIsSOW.SelectedValue = (Convert.ToInt16(dtUserDetails.Rows[0].ItemArray[12])).ToString(); 
    lblLastUpdatedBy_value.Text = dtUserDetails.Rows[0].ItemArray[14].ToString(); 
    lblLastUpdatedDate_value.Text = dtUserDetails.Rows[0].ItemArray[15].ToString(); 
} 

protected void btnGetREMSdetails_Click(object sender, EventArgs e) 
{ 
    //int remsCode = Convert.ToInt32(ddlREMS.SelectedValue); 

    // create ConnectDatabase object to get acces to its methods 
    ConnectDatabase connectDB = new ConnectDatabase(); 
    IDBManager dbManager = connectDB.ConnectDB(); 
    DataSet ds = new DataSet(); 

    try 
    { 
     dbManager.Open(); 
     dbManager.CreateParameters(1); 
     dbManager.AddParameters(0, "@remscode", Convert.ToInt32(ddlREMS.SelectedValue)); 
     ds = dbManager.ExecuteDataSet(CommandType.Text, "select * from vwREMSDetails where [rems code] = @remscode"); 
     gvREMSDetails.DataSource = ds; 
     gvREMSDetails.DataBind(); 
     gvREMSDetails.Visible = true; 
    } 
    catch (Exception error) 
    { 
     HttpContext.Current.Response.Write(error.ToString()); 
    } 
    finally 
    { 
     dbManager.Close(); 
     dbManager.Dispose(); 
    } 
} 

private static DataSet GetUserDetails(string soeid) 
{ 
    // create ConnectDatabase object to get acces to its methods 
    ConnectDatabase connectDB = new ConnectDatabase(); 
    IDBManager dbManager = connectDB.ConnectDB(); 
    DataSet ds = new DataSet(); 

    try 
    { 
     dbManager.Open(); 
     dbManager.CreateParameters(1); 
     dbManager.AddParameters(0, "@soeid", soeid); 
     ds = dbManager.ExecuteDataSet(CommandType.Text, "select * from vwUsersDetails where soeid = @soeid"); 
    } 
    catch (Exception error) 
    { 
     HttpContext.Current.Response.Write(error.ToString()); 
    } 
    finally 
    { 
     dbManager.Close(); 
     dbManager.Dispose(); 
    } 
    return ds; 
} 

private void GetDDLsItems() 
{ 
    // create ConnectDatabase object to get acces to its methods 
    ConnectDatabase connectDB = new ConnectDatabase(); 
    IDBManager dbManager = connectDB.ConnectDB(); 
    DataSet ds = new DataSet(); 
    try 
    { 
     dbManager.Open(); 
     ds = dbManager.ExecuteDataSet(CommandType.StoredProcedure, "sp_select_edit_user_ddls"); 

     ddlREMS.DataSource = ds.Tables[0]; 
     ddlREMS.DataTextField = "remsCode"; 
     ddlREMS.DataValueField = "remsCode"; 
     ddlREMS.DataBind(); 

     ddlActive.DataSource = ds.Tables[1]; 
     ddlActive.DataTextField = "Active"; 
     ddlActive.DataValueField = "ActiveID"; 
     ddlActive.DataBind(); 

     ddlIsGVO.DataSource = ds.Tables[2]; 
     ddlIsGVO.DataTextField = "IsGVO"; 
     ddlIsGVO.DataValueField = "IsGVOID"; 
     ddlIsGVO.DataBind(); 

     //methods.GetGVOFunctions(ddlGVOFunctions); 
     //int? gvoFunctionID = string.IsNullOrEmpty(ddlGVOFunctions.SelectedValue) ? (int?)null : (int?)Convert.ToInt32(ddlGVOFunctions.SelectedValue); 
     methods.GetGVOTeams(null, ddlGVOTeams); 

     ddlProfiles.DataSource = ds.Tables[3]; 
     ddlProfiles.DataTextField = "profilename"; 
     ddlProfiles.DataValueField = "profileID"; 
     ddlProfiles.DataBind(); 

     ddlIsSOW.DataSource = ds.Tables[4]; 
     ddlIsSOW.DataTextField = "IsSOW"; 
     ddlIsSOW.DataValueField = "IsSOWID"; 
     ddlIsSOW.DataBind(); 

     ddlHeadcount.DataSource = ds.Tables[5]; 
     ddlHeadcount.DataTextField = "Headcount"; 
     ddlHeadcount.DataValueField = "HeadcountID"; 
     ddlHeadcount.DataBind(); 
    } 
    catch (Exception error) 
    { 
     HttpContext.Current.Response.Write(error.ToString()); 
    } 
    finally 
    { 
     dbManager.Close(); 
     dbManager.Dispose(); 
    } 
} 

}

+1

你的描述不清晰,頭腦張貼一些代碼,並解釋多一點更清晰 – iJade

+0

添加的代碼。請讓我知道它是否足夠。 – ironcurtain

+0

通常我的問題是,當我去地址選項卡並按Enter鍵(點擊更新按鈕後)它返回舊數據(這是更新點擊之前)。刷新頁面(F5)時,一切正常。 – ironcurtain

回答

1

我不是100%我完全地認識這個問題,但它聽起來對我說,你需要有

if(!IsPostBack) 
{ 
    // load dropdown data here 
} 

哪裏您將所有數據加載到下拉菜單中,然後在下拉菜單中有

<asp:DropDownList SelectedIndexChanged="ddlDropdown_SelectedIndexChanged" id="ddlDropdown" AutoPostBack="true"></asp:DropDownList> 

然後在你的代碼背後有

protected void ddlDropDown_SelectedIndexChanged(object sender, EventArgs e) 
{ 

}