2012-11-19 151 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)。

相关问题