0

我是 ASP.NET 的新手,我不知道如何使用列表框。我正在尝试从我的数据库中获取数据以将其用作我的列表框的项目。有人告诉我最好在我的情况下使用列表视图,但我也不知道该怎么做。我尝试在 youtube 上观看教程课程并在 google 上搜索它,但它对我没有帮助。

我的目标是 ff: - 在他/她的个人资料上显示用户的数据。- 让用户能够在列表框中选择多个项目。- 更新要在用户的个人资料上显示的个人资料和选择的项目。

我已经尝试这样做了一个星期。所以,非常感谢任何能帮助我的人。

这是我的 aspx cs 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(Helper.GetConnection());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ID"] != null)
        {
            if (!IsPostBack)
            {
                GetCourse();
                GetSection();
                GetSchool();
                GetInfo(Session["ID"].ToString());
            }
        }
        else
        {
            //
        }
    }

    void GetCourse()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT CourseName FROM Course";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "Course");
        DataTable myDataTable = ds.Tables[0];
        DataRow tempRow = null;

        foreach (DataRow tempRow_Variable in myDataTable.Rows)
        {
            tempRow = tempRow_Variables;
            lbCourse.Items.Add((tempRow["CourseName"] + ","));
        }
        con.Close();
    }

    void GetSection()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT Name FROM Section";
        SqlDataReader dr = cmd.ExecuteReader();
        lbSection.DataSource = dr;
        lbSection.DataTextField = "Name";
        lbSection.DataBind();
        con.Close();
    }

    void GetSchool()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT SchoolName FROM School";
        SqlDataReader dr = cmd.ExecuteReader();
        ddlSchool.DataSource = dr;
        ddlSchool.DataTextField = "SchoolName";
        ddlSchool.DataBind();
        con.Close();
    }

    void GetInfo(string ID)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "SELECT SchoolID, LastName, FirstName, " +
            "MI, Address, CourseName, Name, SchoolName FROM Profile";
        cmd.Parameters.Add("@ProfileID", SqlDbType.Int).Value = ID;
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            txtID.Text = dr["SchoolID"].ToString();
            txtLN.Text = dr["LastName"].ToString();
            txtFN.Text = dr["FirstName"].ToString();
            txtMI.Text = dr["MI"].ToString();
            txtAddress.Text = dr["Address"].ToString();
            lbCourse.SelectedValue = dr["CourseName"].ToString();
            lbSection.SelectedValue = dr["Name"].ToString();
            ddlSchool.SelectedValue = dr["SchoolName"].ToString();
        }
        con.Close();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "UPDATE Profile SET SchoolID=@SchoolID, " +
            "LastName=@LastName, FirstName=@FirstName, MI=@MI, " +
            "Address=@Address, CourseName=@CourseName, Name=@Name, " +
            "SchoolName=@SchoolName WHERE ProfileID=@ProfileID";
        cmd.Parameters.Add("@SchoolID", SqlDbType.Int).Value =
            txtID.Text;
        cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value =
            txtLN.Text;
        cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value =
            txtFN.Text;
        cmd.Parameters.Add("@MI", SqlDbType.VarChar).Value =
            txtMI.Text;
        cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value =
            txtAddress.Text;
        cmd.Parameters.Add("@CourseName", SqlDbType.VarChar).Value =
            lbCourse.SelectedValue;
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value =
            lbSection.SelectedValue;
        cmd.Parameters.Add("@SchoolName", SqlDbType.VarChar).Value =
            ddlSchool.SelectedValue;
        cmd.Parameters.Add("@ProfileID", SqlDbType.Int).Value =
            Session["ID"].ToString();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnRight1_Click(object sender, EventArgs e)
    {
        for (int i = lbCourse.Items.Count - 1; i >= 0; i--)
        {
            if (lbCourse.Items[i].Selected == true)
            {
                ListBox1.Items.Add(lbCourse.Items[i]);
                ListItem li = lbCourse.Items[i];
                lbCourse.Items.Remove(li);
            }
        }
    }
    protected void btnLeft1_Click(object sender, EventArgs e)
    {
        for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox1.Items[i].Selected == true)
            {
                lbCourse.Items.Add(ListBox1.Items[i]);
                ListItem li = ListBox1.Items[i];
                ListBox1.Items.Remove(li);
            }
        }
    }
    protected void btnAll1_Click(object sender, EventArgs e)
    {
        IEnumerator ie = lbCourse.Items.GetEnumerator();
        while (ie.MoveNext())
        {
            ListItem li = (ListItem)ie.Current;
            li.Selected = true;
        }
    }
    protected void btnRight2_Click(object sender, EventArgs e)
    {
        for (int i = lbSection.Items.Count - 1; i >= 0; i--)
        {
            if (lbSection.Items[i].Selected == true)
            {
                ListBox2.Items.Add(lbSection.Items[i]);
                ListItem li = lbSection.Items[i];
                lbSection.Items.Remove(li);
            }
        }
    }
    protected void btnLeft2_Click(object sender, EventArgs e)
    {
        for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
        {
            if (ListBox2.Items[i].Selected == true)
            {
                lbSection.Items.Add(ListBox2.Items[i]);
                ListItem li = ListBox2.Items[i];
                ListBox2.Items.Remove(li);
            }
        }
    }
    protected void btnAll2_Click(object sender, EventArgs e)
    {
        IEnumerator ie = lbSection.Items.GetEnumerator();
        while (ie.MoveNext())
        {
            ListItem li = (ListItem)ie.Current;
            li.Selected = true;
        }
    }

    public DataRow tempRow_Variables { get; set; }}

这是我的 aspx 代码:

<form id="Form1" runat="server" class="form-horizontal">
        <div class="control-group">
        <label class="control-label">School ID</label>
            <div class="controls">
                <asp:TextBox ID="txtID" runat="server" MaxLength="11"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvID" runat="server" 
                    ErrorMessage="*" ControlToValidate="txtID" SetFocusOnError="true"></asp:RequiredFieldValidator>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label">Last Name</label>
            <div class="controls">
                <asp:TextBox ID="txtLN" runat="server" MaxLength="50"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvLN" runat="server" 
                    ErrorMessage="*" ControlToValidate="txtLN" SetFocusOnError="true"></asp:RequiredFieldValidator>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label">First Name</label>
            <div class="controls">
                <asp:TextBox ID="txtFN" runat="server" MaxLength="50"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvFN" runat="server" 
                ErrorMessage="*" ControlToValidate="txtFN" SetFocusOnError="true"></asp:RequiredFieldValidator>
            </div>                
        </div>

        <div class="control-group">
        <label class="control-label">MI</label>
            <div class="controls">
                <asp:TextBox ID="txtMI" runat="server" MaxLength="50"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvMI" runat="server" 
                    ErrorMessage="*" ControlToValidate="txtMI" SetFocusOnError="true"></asp:RequiredFieldValidator>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label">Address</label>
            <div class="controls">
                <asp:TextBox ID="txtAddress" runat="server" MaxLength="50"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvAddress" runat="server" 
                    ErrorMessage="*" ControlToValidate="txtAddress" SetFocusOnError="true"></asp:RequiredFieldValidator>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label">Course</label>
            <div class="controls">
                <asp:ListBox ID="lbCourse" runat="server" SelectionMode="Multiple"></asp:ListBox>
                <asp:Button ID="btnRight1" runat="server" Text=">>" onclick="btnRight1_Click" />
                <asp:Button ID="btnLeft1" runat="server" Text="<<" onclick="btnLeft1_Click" />
                <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>
                <asp:Button ID="btnAll1" runat="server" Text="Select All"
                    onclick="btnAll1_Click" />
            </div>
        </div>

        <div class="control-group">
        <label class="control-label">Section</label>
            <div class="controls">
                <asp:ListBox ID="lbSection" runat="server" SelectionMode="Multiple"></asp:ListBox>
                <asp:Button ID="btnRight2" runat="server" Text=">>" onclick="btnRight2_Click"/>
                <asp:Button ID="btnLeft2" runat="server" Text="<<" onclick="btnLeft2_Click"/>
                <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple"></asp:ListBox>
                <asp:Button ID="btnAll2" runat="server" Text="Select All" 
                    onclick="btnAll2_Click"/>
            </div>
        </div>

        <div class="control-group">
        <label class="control-label">School</label>
            <div class="controls">
                <asp:DropDownList ID="ddlSchool" runat="server">
                </asp:DropDownList>
            </div>
        </div>

        <p></p>
            <asp:Button ID="btnUpdate" runat="server" Text="Update"
                class="btn btn-success" OnClick="btnUpdate_Click" /> 
    </form>
4

1 回答 1

0

尝试这个:

lbCourse.Items.Add(new ListItem(tempRow["CourseName"].ToString()))
于 2013-08-16T13:22:13.473 回答