【问题标题】:How to pass the value a user inputs into a textbox control to the global.asax file?如何将用户输入到文本框控件的值传递给 global.asax 文件?
【发布时间】:2019-11-27 00:50:29
【问题描述】:

我需要将管理员在应用程序启动时在文本框控件中输入的值传递给 global.asax 文件,然后我可以将其转换为应用程序对象。我希望这个应用程序对象能够为所有用户和他们的会话持续存在,但仅限于特定的数据库连接字符串。 Application 对象是否是此目的的正确选择,如果是,如何合并它?

这是我尝试过的:

global.asax:

public class Global : System.Web.HttpApplication
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    protected void Application_Start(object sender, EventArgs e)
    {



        string folderName = SetPaths.Tbrootpath.Text.ToString();

        string pathString = System.IO.Path.Combine(folderName, "ScannedData");

        Application["scannedDataPath"] = pathString;
    }

我在需要的地方称之为:

  string pathstring = HttpContext.Current.Application["ScannedDataPath"].ToString();

我得到 SetPaths.Tbrootpath is inaccessible ..

【问题讨论】:

  • 不知道你应该怎么做,但是如果你需要输入一些东西,应用程序必须已经启动,所以你不能在它启动的时候做。
  • 好的,这意味着我应该把它放在会话启动方法中吗?但我试图将管理员定义的路径作为应用程序对象,它会一直持续到应用程序关闭。所以我应该什么时候分配它?

标签: asp.net webforms global-asax


【解决方案1】:

不太清楚您所说的“仅适用于特定的数据库连接字符串”是什么意思,但我想我理解您的意思。

就像 wazz 所说,在应用程序启动之前,您不能在文本框中输入值。您也无法从 Global.asax 文件中访问文本框,因此您必须从数据库中存储和检索值。

您的代码还必须能够处理管理员用户尚未输入文件夹名称的情况。至少,管理员页面需要能够在没有设置此值的情况下运行,以便管理员用户能够输入该值。

所以你的代码可能看起来像这样:

global.asax

public class Global : System.Web.HttpApplication
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    protected void Application_Start(object sender, EventArgs e)
    {
        string folderName = GetFolderNameFromDatabase();
        string pathString = "Default/Path"; // Set a default path that can be used until the admin has entered a folder name for the first time. Could also be empty string etc
        // If a value is found in the database, create path from it.
        // Otherwise, continue with default path.
        if(! string.IsNullOrEmpty(folderName) {    
            pathString = System.IO.Path.Combine(folderName, "ScannedData");
        }

         Application["scannedDataPath"] = pathString;
    }

adminPage.aspx(提交事件)

string folderName = Tbroothpath.Text; // Get value from the textbox.

StoreFolderNameInDataBase(folderName); // Store value in the database.

【讨论】:

  • 有没有办法我们可以将其转移到对话中,以便我可以解释场景并帮助您更好地了解我的需求,以便您可以提供最佳解决方法?
猜你喜欢
  • 1970-01-01
  • 2013-10-08
  • 1970-01-01
  • 2017-08-14
  • 2015-04-01
  • 1970-01-01
  • 2012-12-19
  • 2022-08-15
  • 1970-01-01
相关资源
最近更新 更多