小丫头的随笔。。。
页面设计中有一个下拉列表(DropDownList)和多选框列表(CheckBoxList)以及一个可以提供显示的表格(GridView)。当选择下拉列表中具体的值时,进行对多选框的选择,在多选框中挑上沟。点击保存在GridView中显示一条数据。
aiqinhai.aspx如下:
<asp:DropDownList ID="ddl_aiqinhai" runat="server" CssClass="DropDownList" AutoPostBack="True" OnSelectedIndexChanged="ddl_aiqinhai_SelectedIndexChanged">
</asp:DropDownList>
</asp:DropDownList>
<asp:Panel ID="Panel1" runat="server" Height="150px" ScrollBars="Vertical" Width="80%">
<asp:CheckBoxList ID="cbl_aiqinhai" runat="server" Width="80%">
</asp:CheckBoxList></asp:Panel>
<asp:CheckBoxList ID="cbl_aiqinhai" runat="server" Width="80%">
</asp:CheckBoxList></asp:Panel>
aiqinhai.aspx.cs 如下:
/// <summary>
/// 下拉列表的绑定
/// </summary>
private void Bind_dll_aiqinhai()
{
Aiqinhai_BLL aqh= new Aiqinhai_BLL();
DataTable DT = bll.GetName_B();
ddl_aiqinhai.DataSource = DT;
ddl_aiqinhai.DataTextField = "Name";
ddl_aiqinhai.DataValueField = "PK_UserID";
ddl_aiqinhai.DataBind();
}
/// 下拉列表的绑定
/// </summary>
private void Bind_dll_aiqinhai()
{
Aiqinhai_BLL aqh= new Aiqinhai_BLL();
DataTable DT = bll.GetName_B();
ddl_aiqinhai.DataSource = DT;
ddl_aiqinhai.DataTextField = "Name";
ddl_aiqinhai.DataValueField = "PK_UserID";
ddl_aiqinhai.DataBind();
}
/// <summary>
/// 多选框的绑定
/// </summary>
private void Bind_cbl_PostName()
{
Aiqinhai_BLL bll = new Aiqinhai_BLL();
DataTable DT = bll.GetPost_B();
cbl_aiqinhai.DataSource = DT;
cbl_aiqinhai.DataTextField = "PostName";
cbl_aiqinhai.DataValueField = "PK_PostID";
cbl_aiqinhai.DataBind();
}
/// 多选框的绑定
/// </summary>
private void Bind_cbl_PostName()
{
Aiqinhai_BLL bll = new Aiqinhai_BLL();
DataTable DT = bll.GetPost_B();
cbl_aiqinhai.DataSource = DT;
cbl_aiqinhai.DataTextField = "PostName";
cbl_aiqinhai.DataValueField = "PK_PostID";
cbl_aiqinhai.DataBind();
}
#region 下拉列表框的事件
public DataTable GetPostUserID(string UserId)
{
Post_BLL bll = new Post_BLL();
return bll.GetPostUser_B(UserId);
}
public DataTable GetPostUserID(string UserId)
{
Post_BLL bll = new Post_BLL();
return bll.GetPostUser_B(UserId);
}
protected void ddl_aiqinhai_SelectedIndexChanged(object sender, EventArgs e)
{
Bind_cbl_PostName();
DataTable dt = GetPostUserID(this.ddl_aiqinhai.SelectedValue);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < cbl_aiqinhai.Items.Count; i++)
{
if (dr["FK_PostID"].ToString() == cbl_aiqinhai.Items[i].Value)
{
cbl_aiqinhai.Items[i].Selected = true;
}
}
}
}
#endregion
{
Bind_cbl_PostName();
DataTable dt = GetPostUserID(this.ddl_aiqinhai.SelectedValue);
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < cbl_aiqinhai.Items.Count; i++)
{
if (dr["FK_PostID"].ToString() == cbl_aiqinhai.Items[i].Value)
{
cbl_aiqinhai.Items[i].Selected = true;
}
}
}
}
#endregion
循环得到ID
for (int i = 0; i < cbl_aiqinhai.Items.Count; i++)
{
if (cbl_aiqinhai.Items[i].Selected)
{
{
if (cbl_aiqinhai.Items[i].Selected)
{
string userid;
string postid;
userid= this.ddl_aiqinhai.SelectedValue;
postid = cbl_aiqinhai.Items[i].Value;
.....
}
}
userid= this.ddl_aiqinhai.SelectedValue;
postid = cbl_aiqinhai.Items[i].Value;
.....
}
}
当多选框在GridView中,选择下拉列表时所对应的是GridView中的相关信息。
<asp:GridView ID="grv_Aiqinhai" SkinID="GridView" UseAccessibleHeader="false" runat="server" Width="98%"
DataKeyNames="PK_postid" AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate >
<asp:CheckBox ID="cbl_Aiqinhai" runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="60px" />
<ItemStyle HorizontalAlign="Center" Width="60px" />
</asp:TemplateField>
<asp:BoundField DataField="DailyProcedureName" HeaderText="岗位名称" >
<HeaderStyle HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
DataKeyNames="PK_postid" AutoGenerateColumns="False" AllowPaging="True">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate >
<asp:CheckBox ID="cbl_Aiqinhai" runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="60px" />
<ItemStyle HorizontalAlign="Center" Width="60px" />
</asp:TemplateField>
<asp:BoundField DataField="DailyProcedureName" HeaderText="岗位名称" >
<HeaderStyle HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
<PagerSettings Visible="False" />
</asp:GridView>
下拉列表的事件如下:
protected void ddl_aiqinhai_SelectedIndexChanged(object sender, EventArgs e)
{
{
DataTable dt = GetDailyProcedureID(this.ddl_aiqinhai.SelectedValue);
if (dt.Rows.Count > 0)
{
for (int i = 0; i <= grv_Aiqinhai.Rows.Count - 1; i++)
{
if (dt.Rows.Count > 0)
{
for (int i = 0; i <= grv_Aiqinhai.Rows.Count - 1; i++)
{
foreach (DataRow dr in dt.Rows)
{
{
if (grv_Aiqinhai.DataKeys[i].Value.ToString() != null)
{
if (grv_Aiqinhai.DataKeys[i].Value.ToString() == dr["FK_userid"].ToString())
{
CheckBox cbox = (CheckBox)grv_Aiqinhai.Rows[i].FindControl("cbl_Aiqinhai");
cbox.Checked = true;
{
if (grv_Aiqinhai.DataKeys[i].Value.ToString() == dr["FK_userid"].ToString())
{
CheckBox cbox = (CheckBox)grv_Aiqinhai.Rows[i].FindControl("cbl_Aiqinhai");
cbox.Checked = true;
}
}
}
}
}
}
}
}
}
循环得到GridView中的ID
foreach (GridViewRow grv in grv_Aiqinhai.Rows)
{
if (((CheckBox)grv.FindControl("cbl_Aiqinhai")).Checked)
{
{
if (((CheckBox)grv.FindControl("cbl_Aiqinhai")).Checked)
{
.......
}
}
}
}
得到ID即可对数据进行相关操作。
转载于:https://blog.51cto.com/nihaiqin/86941