将列表框1中的值绑定到listbox2,就像一对多关系一样 [英] Binding the values from listbox 1 to listbox2,like a one to many relationship
问题描述
我现在正在开展一个项目,我需要将一个在checkedControlBox 2中填充的学生分配到一个填充在checkedControlBox1中的讲师,就像一对多的关系一样......我不知道该怎么做有效的,我会很感激的答案..谢谢
这是我尝试过但不保存到数据库的东西,checkedListBoxControl2填充了讲师来自tbl_CSLecturer和checkedListBoxControl1填充来自tbl_Student的学生
用于按钮点击事件我有
尝试
{
PMSLogic代码= 新 PMSLogic();
// string userId = main.userId;
foreach ( int i this .checkedListBoxControl2.SelectedIndices)
{
int 讲授ID = Convert.ToInt32(checkedListBoxControl2.GetItemValue(i));
code.DeleteBindLecturerBylecturerId(讲授ID);
foreach ( int j in this .checkedListBoxControl1.SelectedIndices)
{
int studentId = Convert.ToInt32 (checkedListBoxControl1.GetItemValue(J));
tbl_Allocation tblalloc = new tbl_Allocation();
tblalloc.SessionID = Convert.ToInt32( this .comboBoxEx2.SelectedValue);
tblalloc.LecturerID =讲授;
tblalloc.StudentID = studentId;
tblalloc.DateAllocated = System.DateTime.Now;
}
}
RadMessageBox.SetThemeName( Aqua< /跨度>);
RadMessageBox.Show( 主管成功分配给选定的学生, 将Lecturer绑定到学生,MessageBoxButtons.OK,RadMessageIcon.Info,MessageBoxDefaultButton.Button1);
}
catch (例外情况)
{
}
private void checkedListBoxControl2_SelectedIndexChanged(对象发件人,EventArgs e)
{
尝试
{
PMSLogic代码= new PMSLogic();
// int roleId =
// DataTable ItemRoleT = this.checkedListBoxControl2.SelectedIndex.ToString();
for ( int i = 0 ; i < checkedListBoxControl1.ItemCount; i ++)
{
this .checkedListBoxControl1.SetSelected(i,假跨度>);
}
如果(ItemRoleT.Rows.Count > 0 )
{
foreach (DataRow row in ItemRoleT.Rows)
{
string itemText = row.ItemArray [ 1 < /跨度>]的ToString();
int menuCount = this .checkedListBoxControl1.ItemCount;
for ( int i = 0 ; i < menuCount; i ++)
{
string listText = checkedListBoxControl1.GetItemText(ⅰ);
if (itemText.Equals(listText))
{
this .checkedListBoxControl1.SetSelected(i, true );
}
}
}
}
其他
{
for ( int i = 0 ; i < checkedListBoxControl1.ItemCount; i ++)
{
this .checkedListBoxControl1.SetSelected(i , false );
}
}
}
catch (例外情况)
{}
}
}
你好朋友,我想提供一个解决方案,据我了解的要求。我相信除了 tbl_Lecturer 和 tbl_Student 表之外,您已经引入了第三个表来保存一对多关系。假设新表名是 tbl_Lecturer_Student 。
我还假设一次可以选择一个讲师ListBox
控件比Lecturer的CheckBoxList
控件更好。CheckBoxList
适合学生。
最后的假设,并非最不重要,此UI将用于保存关系讲师和学生之间。
我们走了:
< span class =code-keyword>< table style = width:50%; border = 1 >
< tr >
< th > 讲师:< / th >
< th > 学生:< / th >
< / tr >
< tr >
< span class =code-keyword>< td >
< asp:Panel runat = server ScrollBars = 垂直 高度 = 150px >
< asp: ListBox ID = lstLecturer runat = server 宽度 = 1 00% >
< asp:ListItem 值 = 1 > Lecturer1 < / asp:ListItem >
< asp:ListItem 值 = 2 > Lecturer2 < / asp:ListItem >
< asp:ListItem 值 = 3 > Lecturer3 < / asp:ListItem >
< asp:ListItem 值 = 4 > Lecturer4 < / asp:ListItem >
< asp:ListItem 值 = 5 > Lecturer5 < / asp:ListItem >
< asp:ListItem 值 = 6 > Lecturer6 < / asp:ListItem >
< / asp:ListBox >
< span class =code-keyword>< / asp:Panel >
< / td >
< td style = height:100px; >
<! - 添加了面板以显示垂直滚动条 - >
< asp:Panel runat = server ScrollBars = 垂直 高度 = 150px >
< asp:CheckBoxList ID = cblStudent runat = server 宽度 = 100% >
< asp:ListItem 值 = 1 > Student-1 < / asp:ListItem >
< asp:ListItem 值 = 2 > Student-2 < / asp:ListItem >
< asp:ListItem 值 = 3 > 学生-3 < span class =code-keyword>< / asp:ListItem >
< asp:ListItem 值 = 4 > Student-4 < / asp:ListItem >
< asp:ListItem 值 = 5 > Student-5 < / asp:ListItem >
< asp:ListItem 值 = 6 > Student-6 < < span class =code-leadattribute> / asp:ListItem >
< asp:ListItem 值 < span class =code-keyword> = 7 > Student-7 < / asp:ListItem >
< asp:ListItem 值 = 8 > Student-8 < / asp:ListItem >
< asp:ListItem 值 = 9 > Student-9 < / asp:ListItem >
< asp:ListItem Value = 10 > Student-10 < / asp:ListItem >
< / asp:CheckBoxList >
< / asp:Panel >
< / td >
< / tr >
< / table >
< p >
< asp :按钮 ID = btnSave runat = server 文字 = 保存 OnClick = btnSave_Click / < span class =code-keyword>>
< / p
protected void btnSave_Click( object sender,EventArgs e)
{
foreach (ListItem讲师 in lstLecturer.Items)
{
if (讲师选择== true )
{
// 调用方法从LecturerID删除tbl_Lecturer_Student中的所有现有条目
// DeleteLecturerStudentRelationByLecturerID (讲师。价值)
foreach (ListItem student in cblStudent.Items)
{
if (student.Selected == true )
{
// 插入讲师的代码 - 表格中的学生关系
// InsertLecturerStudentRelation(lecturer.Value,student.Value);
}
}
break ; // 一次只能选择一位讲师
}
}
}
如果您有任何进一步的疑问,请告诉我。我很乐意为您提供帮助:)
或者,您可以访问以下文章:
在ASP.NET中实现一对多关系 [ ^ ]
I am working on a project right now,and i need to allocate students populated in a checkedControlBox 2 to a lecturer populated in a checkedControlBox1,like a one to many relationship...please i don't know how to do this effectively,i will appreciate answers..thanks
this is something i have tried but its not saving to the database,checkedListBoxControl2 populates lecturers from tbl_CSLecturer and checkedListBoxControl1 populates students from tbl_Student
for the button click event i have
try
{
PMSLogic code = new PMSLogic();
//string userId = main.userId;
foreach (int i in this.checkedListBoxControl2.SelectedIndices)
{
int lecturerID = Convert.ToInt32(checkedListBoxControl2.GetItemValue(i));
code.DeleteBindLecturerBylecturerId(lecturerID);
foreach (int j in this.checkedListBoxControl1.SelectedIndices)
{
int studentId = Convert.ToInt32(checkedListBoxControl1.GetItemValue(j));
tbl_Allocation tblalloc = new tbl_Allocation();
tblalloc.SessionID = Convert.ToInt32(this.comboBoxEx2.SelectedValue);
tblalloc.LecturerID = lecturerID;
tblalloc.StudentID = studentId;
tblalloc.DateAllocated = System.DateTime.Now;
}
}
RadMessageBox.SetThemeName("Aqua");
RadMessageBox.Show("Supervisor Successfully Allocated to Selected Student(s)", "Bind Lecturer To Student", MessageBoxButtons.OK, RadMessageIcon.Info, MessageBoxDefaultButton.Button1);
}
catch (Exception ex)
{
}
private void checkedListBoxControl2_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
PMSLogic code = new PMSLogic();
//int roleId =
//DataTable ItemRoleT = this.checkedListBoxControl2.SelectedIndex.ToString();
for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
{
this.checkedListBoxControl1.SetSelected(i, false);
}
if (ItemRoleT.Rows.Count > 0)
{
foreach (DataRow row in ItemRoleT.Rows)
{
string itemText = row.ItemArray[1].ToString();
int menuCount = this.checkedListBoxControl1.ItemCount;
for (int i = 0; i < menuCount; i++)
{
string listText = checkedListBoxControl1.GetItemText(i);
if (itemText.Equals(listText))
{
this.checkedListBoxControl1.SetSelected(i, true);
}
}
}
}
else
{
for (int i = 0; i < checkedListBoxControl1.ItemCount; i++)
{
this.checkedListBoxControl1.SetSelected(i, false);
}
}
}
catch (Exception ex)
{ }
}
}
Hello friend, I'm trying to provide a solution as far as I understood the requirement. I believe apart from tbl_Lecturer and tbl_Student tables, you have introduced a third table to hold the one-to-many relationship. Let's say the new table name is tbl_Lecturer_Student.
I also assume that one Lecturer can be selected at a time henceListBox
control would be better choice than aCheckBoxList
control for Lecturer.CheckBoxList
is fine for Students.
Last assumption and not the least, this UI would be used for Saveing relationship among Lecturer and Students.
Here we go:
<table style="width: 50%;" border="1"> <tr> <th>Lecturer:</th> <th>Student:</th> </tr> <tr> <td> <asp:Panel runat="server" ScrollBars="Vertical" Height="150px"> <asp:ListBox ID="lstLecturer" runat="server" Width="100%"> <asp:ListItem Value="1">Lecturer1</asp:ListItem> <asp:ListItem Value="2">Lecturer2</asp:ListItem> <asp:ListItem Value="3">Lecturer3</asp:ListItem> <asp:ListItem Value="4">Lecturer4</asp:ListItem> <asp:ListItem Value="5">Lecturer5</asp:ListItem> <asp:ListItem Value="6">Lecturer6</asp:ListItem> </asp:ListBox> </asp:Panel> </td> <td style="height: 100px;"> <!-- added Panel to show vertical scrollbar --> <asp:Panel runat="server" ScrollBars="Vertical" Height="150px"> <asp:CheckBoxList ID="cblStudent" runat="server" Width="100%"> <asp:ListItem Value="1">Student-1</asp:ListItem> <asp:ListItem Value="2">Student-2</asp:ListItem> <asp:ListItem Value="3">Student-3</asp:ListItem> <asp:ListItem Value="4">Student-4</asp:ListItem> <asp:ListItem Value="5">Student-5</asp:ListItem> <asp:ListItem Value="6">Student-6</asp:ListItem> <asp:ListItem Value="7">Student-7</asp:ListItem> <asp:ListItem Value="8">Student-8</asp:ListItem> <asp:ListItem Value="9">Student-9</asp:ListItem> <asp:ListItem Value="10">Student-10</asp:ListItem> </asp:CheckBoxList> </asp:Panel> </td> </tr> </table> <p> <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" /> </p
protected void btnSave_Click(object sender, EventArgs e) { foreach (ListItem lecturer in lstLecturer.Items) { if (lecturer.Selected == true) { // call method to delete all the existing entries from tbl_Lecturer_Student by LecturerID // DeleteLecturerStudentRelationByLecturerID(lecturer.Value) foreach (ListItem student in cblStudent.Items) { if (student.Selected == true) { // code to insert Lecturer - Student relation in a table // InsertLecturerStudentRelation(lecturer.Value, student.Value); } } break; // there could be one lecturer selected at a time } } }
Let me know if you have any further query. I'll be happy to help you :)
Alternatively, you may visit the below article:
Implementing One-To-Many Relationship in ASP.NET[^]
这篇关于将列表框1中的值绑定到listbox2,就像一对多关系一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!