根据用户ID填充数据绑定DropDownList [英] Populating a Databound DropDownList depending on User ID

查看:53
本文介绍了根据用户ID填充数据绑定DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个数据绑定下拉列表的表单。第一个ddl是学校名称,另一个是一年。我想要做的是使用用户ID匹配的数据填充数据绑定ddl。另外,有没有办法只在ddl中有一年,当选择它将填充我有的文本框?我的意思是,当年ddl是数据绑定的,它有200个用户的2012年。有没有办法让我在ddl中拥有一个2012而且它是ddl学校的用户ID?



I have a form that has two dropdownlists that are databound. The first ddl is school names and the other has a year in it. What I am trying to do is to populate the databound ddl with just the data that the User ID matches. Also, is there a way to just have one year in the ddl that when selected it will populate the textboxes I have? What I mean is that the year ddl is databound and it has 2012 in it for 200 users. Is there a way I can just have one 2012 in that ddl and it goes by the User ID that the ddl school does?

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing.Printing;
using System.Web.SessionState;

public partial class FinancialProfileFormC : 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 (INST_ID, TOTAL_REVE, INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, EXPENDABLE, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LOMGTERMDEBT) values (@INST_ID, @TOTAL_REVE, @INSTRUCTIO, @RESEARCH, @PUBLIC_SER, @ACADEMIC_S, @STUDENT_SE, @INSTITUTIO, @PHYSICAL_P, @NET_AID, @AUXILIARY_, @HOSPITALS, @INDEPENDEN, @OTHEREXP, @TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @EXPENDABLE, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LOMGTERMDEBT)", con);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@TOTAL_REVE", TextBoxTRIR.Text);
        cmd.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.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("@NET_AID", TextBoxNGAS.Text);
        cmd.Parameters.AddWithValue("@AUXILIARY_", TextBoxAuxE.Text);
        cmd.Parameters.AddWithValue("@HOSPITALS", TextBoxHosS.Text);
        cmd.Parameters.AddWithValue("@INDEPENDEN", TextBoxIndeO.Text);
        cmd.Parameters.AddWithValue("@OTHEREXP", TextBoxOE.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("@LOMGTERMDEBT", TextBoxLTD.Text);

        con.Open();
        cmd.ExecuteNonQuery();
    }
    protected void ButtonPrint_Click(object sender, EventArgs e)
    {

    }

    protected void DropDownListSchool_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();

        SqlCommand scmd = new SqlCommand("Select INST_ID from TableCOCINST where INST_ID = " + DropDownListSchool.SelectedValue.ToString(), con);
        SqlDataReader dr = scmd.ExecuteReader();
        if (dr.Read())
        {
            
            TextBoxINST_ID.Text = dr["INST_ID"].ToString();
        }
        

        dr.Close();
        con.Close(); 
   }
    protected void DropDownListLY_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
       con.Open();

       SqlCommand scmd = new SqlCommand("Select INSTRUCTIO, RESEARCH, PUBLIC_SER, ACADEMIC_S, STUDENT_SE, INSTITUTIO, PHYSICAL_P, NET_AID, AUXILIARY_, HOSPITALS, INDEPENDEN, OTHEREXP, TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LONGTERMDEBT from TableFIN2012 where INST_ID = " + DropDownListSchool.SelectedValue.ToString(), con);
       SqlDataReader dr = scmd.ExecuteReader();
       if (dr.Read())
       {
           TextBoxLYInstr.Text = dr["INSTRUCTIO"].ToString();
           TextBoxLYRes.Text = dr["RESEARCH"].ToString();
           TextBoxLYPubS.Text = dr["PUBLIC_SER"].ToString();
           TextBoxLYAcad.Text = dr["ACADEMIC_S"].ToString();
           TextBoxLYStudS.Text = dr["STUDENT_SE"].ToString();
           TextBoxLYInstiS.Text = dr["INSTITUTIO"].ToString();
           TextBoxLYOperM.Text = dr["PHYSICAL_P"].ToString();
           TextBoxLYNGAS.Text = dr["NET_AID"].ToString();
           TextBoxLYAuxE.Text = dr["AUXILIARY_"].ToString();
           TextBoxLYHosS.Text = dr["HOSPITALS"].ToString();
           TextBoxLYIndeO.Text = dr["INDEPENDEN"].ToString();
           TextBoxLYOE.Text = dr["OTHEREXP"].ToString();
           TextBoxLYTA.Text = dr["TOTASSETS"].ToString();
           TextBoxLYTL.Text = dr["TOTLIABILITY"].ToString();
           TextBoxLYNPRNA.Text = dr["NoNEXPPERMRESASSETS"].ToString();
           TextBoxLYTUNA.Text = dr["UNRNETASSETS"].ToString();
           TextBoxLYTR.Text = dr["TOTALREV"].ToString();
           TextBoxLYTFN.Text = dr["TUITFEES"].ToString();
           TextBoxLYCD.Text = dr["CURRDEBT"].ToString();
           TextBoxLYLTD.Text = dr["LONGTERMDEBT"].ToString();

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

    }
}

ASPX CODE:

Ok, sorry. I had the wrong code. I didn't read the question carefully. Here is the code I think you need. 
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:PasswordConnectionString %>" 
                    SelectCommand="SELECT [INST_ID], [LongName] FROM [TableCOCINST]">
                </asp:SqlDataSource>

推荐答案

ConnectionStrings:PasswordConnectionString%>
SelectCommand =SELECT [INST_ID],[LongName] FROM [TableCOCINST]>
< / asp:SqlDataSource>
ConnectionStrings:PasswordConnectionString %>" SelectCommand="SELECT [INST_ID], [LongName] FROM [TableCOCINST]"> </asp:SqlDataSource>


如果您想将会话条件添加到 DropDownList DropDownListSchool ,那么请执行以下操作...

If you want to add one Session condition to DropDownList "DropDownListSchool", then do like below...
<asp:SqlDataSource ID="SqlDataSource1" runat="server"

            ConnectionString="<%


ConnectionStrings:PasswordConnectionString % >

SelectCommand = SELECT * FROM [TableCOCINST] WHERE UserID =?
ConnectionStrings:PasswordConnectionString %>" SelectCommand="SELECT * FROM [TableCOCINST] WHERE UserID = ?"





现在添加一个选择参数如下...



Now add one Select Parameter like below...

<SelectParameters>
  <asp:SessionParameter

    Name="UserID"

    SessionField="UserID"

    DefaultValue="5" />
</SelectParameters>





注意 - 我不知道表结构,所以我假设你在这个表中有一个 UserID 。你需要修改它。

但这是添加一个 Session 参数条件的技巧。



参考 - SessionParameter Class [ ^ ]



Note- I don't know the Table structure, so I just assume that you have a UserID in this table. You need to modify that.
But this is trick to add one Session parameter condition.

Reference- SessionParameter Class[^]


这篇关于根据用户ID填充数据绑定DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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