Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,

I have to create a excel file with a selected date. For that I need to pass a textbox date value from a aspx.cs file to a User Control(ascx.cs) file. First I get the textbox value from the aspx page to the aspx.cs file.

POSDetailReports.aspx.cs
C#
public string ButtonSelectedDate()
        {
         
                return this.selectDateDisplayTextBox.Text;
          
        }




Then I call this method to the ascx.cs file

DailySalesFigureUserControl.ascx.cs
C#
protected void ExportActionButton_Click(object sender, EventArgs e)
       {
           try
           {
               List<string> header = new List<string>();
               Dictionary<int, List<string>> body = new Dictionary<int, List<string>>();
               List<string> footer = new List<string>();

               DailySalesFigureModelResponseDto dailySales = new DailySalesFigureModelResponseDto();
               Type type = dailySales.GetType();
               PropertyInfo[] propertyInfoArray = type.GetProperties();
               int count = 0;

               foreach (PropertyInfo propertyInfo in propertyInfoArray)
               {
                   header.Add(propertyInfo.Name);
               }

               foreach (DailySalesFigureModelResponseDto dailySalesResponse in this.DataSource)
               {
                   List<string> bodyRow = new List<string>();

                   foreach (PropertyInfo propertyInfo in propertyInfoArray)
                   {
                       string propertyValue = string.Empty;

                       object value = propertyInfo.GetValue(dailySalesResponse, null);

                       if (value != null)
                       {
                           propertyValue = value.ToString();
                       }

                       bodyRow.Add(propertyValue);
                   }

                   body.Add(count, bodyRow);
                   count++;
               }

               ExportableDataContent exportableDataContent = new ExportableDataContent(header, body, footer);

               TabularContent tabularContent = new TabularContent();
               tabularContent = exportableDataContent.ToTabularFormat();

               DefaultExporter exporte = new DefaultExporter();

               exporte.Open(string.Empty);

               ExportLocation exportLocation = new ExportLocation("A", "2", "Sheet1");
               exporte.WriteContent(tabularContent, exportLocation);

               string tempFolderPath = @"D:\";  ////Path.GetTempPath();

               string fileName = this.posDetailReports.ButtonSelectedDate() + ".xlsx";

               string excelFilePath = tempFolderPath + fileName;

               if (File.Exists(excelFilePath))
               {
                   File.Delete(excelFilePath);
               }

               exporte.Save(excelFilePath, ExportFormat.Excel);

           }
           catch (Exception ex)
           {

           }
       }



The method is returning null.
C#
string fileName = this.posDetailReports.ButtonSelectedDate() + ".xlsx";


Can anyone help me on this??


Thanks in advance
Posted

You should create properties to get/set the file name in your user control.
C#
public string FileName
{
    get
    {
        return fileName;
    }
    set
    {
        fileName = value;
    }
}

In your POSDetailReports.aspx.cs, use this code
C#
usrControl.FileName = ButtonSelectedDate();
 
Share this answer
 
You can not access the parent page details in user controls. I am assuming the object posDetailReports is a instance of your partent page. But rembmer this is a different instance and not same what is rendered to client.

Have a look at this link to get some idea. http://vpalmu.wordpress.com/2012/04/15/passing-values-between-usercontrol-and-asp-net-page/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900