简体   繁体   English

如何在 ASP.NET 中将值从 Web 表单传递给另一个?

[英]How pass a values from a web form to another in ASP.NET?

I am trying to send string data from one web form to another, however it throws the System.NullReferenceException error.我试图将字符串数据从一个 Web 表单发送到另一个,但是它抛出 System.NullReferenceException 错误。 I read in the Postgress documentation that this error means there is no data to work with.我在 Postgress 文档中读到这个错误意味着没有数据可以使用。 This is the code for the first form:这是第一种形式的代码:

protected void Button2_Click(object sender, EventArgs e)
{
    editar ed = new editar(tname.Text.ToString());
    //ed.putId(tname.Text.ToString());
    String url = "editar.aspx";
    Response.Redirect(url);
}

Creating the object of the second form was one of the ways I found to send the data, but it keeps throwing the error.创建第二种形式的对象是我发现的发送数据的方式之一,但它不断抛出错误。 In the second form you should receive it.在第二种形式中,您应该收到它。

public editar(string name)
{
    this.tname.Text = name;
}

The basic idea is that the string is received in the second form to put it in a text box and there, by means of a button, the information of a database can be updated.其基本思想是在第二种形式中接收字符串,将其放入文本框中,然后通过按钮更新数据库的信息。

protected void Button2_Click(object sender, EventArgs e)
{
    upt = new NpgsqlCommand("update usuario set cedula = '" + tcedula.Text.ToString() + "' where nombre = '" + tname.Text.ToString() + "'", conn);
    upt.ExecuteNonQuery();
}

I have already tried through the POST and Session method, the problem with these two methods is that although the data arrives correctly every time the page is reloaded the data is reloaded and that happens each time the button, therefore the SQL statement to update the information is null, that is, it executes it, but it does not update the database.我已经尝试过 POST 和 Session 方法,这两种方法的问题是,尽管每次重新加载页面时数据都正确到达,但每次重新加载数据时都会发生这种情况,因此每次单击按钮时都会发生这种情况,因此需要更新信息的 SQL 语句为空,即执行它,但不更新数据库。 I use Visual Studio 2019, ASP.NET, and Postgres.我使用 Visual Studio 2019、ASP.NET 和 Postgres。

There are two things to understand here:这里有两件事需要理解:

  1. Web Forms still use HTTP requests for each page view and even for each server event like button clicks. Web 窗体仍然为每个页面视图甚至每个服务器事件(如按钮单击)使用 HTTP 请求。
  2. Form class instances do not persist between requests.表单类实例不会在请求之间持续存在 Your code runs for long enough to send some html or javascript to the browser, and then everything is immediately thrown away;您的代码运行足够长的时间来向浏览器发送一些 html 或 javascript,然后所有内容都会立即被丢弃; nothing stays in memory.没有什么会留在记忆中。 Even if the same user requests the same page again (ie, clicks refresh in their browser), you still get an entirely new instance of your page class that doesn't know anything of what has come before.即使同一个用户再次请求同一个页面(即,在他们的浏览器中点击刷新),您仍然会得到一个全新的页面类实例,它对之前发生的事情一无所知。

Therefore, if you have a value you want to use across HTTP requests, as in this case when you have two different forms, you have to put it somewhere that will be available when the later http request is processed.因此,如果您有一个值要在 HTTP 请求中使用,就像在这种情况下,当您有两种不同的表单时,您必须将它放在某个地方,以便在处理稍后的 http 请求时可用。 You can do that several ways: the session, a GET (url) or POST parameter, or save to a database during the first request and receive on the second.您可以通过多种方式做到这一点:会话、GET (url) 或 POST 参数,或者在第一个请求期间保存到数据库并在第二个请求中接收。

In this case, a session value seems most appropriate, but it's hard to be sure without knowing more of what you're doing.在这种情况下,会话值似乎最合适,但如果不了解更多您在做什么,就很难确定。


And for God's sake, fix that glaring SQL injection issue.看在上帝的份上,修复那个明显的 SQL 注入问题。 You shouldn't be writing database code for the web without knowing about that.您不应该在不知道这一点的情况下为 Web 编写数据库代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Asp.net 中将值从一种形式传递到另一种形式 - How can I pass values from one form to another in Asp.net 如何:在ASP.NET网页之间传递敏感值? - How to: Pass sensitive values Between ASP.NET Web Pages? 将值从一个网页中的asp.net控件传递到另一网页中的asp.net控件 - Pass values from the asp.net controls in one webpage to asp.net controls in another webpage 如何在ASP.NET Web表单中使用Ajax更新ListView中的值 - How to update values in a ListView with ajax in asp.net web form 如何将值从一个控制器传递到ASP.Net MVC3中的另一个控制器 - How to pass values from One controller to another Controller in ASP.Net MVC3 如何使用从ASP.NET Web表单中的另一个对象内实例化的对象 - How to work with an object that was instantiated inside another object from a ASP.NET web form 如何从C#ASP.NET Web表单背后的代码获取图表值 - How to get values of chart from code behind c# asp.net web form 如何以 web 形式从 asp.net 控件到 javascript 获取值 - How to get values from an asp.net control to a javascript in the web form 如何在asp.net中将图像从一页传递到另一页 - how to pass image from one page to another in asp.net 在ASP.NET中从一种Web表单移动到另一种Web表单 - Moving from one web form to another web form in ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM