DataList里面镶嵌多个服务器控件获取信息
//设置控件
<asp:DataList ID="dlAuthorsInfo" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" OnDeleteCommand="dlAuthorsInfo_DeleteCommand" OnItemDataBound="dlAuthorsInfo_ItemDataBound" ForeColor="Black">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="ID:" Width="44px"></asp:Label>//CodeGo.net/
<asp:Label ID="labUserID" runat="server" Font-Size="9pt" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="学位:" Width="46px"></asp:Label>
<asp:Label ID="labName" runat="server" Font-Size="9pt" Text='<%# DataBinder.Eval(Container.DataItem,"state") %>'></asp:Label>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="8pt" Text="是否勾选" />
<asp:Label ID="Label5" runat="server" Font-Size="9pt" Text="性别:" Width="42px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Font-Size="8pt"
Width="58px" DataTextField="sex" DataValueField="sex" Enabled="False">
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
//其他学生信息(略)
。
//数据绑定显示
protected void Page_Load(object sender, EventArgs e)//显示性别
{
if (!IsPostBack)
{
sex_select();
}
}
//显示DropDownList索引
public void sex_select()
{
DropDownList ddl;
string sqlstr = "select top 3* from tb_mrEmp";
DataSet myds = ExceDS(sqlstr);
dlAuthorsInfo.DataSource = myds;
dlAuthorsInfo.DataKeyField = "ID";
dlAuthorsInfo.DataBind();
for (int i = 0; i < dlAuthorsInfo.Items.Count; i++)
{
if (Convert.ToString(myds.Tables[0].Rows[i]["sex"]).Trim() == "男")
{
ddl = (DropDownList)dlAuthorsInfo.Items[i].FindControl("DropDownList1");
ddl.SelectedIndex = 0;
}
if (Convert.ToString(myds.Tables[0].Rows[i]["sex"]).Trim() == "女")
{
ddl = (DropDownList)dlAuthorsInfo.Items[i].FindControl("DropDownList1");
ddl.SelectedIndex = 1;
}
}
}
//CheckBox复选框用来删除指定行
protected void Button2_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
for (int i = 0; i <= dlAuthorsInfo.Items.Count - 1; i++)
{
CheckBox cbox = (CheckBox)dlAuthorsInfo.Items[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string sqlstr = "delete from tb_mrEmp where ID='" +dlAuthorsInfo.DataKeys[i].ToString()+ "'";
this.ExceSQL(sqlstr);
}
}
bind();
}
//字符关系这里仅提供朋友所提功能(其他略)
//设置控件
<asp:DataList ID="dlAuthorsInfo" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" OnDeleteCommand="dlAuthorsInfo_DeleteCommand" OnItemDataBound="dlAuthorsInfo_ItemDataBound" ForeColor="Black">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="ID:" Width="44px"></asp:Label>//CodeGo.net/
<asp:Label ID="labUserID" runat="server" Font-Size="9pt" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
<asp:Label ID="Label3" runat="server" Font-Size="9pt" Text="学位:" Width="46px"></asp:Label>
<asp:Label ID="labName" runat="server" Font-Size="9pt" Text='<%# DataBinder.Eval(Container.DataItem,"state") %>'></asp:Label>
<asp:CheckBox ID="CheckBox1" runat="server" Font-Size="8pt" Text="是否勾选" />
<asp:Label ID="Label5" runat="server" Font-Size="9pt" Text="性别:" Width="42px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Font-Size="8pt"
Width="58px" DataTextField="sex" DataValueField="sex" Enabled="False">
<asp:ListItem>男</asp:ListItem>
<asp:ListItem>女</asp:ListItem>
</asp:DropDownList>
//其他学生信息(略)
。
//数据绑定显示
protected void Page_Load(object sender, EventArgs e)//显示性别
{
if (!IsPostBack)
{
sex_select();
}
}
//显示DropDownList索引
public void sex_select()
{
DropDownList ddl;
string sqlstr = "select top 3* from tb_mrEmp";
DataSet myds = ExceDS(sqlstr);
dlAuthorsInfo.DataSource = myds;
dlAuthorsInfo.DataKeyField = "ID";
dlAuthorsInfo.DataBind();
for (int i = 0; i < dlAuthorsInfo.Items.Count; i++)
{
if (Convert.ToString(myds.Tables[0].Rows[i]["sex"]).Trim() == "男")
{
ddl = (DropDownList)dlAuthorsInfo.Items[i].FindControl("DropDownList1");
ddl.SelectedIndex = 0;
}
if (Convert.ToString(myds.Tables[0].Rows[i]["sex"]).Trim() == "女")
{
ddl = (DropDownList)dlAuthorsInfo.Items[i].FindControl("DropDownList1");
ddl.SelectedIndex = 1;
}
}
}
//CheckBox复选框用来删除指定行
protected void Button2_Click(object sender, EventArgs e)
{
sqlcon = new SqlConnection(strCon);
for (int i = 0; i <= dlAuthorsInfo.Items.Count - 1; i++)
{
CheckBox cbox = (CheckBox)dlAuthorsInfo.Items[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string sqlstr = "delete from tb_mrEmp where ID='" +dlAuthorsInfo.DataKeys[i].ToString()+ "'";
this.ExceSQL(sqlstr);
}
}
bind();
}
//字符关系这里仅提供朋友所提功能(其他略)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货