Load a txt file in a TextBox

Published 02 mars 08 06:51 | Laurent Duveau

Here is a very simple piece of C# code to load a text file in a TextBox. Typical use to display a disclaimer.

using System.IO;



if
(!Page.IsPostBack)
{
   StreamReader StreamReader1 =
             new StreamReader(Server.MapPath("disclaimer.txt"));
   txtDisclaimer.Text = StreamReader1.ReadToEnd();
   StreamReader1.Close();
}

Then you could add a CheckBox to enable a Postback Button (disabled by default):

<input id="cbAgree" type="checkbox" onclick="ManageBtnGoState();" />
<label for="cbAgree" style="font-weight:bold; color:Red;">
I Agree with the present statement</label>
<asp:Button ID="btnGo" runat="server" Text="Take Survey"
onclick="btnGo_Click" Enabled="false" />

And the Js function:

<script type="text/javascript">
function ManageBtnGoState()
{
    $get('<%= btnGo.ClientID %>').disabled = 'disabled';  
    if ( $get("cbAgree").checked == true )
    {    $get('<%= btnGo.ClientID %>').disabled = '';  }
}
</script>

Note that $get() is a function from the ASP.NET AJAX client framework. If you don't have it please use document.getElementById() instead.

Read the complete post at http://weblogs.asp.net/lduveau/archive/2008/03/02/load-a-txt-file-in-a-textbox.aspx

Filed under: ,