我想在单击按钮时使用System.IO
创建一个简单的文件。实际上,我的代码确实生成了文件,但是每次浏览器重新加载时它都会创建文件,而不是仅在单击按钮时生成。下面是我的代码。任何人都可以帮忙,看看他们是否能弄清楚为什么这会在页面加载时触发?
using System;
using System.IO;
namespace ColorPicker
{
public partial class ColorPicker : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Empty for now
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
SetEventHandlers();
}
private void GenerateSassFile()
{
var ColorInputVal = ColorInput;
ColorInputVal.Attributes.Add("class", "form-control");
ColorInputVal.Attributes.Add("value", "#000");
var ColorInputH = this.ColorInputH.Value = "red";
var RenderStyleBtn = RenderStyle;
RenderStyleBtn.Text = "Render!";
string path = @"c:\ColorPicker\Scss\_vars.scss";
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("$primary: " + ColorInputH + " !important;");
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
private void RenderButton_Click(object sender, EventArgs e)
{
GenerateSassFile();
System.Diagnostics.Debug.WriteLine("Button clicked");
}
private void SetEventHandlers()
{
RenderStyle.Click += RenderButton_Click;
}
}
}
答案 0 :(得分:0)
我认为问题出在这一行,我假设RenderStyle与RenderButton不同,并且每次触发RenderStyle时都会触发RenderButton。
RenderStyle.Click += RenderButton_Click;
因此,每当您按下RenderStyle按钮时,它就是“ GenerateSassFile”。