zoukankan
html css js c++ java
个人学习代码保存:例7.ListBox小代码
前台代码:Default.aspx
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:ListBox ID
=
"
ListBox1
"
runat
=
"
server
"
SelectionMode
=
"
Multiple
"
></
asp:ListBox
>&
nbsp;
<
asp:Button ID
=
"
Button2
"
runat
=
"
server
"
OnClick
=
"
Button2_Click
"
Text
=
"
>
"
/>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
OnClick
=
"
Button1_Click
"
Text
=
"
>>
"
/>
<
asp:ListBox ID
=
"
ListBox2
"
runat
=
"
server
"
SelectionMode
=
"
Multiple
"
></
asp:ListBox
></
div
>
</
form
>
</
body
>
</
html
>
后台代码:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack)
{
this
.ListBindData();
}
}
public
void
ListBindData()
{
string
connstr
=
ConfigurationManager.AppSettings[
"
ConnectionString
"
].ToString();
SqlConnection con
=
new
SqlConnection(connstr);
if
(con.State.Equals(ConnectionState.Closed))
{
con.Open();
}
SqlCommand cmd
=
new
SqlCommand(
"
select * from guestbook
"
,con);
SqlDataReader sdr
=
cmd.ExecuteReader(CommandBehavior.CloseConnection);
//
这是第一种方法了
//
while (sdr.Read())
//
{
//
ListBox1.Items.Add(new ListItem(sdr["title"].ToString(),sdr["id"].ToString()));
//
}
ListBox1.DataSource
=
sdr;
ListBox1.DataTextField
=
"
title
"
;
ListBox1.DataValueField
=
"
id
"
;
ListBox1.DataBind();
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
foreach
(ListItem item
in
ListBox1.Items)
{
ListBox2.Items.Add(item);
}
ListBox1.Items.Clear();
}
protected
void
Button2_Click(
object
sender, EventArgs e)
{
ListBox2.Items.Add(ListBox1.SelectedItem);
ListBox1.SelectedIndex
=
-
1
;
ListBox2.SelectedIndex
=
-
1
;
}
}
查看全文
相关阅读:
ObjectiveC分类
显示时间格式
js模拟签名
安装卸载homebrew
NSFastEnumeration
拼接音频
在Orchard模块中访问模块本地的AppSettings
重装证书
msysgit中文问题
Apple Push Notification service
原文地址:https://www.cnblogs.com/wbcms/p/1037554.html
最新文章
C# socket编程第三篇
Sqlserver函数之SCOPE_IDENTITY()demo
求助 加载配置文件时出错,未能开始监视对web.config的更改
A class extends ArrayList, but the instance of the calss do not work in a complex case.
我的英文win 2003 装不上 vs2005中文版 公司的就可以
seems a bug of ie8
可不可以 在webservice使用membershipprovider 和roleshipprovider呢
程序员如何为国家贡献力量?
求救 带水晶报表的web项目怎么发布啊
学什么编程语言
热门文章
内存映射
22个小故事
三个故事说穿了许多人
XML简单操作
围观一下tp的游戏保护转载
驾校考试秘笈 不用看书就能通过!!
黄鹤楼
常用消息常量
二叉树C++语言
数据类型 解构赋值
Copyright © 2011-2022 走看看