2012-11-19 152 views
1

問題:我有一個asp.net文本框,我將文本放入文本中並將其更改爲應將其值賦給變量,這不會發生。從Asp.Net文本框中檢索值TextBox

問題:從Textbox中檢索值的最佳方法是什麼?

C#代碼:

protected void btnSourceConnect_Click(object sender, EventArgs e) 
{ 
    if (Util.Connection(SourceString, 0)) 
    { 
     lblTesting.Text = "Working!"; 
    } 
    else 
    { 
     lblTesting.Text = "It No Work"; 
    } 
} 
protected void txtSourceServer_TextChanged(object sender, EventArgs e) 
{ 
     SourceString.DataSource = txtSourceServer.Text;  
} 

protected void txtSourceDatabase_TextChanged(object sender, EventArgs e) 
{ 

     SourceString.InitialCatalog = txtSourceDatabase.Text; 

} 

protected void txtSourceUN_TextChanged(object sender, EventArgs e) 
{ 

     SourceString.UserID = txtSourceUN.Text; 

} 

protected void txtSourcePass_TextChanged(object sender, EventArgs e) 
{ 

     SourceString.Password = txtSourcePass.Text; 
} 

Asp.Net代碼:

<table style="width: 100%;Border:1px solid Black;"> 
    <tr> 
      <td class="style1"> 
       &nbsp; 
       Source Server:</td> 
      <td class="style1"> 
       &nbsp;&nbsp; 
       <asp:TextBox ID="txtSourceServer" runat="server" 
        ontextchanged="txtSourceServer_TextChanged" AutoPostBack="True"></asp:TextBox> 
      </td> 
      <td class="style1"> 
       &nbsp; Source Database: 
      </td> 
      <td class="style1"> 
       &nbsp;&nbsp; 
       <asp:TextBox ID="txtSourceDatabase" runat="server" 
        ontextchanged="txtSourceDatabase_TextChanged" AutoPostBack="True"></asp:TextBox> 
      </td> 

     </tr> 
     <tr> 
      <td class="style1"> 
       &nbsp; User Name: 
      </td> 
      <td class="style1"> 
       &nbsp;&nbsp; 
       <asp:TextBox ID="txtSourceUN" runat="server" 
        ontextchanged="txtSourceUN_TextChanged" AutoPostBack="True"></asp:TextBox> 
      </td> 
      <td class="style1"> 
       &nbsp; Password: 
      </td> 
      <td class="style1"> 
       &nbsp;&nbsp; 
       <asp:TextBox ID="txtSourcePass" runat="server" 
        ontextchanged="txtSourcePass_TextChanged" AutoPostBack="True"></asp:TextBox> 
      </td> 
     </tr> 
     <tr> 
      <td class="style1"> 
       &nbsp; 
      </td> 
      <td class="style1"> 
       &nbsp; 
      </td> 
      <td class="style1"> 
       &nbsp; 
      </td> 
      <td class="style1"> 
       &nbsp; 
       <asp:Button ID="btnSourceConnect" runat="server" Text="Test Connection" 
        onclick="btnSourceConnect_Click" /> 
      </td> 
     </tr> 
     </table> 

回答

1

什麼是檢索從文本框中的值的最佳方法是什麼?

最好的方法就像我現在正在編寫的控件一樣,你有一個按鈕可以回發,然後你可以在後面的代碼中讀取文本框的值。

在你使用的每個文本框上使用AutoPostBack="True"涉及太多不必要的回發到服務器,最好使用「提交」按鈕並在用戶單擊提交時保存所有值。

在你如果你有一個按鈕,你只需要刪除自動回傳,只有當你嘗試爲連接設置你的價值觀:

protected void btnSourceConnect_Click(object sender, EventArgs e) 
{ 
    SourceString.UserID = txtSourceUN.Text; 
    SourceString.DataSource = txtSourceServer.Text;  
    SourceString.InitialCatalog = txtSourceDatabase.Text; 
    SourceString.Password = txtSourcePass.Text; 

    if (Util.Connection(SourceString, 0)) 
    { 
     lblTesting.Text = "Working!"; 
    } 
    else 
    { 
     lblTesting.Text = "It No Work"; 
    } 
} 
+0

非常感謝你,將標記爲答案 –

1

的AutoPostBack =真上一個文本框將導致頁面在文本框丟失焦點時發回,而不是在每次文本更改時發回。

從你的代碼的外觀來看,這個功能的大部分應該發生在客戶端(即用javascript)。

相關問題