Html InputFile control regarding

R

Raji

Hi!
I'm working on ASP.NET using C#.
I'm using Html InputFile Control in webform to download file.I've two buttons,namely,"Import" and "Show summary".I'll hav to click "Show Summary" to see the details of the file to be downloaded before clicking "Import" button.But when I click "Show Summary",the filepath in inputFile control is lost,as result I'm unable to download the file(on clicking "Import").Can anyone suggest a solution asap.
regards,
raji
Note;Code given below for reference.
public class ImportQB : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblTitleMsg;
protected System.Web.UI.WebControls.Label lblErr;
protected System.Web.UI.HtmlControls.HtmlTableRow NewButtons;
protected System.Web.UI.HtmlControls.HtmlInputFile fileCtl;
protected System.Web.UI.WebControls.Button btnRestore;
protected System.Web.UI.HtmlControls.HtmlTableRow trImport;
protected System.Web.UI.WebControls.Panel pnlSummary;
protected System.Web.UI.WebControls.Button btnSummary;
DataSet ds;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvFile;
string filename;
protected Header ucHeader;

protected HtmlGenericControl FileName=new HtmlGenericControl();
protected System.Web.UI.HtmlControls.HtmlTableRow trInputFile= new TableRow();
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.HtmlControls.HtmlTableCell tdInputFile=new HtmlTableCell();

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
ucHeader.currentTab = Common.Tabs.QuestionBanks;
}

// Put user code to initialize the page here
if(Context.User.IsInRole(Roles.DataAdmin.ToString())==false &&
Context.User.IsInRole(Roles.Admin.ToString())==false)
{
Response.Redirect("AccessDenied.aspx");
}

fileCtl.Accept="xml";

}
private bool LoadFile()
{
//if the file has been selected, then move this file to the server and load it into the DS
if (fileCtl.PostedFile != null)
{
filename=CommonMethods.RestoreFilePath;
fileCtl.PostedFile.SaveAs(filename);

ds=new DataSet();
string schemafile=CommonMethods.ExportSchemaName;
try //generate the schema file
{
if (System.IO.File.Exists(schemafile)==false)
{
CommonMethods.GenerateExportSchema();
}
}
catch(Exception ex)
{
lblErr.Text = " Unable to generate schema.<br>" + ex.Message;
return false;
}


try
{
//ds.ReadXmlSchema(schemafile);

try // read xml
{
ValidateXML();
ds.ReadXml(filename);
}
catch(Exception ex)
{
lblErr.Text = "The file was not in the correct format.<br>" + ex.Message;
return false;
}
}
catch(Exception ex)
{
lblErr.Text = " Dependency information is missing.<br>" + ex.Message;
return false;
}

return true;
}
else
{
return false;
}

}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnRestore.Click += new System.EventHandler(this.btnRestore_Click);
this.btnSummary.Click += new System.EventHandler(this.btnSummary_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnRestore_Click(object sender, System.EventArgs e)
{
if (LoadFile()==false)
{
lblErr.Text += "<br>Import failed!";
return;
}


try
{
QuestionBankMethods.RestoreQuestionBank(ds);
lblErr.Text="Question Bank imported successfully!";

}
catch (Exception ex)
{
lblErr.Text="Question Bank could not be imported!<br>" + ex.Message;
}

}

private void PopulateSummary(DataTable dtbl)
{
Table tb=new Table();
tb.CellSpacing=0;
tb.CellPadding=5;
TableRow trow;
TableCell tcell;

for(int i=0; i<dtbl.Columns.Count; i++)
{
trow=new TableRow();
tcell=new TableCell();
SetStyle(ref tcell);
tcell.Text=dtbl.Columns.ColumnName ;
trow.Cells.Add(tcell);

tcell=new TableCell();
SetStyle(ref tcell);
for(int j=0; j<dtbl.Rows.Count; tcell.Text += "<br>", j++ )
{
if (dtbl.Rows[j].GetType()==typeof(DateTime))
{
tcell.Text = Convert.ToDateTime(dtbl.Rows[j].ToString()).ToString("dddd, dd-MMM-yyyy hh:mm tt");
}
else
{
tcell.Text = dtbl.Rows[j].ToString();
}
}
trow.Cells.Add(tcell);
SetStyle(ref trow);
tb.Rows.Add(trow);
}

pnlSummary.Controls.Add(tb);
}

private void btnSummary_Click(object sender, System.EventArgs e)
{
if (LoadFile()==false)
{
return;
}

trInputFile.Visible=true;
tdInputFile.Visible=true;
fileCtl.Visible=true;
TextBox1.Text=fileCtl.PostedFile.FileName;
PopulateSummary(ds.Tables[DBHelper.tblBckupHeader]);

}

private void SetStyle(ref TableCell tcell)
{
tcell.CssClass="LabelBold";
tcell.VerticalAlign=VerticalAlign.Top;
tcell.BorderStyle=BorderStyle.Solid;
tcell.BorderWidth=Unit.Pixel(1);
}

private void SetStyle(ref TableRow trow)
{
trow.VerticalAlign=VerticalAlign.Top;
trow.BorderStyle=BorderStyle.Solid;
trow.BorderWidth=Unit.Pixel(1);
}

private bool ValidateXML()
{
XmlTextReader xrFile=null;
XmlValidatingReader xval=null;

try
{
//read the xml file
xrFile = new XmlTextReader(filename);

xval = new XmlValidatingReader(xrFile); //load the xml file in the validation reader
xval.Schemas.Add(LoadSchema()); //load schema
xval.ValidationType=ValidationType.Auto; //type of validation
xval.ValidationEventHandler += new ValidationEventHandler(ValidationError); //event handler to be used in case of error

while(xval.Read());

return true;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (xval != null)
{
xval.Close();
}
}

}


private XmlSchema LoadSchema()
{
//read the schema file
XmlTextReader xr = new XmlTextReader(CommonMethods.ExportSchemaName);

XmlSchema xsd = XmlSchema.Read(xr, new ValidationEventHandler(ValidationError));
xr.Close();
return xsd;

}

private void ValidationError (object sender, System.Xml.Schema.ValidationEventArgs e)
{
throw new Exception(e.Message);
}


}




___
Newsgroups brought to you courtesy of www.dotnetjohn.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top