保存数据后填充表单 [英] Populate form after data is saved

查看:64
本文介绍了保存数据后填充表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户填写并单击提交的表单。保存数据后,用户将重定向回主屏幕。当用户返回到刚填写的表单时,我正在尝试获取用户刚刚保存的数据以填充表单。有办法吗?



I have a form that a user fills out and clicks submit. Once the data is saved and the user is redirected back to the home screen. I am trying to get the data that the user just saved to populate the form when the user goes back to the form they just filled out. Is there a way of doing this?

SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con3.Open();

            SqlCommand scmd3 = new SqlCommand("Select INST_ID, FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from Table33 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
            SqlDataReader dr3 = scmd3.ExecuteReader();
            if (dr3.Read())

            TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
            TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
            TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
            TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
            TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
            TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
            TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
            TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
            TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
            TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
            TextBoxTNNCC.Text = dr3["NCHC"].ToString();
            con3.Close();
            dr3.Close();

推荐答案

大量的方式...你想要做的就是保存数据并在页面加载事件中恢复它,但具体如何做到这一点取决于你。

如果您将用户登录,那么您可能希望将数据保存在数据库中并在那里恢复论坛。替代方案是cookie(存储在客户端,因此可以在会话之间保留,前提是用户使用相同的PC)或者在Session集合中(在服务器上,因此当浏览器页面关闭时丢失,或者20分钟,无论哪个)首先发生)



想一想你想要做什么,然后谷歌可以帮助你提供更多细节。
Loads of ways... Probably what you want to do is save the data and restore it in the page load event, but exactly how you do that is up to you.
If you log your users in, then you might want to save the data in your database and restore forum there. Alternatives are cookies (stored on the client, so can be preserved from session to session, provided the user uses the same PC) or in the Session collection (on the server, so it's lost when the browser page closes, or twenty minutes, whichever happens first)

Have a think about exactly what you want to do, and then Google can help you with more details.


在代码块周围添加 {} ,只有在读取数据时才应执行。请参阅下面的示例。编写代码的方式会导致赋值语句以 TextBoxFTG.Text = dr3 [FT_GRAD]开头.ToString(); 执行是否有数据。



我在代码开头添加了一个 if 语句来检查以确保有数据在 TextBoxINST_ID.Text 中。这将处理 TextBoxINST_ID.Text 尚未包含值的情况。另外,请参阅下面有关使用SQLParameter类的注释。



Add { and } around the code block that should be executed only when data is read. See the example below. The way your code is written would cause the assignment statements starting with TextBoxFTG.Text = dr3["FT_GRAD"].ToString(); to be executed whether there was data or not.

I added an if statement at the start of the code to check to be sure that there is data in TextBoxINST_ID.Text. This will handle the case when TextBoxINST_ID.Text does not yet contain a value. Also, see comment below about using SQLParameter class.

if (TextBoxINST_ID.Text.Trim().Length > 0)
{
SqlConnection con3 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
            con3.Open();
 
            SqlCommand scmd3 = new SqlCommand("Select INST_ID, FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC from Table33 where INST_ID = '" + TextBoxINST_ID.Text + "'", con3);
            SqlDataReader dr3 = scmd3.ExecuteReader();
            if (dr3.Read())
            {
            TextBoxFTUG.Text = dr3["FT_UNDERGR"].ToString();
            TextBoxFTG.Text = dr3["FT_GRAD"].ToString();
            TextBoxTHUGDR.Text = dr3["FTE_UNDERG"].ToString();
            TextBoxTHGDR.Text = dr3["FTE_GRAD"].ToString();
            TextBoxNCCDR.Text = dr3["NON_CREDIT"].ToString();
            TextBoxTCNC.Text = dr3["TOTAL_FTE"].ToString();
            TextBoxTNFUG.Text = dr3["FCFTUHC"].ToString();
            TextBoxTNFG.Text = dr3["FCFTPBHC"].ToString();
            TextBoxTNCPUG.Text = dr3["FCPTUHC"].ToString();
            TextBoxTNCPG.Text = dr3["FCPTPBHC"].ToString();
            TextBoxTNNCC.Text = dr3["NCHC"].ToString();
            }
            con3.Close();
            dr3.Close();
}





此外,使用参数化查询而不是'是最佳做法SELECT语句中的+ TextBoxINST_ID.Text +'。这可以防止SQL注入攻击并提供更好的性能。请参阅Microsoft帮助文件文档 SQLParameter类 [ ^ ]



Also, it would be a best practice to use a parameterized query rather than '" + TextBoxINST_ID.Text + "'" in you SELECT statement. This prevents SQL Injection Attacks and provides better performance. See Microsoft help file documentation for SQLParameter Class[^]


这篇关于保存数据后填充表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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