I've created a method so that when a button (butCommit) is clicked, the value of a textbox is taken and stored into a string. That string is then written to a .txt file using the WriteAllText method. However, when this method is executed, the file is not modified at all.
The button method is working fine, as I have a response.redirect method in there which works every time.
The path for the .txt file is also correct as I have another method which will, on the page load, display the current contents of the .txt file (using the ReadAllText method) and the exact same path that I am using for the WriteAllText method.
Here is the code giving me problems:
void butCommit_Click(object sender, EventArgs e)
{
var path = Server.MapPath(#"~/content.txt");
string content = txtHomepageContent.Text;
System.IO.File.WriteAllText(path, content);
Response.Redirect("Default.aspx");
}
I'll repeat again: The above method is initialized and works fine with the Response.Redirect method, just not the WriteAllText method.
EDIT For more clarity:
The purpose of this is to display a message on the home page of a site. That message is required to be stored in a .txt file which is on the server (named content.txt). In the ControlPanel.aspx page, the user needs to be able to change the context of the text file using a text box. Preferably they are also able to view what is currently in content.txt using the same text box.
The code I have for the viewing bit is this:
protected void Page_Load(object sender, EventArgs e)
{
var path = Server.MapPath(#"~/content.txt");
string content = System.IO.File.ReadAllText(path);
txtHomepageContent.Text = content;
}
This specifies "path" as the path to content.txt, then specifies "content" as a string containing content.txt's data. After that it places "content" into the textbox txtHomepageContent. This is also the text box used to input the new data for content.txt.
It seems if I remove the above section of code, I am able to write to the text file now with no problems, however I of course am unable to view what is in there first. If I leave the above code chunk in, then whatever I write in the textbox is ignored, and the original text is submitted back into the content.txt, resulting in no changes.
I do understand there are easier ways of doing this such as storing in a database, but the requirements are for it to be in a .txt file.
You can either use the Page_Init event handler instead of Page_Load to initialize your text box (since submitted postback values will load between the Init and Load events, effectively overwriting the text box value with what was submitted), or if you want to stay with Page_Load, check whether there is a postback first:
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.IsPostBack)
return;
var path = Server.MapPath(#"~/content.txt");
string content = System.IO.File.ReadAllText(path);
txtHomepageContent.Text = content;
}
Related
I have a simple request. I have this:
So, in my .aspx :
<asp:FileUpload ID="myFileUpload" runat="server" onclick="LoadFile_Click" />
After the pression of the "Load file" button, I want that the page will be filled with the data of the document choosen. Like this:
In my .aspx.cs
protected void LoadFile_Click(Object sender, EventArgs e)
{
string address = // a way to get the path of the file. How?
BindExcelToPage(address); // this method fills the web page with the data taken from the file uploaded;
}
The informative phrase "No file selected" (or whatever) must be invisible. I want just the Load button.
I don't know how to implement the starts of the event for the loading of the page.
After you BindExcel file with page, hide your fileupload control and replace with it label(if you want use label and button) and set the filename in label. This way it looks like file is there. FileUpload control's filename is readonly, so you can't assign it.
I'm currently trying to look at a directory, and then preview a .jpeg from a list box. I have the list box populating with the contents of the directory and only showing Jpegs, but I can't think of what to do to get the jpeg preview in a picture box. I'm using an asp .net application on Visual Studio 2010.
This is the code I have
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo infoDir = new DirectoryInfo(#"G:\Test_Directory");
FileInfo[] infoFile = infoDir.GetFiles("*.jpeg");
foreach( FileInfo file in infoFile )
{
lstDirectory.Items.Add(file.Name);
}
}
protected void lstDirectory_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
I'm under the understanding Postback needs to be used. If anyone is able to help, that would be great.
The file which is in the G: Drive, is a jpeg, which can be seen in the list box is : jpegimage.jpeg
Thanks.
How about something like this?
I think you could do this mostly in Javascript, with two additional ASP.NET page.
First, create a new web page. We'll call this A.aspx. This page will be passed the image name in the query string. It will be very simple: it will just fetch the contents of the file from "G:\TestDirectory" and write it to the Response stream. There are quite a few questions and answers on Stack Overflow on how to do this, if you haven't done it before.
Then, create another web page. We'll call this B.aspx. This will have an image control with height and width set appropriately. It will also take the image name from its query string. The code-behind will build a URL to use as the ImageSource property on the image control. The URL will be that of A.aspx, with the (URL-encoded) image name appended as a parameter.
On your ASP.NET page, hook up an event handler to your listbox. When the selected index on the list box changes, on the client side, build a URL, based on the URL to B.aspx with the image name from the list box appended as a parameter. Then open a window, using the URL you just built, pointing to B and passing the desired file name.
So: when the list box selected index changes (or when you double click, or whatever event you pick), the javascript will open a window with page B.aspx. Page B will have an image control, set to the URL to A.aspx. A.aspx will stream the image contents to the image control, which will appear in your new window.
i have been trying to upload a file to Fileshawk.com from my application.
Here is how i did it :
1- Created a web browser control
2- Loaded the page
3- in the web page it has an input tag of file.
4- i tried to add the file to HTML by using that method which failed.
private void Set_Text_TAG_INPUT(string attribute, string attname, string value)
{
// Get a collection of all the tags with name "input";
HtmlElementCollection htmle = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement current in htmle)
{
if (current.GetAttribute(attribute).Equals(attname))
{
current.SetAttribute("value", value);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Set_Text_TAG_INPUT("id","upfile_1342028154587", "FILE.txt");
}
So it didn`t change the value of the Input tag.
Is there any way to add my file to the HTML or page code and about pressing upload i already have a method that invokes the web browser button and click it.
For example , when you click Select file from the input file tag , a window pop up and you select the file , now when you click okay , where is that file stored in the HTML ?
Finally excuse me if i am a newbie in HTML and web knowledge.
This is a security feature in browsers. You cannot programmatically set the value of the value of an input tag of type 'file' since it would introduce a security hole (e.g. a malicious developer could set the value right before handling a submit event and get whatever file they wanted).
Use WebScraper, a C# .NET library to upload file/download file easily, but you need to analyze Fileshawk.com HTML to come up with your own WebScraper syntax, there are totally 22 file host syntax available for sample and already useable.
http://sorainnosia.com/Home/Article/WEBSCRAPER
I'm trying to create a C# webpage that uses a GET method so I can bookmark form data, email it to other people, etc., but changing the form method to GET results in the querystring containing the VIEWSTATE. I can take out the runat=server tags (as mentioned here) but then I don't know how to set the values of my form fields. I don't mind manually persisting them, but I can't figure out how.
So, how do I either utilize the viewstate but keep it out of my querystring, or access the form controls without runat=server?
If you are using, for example, a search/filter form on your webpage that you want via GET, here's a pattern I've used multiple times in my own code. When the user submits a button to process what they've entered in the form, I let it hit my code-behind as a POST as normal. Then I manually construct a GET request and redirect to it:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var query =
"?filter1=" + TxtFilter1.Text +
"&filter2=" + TxtFilter2.Text +
"&filter3=" + TxtFilter3.Text; // etc.
Response.Redirect(query);
}
Then on load of the page, I process the entries in the query string to pre-populate the relevant form controls on the page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TxtFilter1.Text = Request.QueryString["filter1"] ?? "";
TxtFilter2.Text = Request.QueryString["filter2"] ?? "";
TxtFilter3.Text = Request.QueryString["filter3"] ?? "";
// etc.
}
}
This allows me to have control over what actually goes into the QueryString without messing with the built-in form POST managed by ASP.Net (including View State). Also, when a user has filled out the form and pressed the button, they have a linkable url, and even though it was a POST, if they refresh the page they don't get that annoying message about re-sending data to the server.
I've got a problem on a WebForms application where a user selects some criteria from drop downs on the page and hits a button on the page which calls this method:
protected void btnSearch_Click(object sender, EventArgs e)
They then click on button to download a zip file based on the criteria which calls this method:
protected void btnDownload_Click(object sender, EventArgs e)
In IE, they are prompted with the bar at the top of the browser that tells them:
"To help protect your security, Internet Explorer blocked this site
from downloading files to your computer".
When they click on that bar to download the file, it fires the btnSearch_Click event again.
Response.ContentType and Response.AddHeader has been set up correctly.
The problem is, that btnSearch appends criteria so basically it is being appended twice and causing problems.
Is there something I can do to prevent this?
This is a VS2008 web application using C# 3.5 for what it's worth.
When they click the download button, do a Redirect to the ZIP file handler (page?) to download the file. i.e. use the Post-Redirect-Get pattern: http://en.wikipedia.org/wiki/Post/Redirect/Get
Are you using the Content-Disposition header?
Response.AddHeader("Content-Disposition", "attachment; filename=fileName.zip");
Try changing the content type to match the file type?
Save a boolean value into the Session indicating that the criteria have been already appended.
When the user selects another value from the dropdowns then set this value to false.
Inside your btn_Download event you then can check the value of the Session variable and avoid setting the criteria twice.