如何在asp.net的dropdownlist中绑定数据库中的选定值

时间:2015-09-11 16:58:31

标签: c# asp.net data-binding

我正在尝试在下拉列表中绑定数据库中的选定项目。我没有在下拉列表中获取用户选择的数据,而是加载所有内容。我需要的是从数据库中获取默认选定值以及其他项目。请帮我克服这个问题。提前感谢你。

存储过程:

CREATE PROCEDURE [dbo].[get_student_details]
  @StudentId int = 0
AS
BEGIN
    SET NOCOUNT ON;

    SELECT      
        dbo.Student.InstituteId, 
        dbo.Student.Institute,
        dbo.Student.Name, 
        dbo.Student.Gender,
        dbo.Student.Age
    FROM         
        dbo.Student 
    WHERE       
        dbo.Student.StudentId = @StudentId
END             

我的.aspx标记:

<asp:DropDownList ID="ddlInstitute" runat="server"></asp:DropDownList>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtGender" runat="server"></asp:TextBox>
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<asp:Button ID="btnPersonalDetails" runat="server"  Text="Search" OnClick="GetStudentDetails"/>

我的代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FillInstitute();
    }                   
}

public void FillInstitute()
{
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "get_Institute";
    cmd.Connection = con;

    try
    {
        con.Open();
        ddlInstitute.DataSource = cmd.ExecuteReader();
        ddlInstitute.DataTextField = "Institute";
        ddlInstitute.DataValueField = "InstituteId";
        ddlInstitute.DataBind();
        ddlInstitute.Items.Insert(0, new ListItem("--Select--", "0"));
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}

public void GetStudentDetails()
{
    studentid= 123;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "get_student_details";
    cmd.Parameters.Add("@StudentId", SqlDbType.Int).Value = studentid;
    cmd.Connection = con;

    try
    {
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            ddlInstitute.DataValueField= dr["InstituteId"].ToString();
            ddlInstitute.DataTextField= dr["Institute"].ToString();
            txtName.Text = dr["Name"].ToString();
            txtGender.Text = dr["Gender"].ToString();
            txtAge.Text = dr["Age"].ToString();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }
}

3 个答案:

答案 0 :(得分:2)

您必须使用SelectedValueDropDownList属性。DataTextFieldDataValueField用于指定DataSource中应将哪些属性用作文字的下拉列表。

替换这些行:

ddlInstitute.DataValueField= dr["InstituteId"].ToString();
ddlInstitute.DataTextField= dr["Institute"].ToString();

使用:

ddlInstitute.SelectedValue= dr["InstituteId"].ToString();

或者您也可以这样做:

ddlInstitute.Items.FindByValue(dr["InstituteId"].ToString()).Selected = true;

您也可以参考this article

答案 1 :(得分:0)

假设您知道要选择哪个ID,请尝试以下操作:

[{x:0, y:0}, {x:0, y:10}, {x:10, y:10}, {x:0, y:10}]

答案 2 :(得分:0)

尝试将其作为一种功能:

void FN_loadBranch()
{
    classname cls = new classname ();

    DataTable dt = new DataTable();
    dt = cls.FUNCTIONNAMEINCLASS(int id);

    dropdown.DataSource = dt;
    dropdown.DataValueField = "valuefield";
    dropdown.DataTextField = "textfield";
    dropdown.DataBind();

    ddlBranch.Items.Insert(0, new ListItem("--Select--", "0"));
}

在功能中:

public DataTable FUNCTIONNAMEINCLASS( int id)
{  
    try    
    {
        using (SqlConnection cn = new SqlConnection(CLASS.ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("[storedprocedure]", cn);

            cn.Open();
            cmd.CommandType = CommandType.StoredProcedure;     

            cmd.Parameters.Add("@ID", SqlDbType.VarChar, 50).Value = id;

            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            sda.Fill(dt);
            return dt;
        }
    }
}

在函数

中使用存储过程