页面加载取决于登录用户。 [英] Page Load depending on logged in user.
本文介绍了页面加载取决于登录用户。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一旦用户登录,我就陷入了如何在表单上加载登录用户数据的问题。在表单上有一个下拉列表,它被数据绑定到数据库。当用户登录网站时,ddl应仅加载该用户的数据。示例:如果用户的ID为1234且数据的ID为1234,那么这是我想在ddl中显示的唯一数据。有没有办法为数据库中的用户执行此操作?
使用系统;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Drawing;
使用 System.Text;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Configuration;
使用 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();返回false);
}
protected void ButtonSubmit_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager。 ConnectionStrings [ PasswordConnectionString]。ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand( 插入TableFIN2013(User_ID,TOTAL_REVE,INSTRUCTIO,RESEARCH,PUBLIC_SER,ACADEMIC_S,STUDENT_SE,INSTITUTIO,PHYSICAL_P,SCHOLARSHI,AUXILIARY_,HOSPITALS,INDEPENDEN,OTHEREXP,TOTASSETS,TOTLIABILITY,NoNEXPPERMRESASSETS,EXPENDABLE,UNRNETASSETS,TOTALREV ,TUITFEES,CURRDEBT,LONGTERMDEBT)值(@ User_ID,@ TOTAL_REVE,@ INSTRUCTIO,@ RESEARCH,@ Public_SASS,@ ACADEMIC_S,@ STUDENT_SE,@ INSTITUTIO,@ PHYSICAL_P,@ SCHOLARSHI,@ AUXILIARY_,@ HEALTHPITALS,@ INDEPENDEN,@OTHEREXP ,@ TOTASSETS,@ TOTLIABILITY,@ NoNEXPPERMRESASSETS,@ EXPENDABLE,@ UNRNETASSETS,@ TOTALREV,@ TUITFEES,@ CURRDEBT,@ LongterMDEBT),con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue( @ User_ID,TextBoxINST_ID.Text );
cmd.Parameters.AddWithValue( @ TOTAL_REVE,TextBoxTROA.Text);
cmd.Parameters.AddWithValue( @ INSTRUCTIO,TextBoxInstr.Text);
cmd.Parameters.AddWithValue( @ RESEARCH,TextBoxRes.Text);
cmd.Parameters.AddWithValue( @ PUBLIC_SER,TextBoxPubS.Text);
cmd.Parameters.AddWithValue( @ ACADEMIC_S,TextBoxAcad.Text);
cmd.Parameters.AddWithValue( @ STUDENT_SE,TextBoxStudS.Text);
cmd.Parameters.AddWithValue( @ INSTITUTIO,TextBoxInstiS.Text);
cmd.Parameters.AddWithValue( @ PHYSICAL_P,TextBoxOperM.Text);
cmd.Parameters.AddWithValue( @ SCHOLARSHI,TextBoxSFEDA.Text);
cmd.Parameters.AddWithValue( @ AUXILIARY _,TextBoxAuxE.Text);
cmd.Parameters.AddWithValue( @ HOSPITALS,TextBoxHosS.Text);
cmd.Parameters.AddWithValue( @ INDEPENDEN,TextBoxIndeO.Text);
cmd.Parameters.AddWithValue( @ OTHEREXP,TextBoxOED.Text);
cmd.Parameters.AddWithValue( @ TOTASSETS,TextBoxTA.Text);
cmd.Parameters.AddWithValue( @ TOTLIABILITY,TextBoxTL.Text);
cmd.Parameters.AddWithValue( @NoNEXPPERMRESASSETS,TextBoxNPRNA.Text);
cmd.Parameters.AddWithValue( @ EXPENDABLE,TextBoxETRNA.Text);
cmd.Parameters.AddWithValue( @ UNRNETASSETS,TextBoxTUNA.Text);
cmd.Parameters.AddWithValue( @ TOTALREV,TextBoxTR.Text);
cmd.Parameters.AddWithValue( @ TUITFEES,TextBoxTFN.Text);
cmd.Parameters.AddWithValue( @ CURRDEBT,TextBoxCD.Text);
cmd.Parameters.AddWithValue( @ LONGTERMDEBT,TextBoxLTD.Text);
con.Open();
cmd.ExecuteNonQuery();
}
受保护 void DropDownListSchool_SelectedIndexChanged1( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager。 ConnectionStrings [ PasswordConnectionString]。ConnectionString);
con.Open();
的SqlConnection CON2 = <跨度类= 代码关键字>新跨度>的SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings [<跨度类= 代码串>跨度> PasswordConnectionString]。ConnectionString);
con2.Open();
SqlCommand scmd = new SqlCommand( 从TableCOCINST中选择User_ID,其中User_ID = + DropDownListSchool.SelectedValue.ToString(),con);
SqlCommand scmd2 = new SqlCommand( 选择TableFIN2012中的User_ID,INSTRUCTIO,RESEARCH,PUBLIC_SER,ACADEMIC_S,STUDENT_SE,INSTITUTIO,PHYSICAL_P,SCHOLARSHI,AUXILIARY_,PHYSOS_PE,INDEXARSHI,AOTILIARY_,HOTPITALS,INDEPENDEN,OTHEREXP,TOTASSETS,TOTLIABILITY,NoNEXPPERMRESASSETS,UNRNETASSETS,TOTALREV,TUITFEES,CURRDEBT,LONGTERMDEBT其中User_ID = + DropDownListSchool.SelectedValue.ToString(),con2);
SqlDataReader dr = scmd.ExecuteReader();
SqlDataReader dr2 = scmd2.ExecuteReader();
if (dr.Read())
if (dr2.Read( ))
{
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();
{
}
}
}
解决方案
< blockquote> Hello Kwesi,
这是一个小片段,它将向您展示如何完成它。我假设登录用户的ID在会话中通过名为 USER_ID
的密钥可用。
protected void Page_Load( object sender,EventArgs e){
ButtonPrint.Attributes.Add( onclick, window.print();返回false);
fillDropdown();
}
受保护 void fillDropdown(){
字符串 strSQL = SELECT * FROM YOUR_TABLE WHERE USER_ID = @user_id跨度>;
字符串 strCon = ConfigurationManager.ConnectionStrings [ PasswordConnectionString跨度>]的ConnectionString。
使用(DataTable table = new DataTable( YOUR_TABLE)){
使用(SqlConnection sqlConn = new SqlConnection(strCon)){
使用(SqlCommand cmd = new SqlCommand()){
cmd.CommandText = strSQL;
cmd.Parameters.Add( @ user_id,SqlDbType.VarChar, 20 )。值=会话[ USER_ID] ;
使用(SqlDataAdapter da = new SqlDataAdapter(cmd)){
da。补(表);
yourDDL.DataSource = dt;
yourDDL.DataTextField = DISPLAY_FIELD_NAME;
yourDDL.DataValueField = VALUE_FIELD_NAME;
yourDDL.DataBind();
}
}
}
}
}
问候,
I am stuck on how to get a logged in user data to load on a form once the user has logged in. On the form there is a dropdownlist that is databound to a database. When a user logs on the the website the ddl should load only the data for that user. Example: If the user haa an ID of 1234 and the data has an ID of 1234, that is the only data I want to show in the ddl. Is there a way of doing this for users that are in a database?
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
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");
}
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into TableFIN2013 (User_ID, TOTAL_REVE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, SCHOLARSHI, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT) values (@User_ID, @TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @SCHOLARSHI, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LONGTERMDEBT)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@User_ID", TextBoxINST_ID.Text);
cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTROA.Text);
cmd.Parameters.AddWithValue("@INSTRUCTIO", TextBoxInstr.Text);
cmd.Parameters.AddWithValue("@RESEARCH", TextBoxRes.Text);
cmd.Parameters.AddWithValue("@PUBLIC_SER", TextBoxPubS.Text);
cmd.Parameters.AddWithValue("@ACADEMIC_S", TextBoxAcad.Text);
cmd.Parameters.AddWithValue("@STUDENT_SE", TextBoxStudS.Text);
cmd.Parameters.AddWithValue("@INSTITUTIO", TextBoxInstiS.Text);
cmd.Parameters.AddWithValue("@PHYSICAL_P", TextBoxOperM.Text);
cmd.Parameters.AddWithValue("@SCHOLARSHI", TextBoxSFEDA.Text);
cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOED.Text);
cmd.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
cmd.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
cmd.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
cmd.Parameters.AddWithValue("@EXPENDABLE", TextBoxETRNA.Text);
cmd.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
cmd.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
cmd.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
cmd.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
cmd.Parameters.AddWithValue("@LONGTERMDEBT", TextBoxLTD.Text);
con.Open();
cmd.ExecuteNonQuery();
}
protected void DropDownListSchool_SelectedIndexChanged1(object sender, EventArgs e)
{
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 User_ID from TableCOCINST where User_ID = " + DropDownListSchool.SelectedValue.ToString(), con);
SqlCommand scmd2 = new SqlCommand("Select User_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 User_ID = " + DropDownListSchool.SelectedValue.ToString(), con2);
SqlDataReader dr = scmd.ExecuteReader();
SqlDataReader dr2 = scmd2.ExecuteReader();
if (dr.Read())
if (dr2.Read())
{
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();
{
}
}
}
解决方案
Hello Kwesi,
Here is a small snippet which will show you how it can be done. I am assuming that logged in user's id is available in session via key namedUSER_ID
.
protected void Page_Load(object sender, EventArgs e) { ButtonPrint.Attributes.Add("onclick", "window.print(); return false"); fillDropdown(); } protected void fillDropdown() { String strSQL = "SELECT * FROM YOUR_TABLE WHERE USER_ID = @user_id"; String strCon = ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString; using (DataTable table = new DataTable("YOUR_TABLE")) { using (SqlConnection sqlConn = new SqlConnection(strCon)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = strSQL; cmd.Parameters.Add("@user_id", SqlDbType.VarChar, 20).Value = Session["USER_ID"]; using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(table); yourDDL.DataSource = dt; yourDDL.DataTextField = "DISPLAY_FIELD_NAME"; yourDDL.DataValueField = "VALUE_FIELD_NAME"; yourDDL.DataBind(); } } } } }
Regards,
这篇关于页面加载取决于登录用户。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文