Save as DialogBox to save textbox content to a newfile using asp.net
- by user195114
I want the users to type their text in the given textbox and on clicking on createNewFile Button, a SaveAs Dialogbox should popup and the users should browse through the location and save the file as desired.
I have tried some thing but
(1) the dialog box goes behind the application
(2) when run, dialogbox opens 3 times, means it executes 3 times
REPLY TO THE POST
protected void btnNewFile_Click(object sender, EventArgs e)
{
StreamWriter sw = null;
try
{
SaveFileDialog sdlg = new SaveFileDialog();
DialogResult result = sdlg.ShowDialog();
sdlg.InitialDirectory = @"C:\";
sdlg.AddExtension = true;
sdlg.CheckPathExists = true;
sdlg.CreatePrompt = false;
sdlg.OverwritePrompt = true;
sdlg.ValidateNames = true;
sdlg.ShowHelp = true;
sdlg.DefaultExt = "txt";
// sdlg.ShowDialog = Form.ActiveForm;
string file = sdlg.FileName.ToString();
string data = txtNewFile.Text;
if (sdlg.ShowDialog() == DialogResult.OK)
{
sw.WriteLine(txtNewFile.Text);
sw.Close();
}
if (sdlg.ShowDialog() == DialogResult.Cancel)
{ sw.Dispose(); }
//Save(file, data);
}
catch
{ }
finally
{
if (sw != null)
{
sw.Close();
}
}
}
private void Save(string file, string data)
{
StreamWriter writer = new StreamWriter(file);
SaveFileDialog sdlg1 = new SaveFileDialog();
try
{
if (sdlg1.ShowDialog() == DialogResult.OK)
{
writer.Write(data);
writer.Close();
}
else
writer.Dispose();
}
catch (Exception xp)
{
MessageBox.Show(xp.Message);
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
I have tried this.