如何使用回发C# [英] how to use postback c#

查看:60
本文介绍了如何使用回发C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对C#相当陌生.我有一个Web应用程序,它连接到MySQL服务器,并在代码隐藏文件的Page_Load方法上将gridview绑定到它的数据.我的网络应用程序中有3个页面.每次我单击页面时,它总是从SQL加载数据,简而言之,每次我单击页面时,它都会建立连接.我希望它只加载一次,在页面第一次加载期间,因此,如果用户返回页面,它将不会尝试连接到MySQL服务器.我目前在每个页面的Page_Load中都有这个

Hi,

I am quite new with C#. I have a web app that connects to a MySQL server and binds a gridview to it''s data, on the Page_Load method of the code-behind file. I have 3 pages in my web app. Every time I click on the page it always loads the data from the SQL, in short it makes a connection every time I click the page. I want it to load only once, during the first time the page loads, so if the user goes back to the page it won''t try to connect to the MySQL server. I currently have this on the Page_Load of each page

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               if(DBconnection=true)
               {
               }
            }
        {



任何输入,我们将不胜感激.



Any input is greatly appreciated.

Thanks!

推荐答案

使用页面属性IsPostback很有帮助,只要您基于同一页面的各种事件回发到同一页面即可.
如果要在不同页面上保留数据,则需要将其存储在SessionCache中.这样,只要存储了数据,就可以在不连接数据库的情况下取回数据.

以下内容会有所帮助:
MSDN:ASP.NET状态管理概述 [ ASP.NET中的状态管理-1 [
Using page property IsPostback helps as long as you are posting back to same page based on various events of the same page.
If you want to persist data across different pages then you need to store it in Session or Cache it. This way, you can get your data back without connecting to database as long as you have data stored.

Following would help:
MSDN: ASP.NET State Management Overview[^]
State management in ASP.NET - 1[^]




回发是将表单提交回服务器(例如通过单击按钮)的事件.
如果您只想在页面加载和第一次加载时填充网格视图,
如果您不想在按钮单击事件或类似事件期间建立数据库连接,则必须输入数据库连接页面负载,并且该负载应在
之内 如果(!IsPostBack)
{
//您的代码在这里
}

这样数据库连接仅发生一次.
不仅是数据库连接,还可以在not postback事件中输入代码,以便仅在循环语句之初就必须加载的任何内容.

谢谢

Ganesh
Hi,

Postback is an event when a form is submitted back to the server for example by a button click.
If you want to fill the grid view only at the first time when the page loads and
if you do not want make a database connection during the button click event or similar events, you must enter the database connection page load and it should be within
if (!IsPostBack)
{
//Your code Here
}

So that the database connection occurs only once.
Not only database connection, you can also enter the code within not postback event for anything that must be loaded only at the beginning like loop statements.

Thank You,

Ganesh


这篇关于如何使用回发C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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