将文本框值从aspc.cs文件传递到用户控件(ascx.cs)文件 [英] Passing a textbox value from an aspc.cs file to a user control(ascx.cs) file

查看:83
本文介绍了将文本框值从aspc.cs文件传递到用户控件(ascx.cs)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我必须创建一个带有选定日期的excel文件。为此,我需要将文本框日期值从aspx.cs文件传递到用户控件(ascx.cs)文件。首先,我从aspx页面获取文本框值到aspx.cs文件。



POSDetailReports.aspx.cs

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

public string ButtonSelectedDate()
        {
         
                return this.selectDateDisplayTextBox.Text;
          
        }









然后我将此方法调用到ascx.cs文件



DailySalesFigureUserControl.ascx.cs





Then I call this method to the ascx.cs file

DailySalesFigureUserControl.ascx.cs

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)
           {

           }
       }







方法返回null。




The method is returning null.

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





任何人都可以帮我吗?





提前致谢



Can anyone help me on this??


Thanks in advance

推荐答案

您应该创建属性以在用户控件中获取/设置文件名。

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



在您的POSDetailReports.aspx.cs中,使用此代码


In your POSDetailReports.aspx.cs, use this code

usrControl.FileName = ButtonSelectedDate();


您无法访问用户控件中的父页面详细信息。我假设对象posDetailReports是您的partent页面的一个实例。但是rembmer这是一个不同的实例,与呈现给客户端的实例不一样。



看一下这个链接就可以了解一下。 http://vpalmu.wordpress.com/2012 / 04/15 / passing-values-between-usercontrol-and-asp-net-page / [ ^ ]
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/[^]


这篇关于将文本框值从aspc.cs文件传递到用户控件(ascx.cs)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆