ASP.NET は初めてで、リストボックスの使い方がわかりません。データベースからデータを取得して、リストボックスの項目として使用しようとしています。私の場合、リストビューを使用する方が良いと言われましたが、その方法もわかりません。YouTube でチュートリアル レッスンを視聴したり、Google で検索したりしましたが、役に立ちませんでした。
私の目標は次のとおりです。 -ユーザーのデータをプロフィールに表示します。- ユーザーがリストボックスで複数のアイテムを選択できるようにします。-プロフィールと、ユーザーのプロフィールに表示される選択されたアイテムを更新します。
私はこれを1週間やろうとしています。だから、私を助けてくれる人に感謝します。
これは、私の 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>