页面加载填充文本框代码检查 [英] Page Load Populate Textboxes code check

查看:69
本文介绍了页面加载填充文本框代码检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道我的代码是否正确或者我是否需要更改任何内容?我正在做的是在用户登录网站时填充文本框。文本框将使用用户信息填充数据库中的数据,具体取决于登录的用户。以下是页面加载的代码:



I just wanted to know if my code was right or if I need to change anything? What I am doing is populating textboxes when a user logs in to the website. The textboxes will populate with data from the database with the user information depending on the user that is logged in. Here is the code for Page Load:

protected void Page_Load(object sender, EventArgs e)
    {
        ButtonPrint.Attributes.Add("onclick", "window.print(); return false");

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

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


        SqlCommand scmd = new SqlCommand("Select INST_ID, LongName from TableCOCINST where INST_ID = LongName" + TextBoxSchool.Text + "'", con);
        SqlCommand scmd2 = new SqlCommand("Select INST_ID, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT from TableFIN2012 where INST_ID = INST_ID" + TextBoxSchool.Text + "'", con2);
        SqlDataReader dr = scmd.ExecuteReader();
        SqlDataReader dr2 = scmd2.ExecuteReader();

        if (dr.Read())
        if (dr2.Read())
            {
                TextBoxSchool.Text = dr["LongName"].ToString();
                TextBoxLYInstr.Text = dr2["INSTRUCTIO"].ToString();
                TextBoxLYRes.Text = dr2["RESEARCH"].ToString();
                TextBoxLYPubS.Text = dr2["PUBLIC_SER"].ToString();
                TextBoxLYAcad.Text = dr2["ACADEMIC_S"].ToString();
                TextBoxLYStudS.Text = dr2["STUDENT_SE"].ToString();
                TextBoxLYInstiS.Text = dr2["INSTITUTIO"].ToString();
                TextBoxLYOperM.Text = dr2["PHYSICAL_P"].ToString();
                TextBoxLYSFEDA.Text = dr2["SCHOLARSHI"].ToString();
                TextBoxLYAuxE.Text = dr2["AUXILIARY_"].ToString();
                TextBoxLYHosS.Text = dr2["HOSPITALS"].ToString();
                TextBoxLYIndeO.Text = dr2["INDEPENDEN"].ToString();
                TextBoxLYOED.Text = dr2["OTHEREXP"].ToString();
                TextBoxLYTA.Text = dr2["TOTASSETS"].ToString();
                TextBoxLYTL.Text = dr2["TOTLIABILITY"].ToString();
                TextBoxLYNPRNA.Text = dr2["NoNEXPPERMRESASSETS"].ToString();
                TextBoxLYTUNA.Text = dr2["UNRNETASSETS"].ToString();
                TextBoxLYTR.Text = dr2["TOTALREV"].ToString();
                TextBoxLYTFN.Text = dr2["TUITFEES"].ToString();
                TextBoxLYCD.Text = dr2["CURRDEBT"].ToString();
                TextBoxLYLTD.Text = dr2["LONGTERMDEBT"].ToString();
                TextBoxINST_ID.Text = dr["INST_ID"].ToString();

            }
        dr.Close();
        con.Close();
        dr2.Close();
        con2.Close();

推荐答案

你应该把这段代码放在 IsPostBack [ ^ ]阻止。否则,每次由于页面上的任何事件而发生回发时都会执行。

You should put this code inside IsPostBack[^] Block. Otherwise, it will be executed each time Post Back happens due to any event on your page.
protected void Page_Load()
{
    if (!IsPostBack)
    {
        // All your codes.
    }
}


我花了一分钟找到我的错误并纠正它。我还添加了我需要的会话。以下是新代码:



It took me a minute to find my mistake and correct it. I also add the session that I need. Here is the new code:

using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Drawing.Printing;

public partial class FinancialProfileFormA : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        ButtonPrint.Attributes.Add("onclick", "window.print(); return false");

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

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

        TextBoxINST_ID.Text = Session["inst_id"].ToString();
        SqlCommand scmd = new SqlCommand("Select INST_ID, LongName from TableCOCINST where INST_ID = '" + TextBoxINST_ID.Text + "'", con);
        SqlCommand scmd2 = new SqlCommand("Select INST_ID, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT from TableFIN2012 where INST_ID = '" + TextBoxINST_ID.Text + "'", con2);
        SqlDataReader dr = scmd.ExecuteReader();
        SqlDataReader dr2 = scmd2.ExecuteReader();

        if (dr.Read())
        if (dr2.Read())
            {
                TextBoxSchool.Text = dr["LongName"].ToString();
                TextBoxLYInstr.Text = dr2["INSTRUCTIO"].ToString();
                TextBoxLYRes.Text = dr2["RESEARCH"].ToString();
                TextBoxLYPubS.Text = dr2["PUBLIC_SER"].ToString();
                TextBoxLYAcad.Text = dr2["ACADEMIC_S"].ToString();
                TextBoxLYStudS.Text = dr2["STUDENT_SE"].ToString();
                TextBoxLYInstiS.Text = dr2["INSTITUTIO"].ToString();
                TextBoxLYOperM.Text = dr2["PHYSICAL_P"].ToString();
                TextBoxLYSFEDA.Text = dr2["SCHOLARSHI"].ToString();
                TextBoxLYAuxE.Text = dr2["AUXILIARY_"].ToString();
                TextBoxLYHosS.Text = dr2["HOSPITALS"].ToString();
                TextBoxLYIndeO.Text = dr2["INDEPENDEN"].ToString();
                TextBoxLYOED.Text = dr2["OTHEREXP"].ToString();
                TextBoxLYTA.Text = dr2["TOTASSETS"].ToString();
                TextBoxLYTL.Text = dr2["TOTLIABILITY"].ToString();
                TextBoxLYNPRNA.Text = dr2["NoNEXPPERMRESASSETS"].ToString();
                TextBoxLYTUNA.Text = dr2["UNRNETASSETS"].ToString();
                TextBoxLYTR.Text = dr2["TOTALREV"].ToString();
                TextBoxLYTFN.Text = dr2["TUITFEES"].ToString();
                TextBoxLYCD.Text = dr2["CURRDEBT"].ToString();
                TextBoxLYLTD.Text = dr2["LONGTERMDEBT"].ToString();
                

            }
        dr.Close();
        con.Close();
        dr2.Close();
        con2.Close();


这篇关于页面加载填充文本框代码检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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