0
我是新來的ASP.NET,我不知道如何使用列表框。我試圖從我的數據庫中獲取數據,將其用作列表框的項目。我被告知最好在我的情況下使用列表視圖,但我也不知道如何去做。我試着在YouTube上看教程課,並在谷歌搜索它,但它並沒有幫助我。從數據庫中獲取數據並將其用作列表框的項目。 (ASP.NET)
我的目標是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 [email protected], " +
"[email protected], [email protected], [email protected], " +
"[email protected], [email protected], [email protected], " +
"[email protected] WHERE [email protected]";
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>