2014-01-30 151 views
0

我對ASP.NET非常陌生,遇到了一個我尚未弄清楚的問題。在我的Default.aspx頁面上,我有一個用戶可以註冊的地方。這隻需要字段 - 電子郵件和密碼。當提交按鈕被點擊時,他們將進入Signup.aspx,它顯示一個更大的註冊表單,其中包含用戶名,電子郵件,密碼,確認密碼等字段。我想自動填充我的電子郵件和密碼文本框與用戶輸入的Default.aspx頁面。這可能嗎?我正在使用Microsoft Visual Web Developer 2010 Express。基於用戶輸入的自動填充文本框

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

然後邏輯上我的標題只應該說「基於用戶輸入」;) – Vince

+0

否。反對意見是將「元數據」添加到您的標題。 –

回答

0

有多種方法可以做到這一點。

  • 一種方法是使用查詢字符串導航到喜歡:

    http://example.com/Signup.aspx?email=value1&password=value2

    和Signup.aspx獲得價值的Page_Load事件像:

    String email = Request.QueryString["email"];

    String password = Request.QueryString["password"];

    ,並分配給您的文本框控件,如:

    txtEmail.text =email;

    txtPassword=password;

但是發佈的密碼作爲查詢字符串明文是不是好的做法。

  • 其他的方法是GET HTTP POST 從源頁面信息:
    1. 在源頁,包括包含HTML元素(例如輸入或文本域)或ASP.NET服務器表單元素控件(如TextBox或DropDownList控件)在提交表單時發佈值。
    2. 在目標頁面中,閱讀Form集合,該集合返回一個名稱/值對的字典,每個發佈值一對。例如

//

void Page_Load(object sender, EventArgs e) 
{ 
    System.Text.StringBuilder displayValues = 
     new System.Text.StringBuilder(); 
    System.Collections.Specialized.NameValueCollection 
     postedValues = Request.Form; 
    String nextKey; 
    for(int i = 0; i < postedValues.AllKeys.Length; i++) 
    { 
     nextKey = postedValues.AllKeys[i]; 
     if(nextKey.Substring(0, 2) != "__") 
     { 
      displayValues.Append("<br>"); 
      displayValues.Append(nextKey); 
      displayValues.Append(" = "); 
      displayValues.Append(postedValues[i]); 
     } 
    } 
    Label1.Text = displayValues.ToString(); 
} 
  • 最佳 & 最簡單我認爲是使用會議

    Submitbutton_click事件默認的。ASPX,值保存到會話,如:

    Session["Email"] = txtEmail.text;

    Session["Password"]=txtPassword.text;

Signup.aspxPage_load事件檢索值,如:

string email = (string)(Session["Email"]);

string password = (string)(Session["Password"]);

&分配給你的控制/文本框,如:

txtEmail.text =email;

txtPassword=password;

有更多的方式太檢索前一頁值一樣PreviousPage.FindControl("txtEmail"),但我從來不喜歡他們。

+0

我的代碼出了什麼問題?在我的默認頁面上,在submitbutton_click事件處理程序中,我有:Session(「Email」)= TextBoxEmail.Text然後在我的Signup頁面中,在onload處理程序中,我有:Dim Email As String = Session(「Email」)/ n NewTextBoxEmail.Text =電子郵件。它不會像我所期望的那樣在文本框中顯示文本。 – Vince

+0

當我使用調試器,它給了我這個錯誤:對象引用未設置爲對象的實例。 – Vince

+0

您需要將會話中的值轉換爲其對應的數據類型,並在使用會話之前檢查null,例如if(Session [「Email」]!= null &&!string.IsNullOrEmpty((string)Session [「Email」])) {\\做你的事情}' –

相關問題