基本的Web控件四

基本的Web控件用法二

 

ListBox控件

<div>
        <h1>ListBox控件</h1>
        学生列表:
        <br/>
        <asp:ListBox ID="ListBox1" runat="server" Width="100" Height="100" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged“AutoPostBack="true">
        </asp:ListBox>
        <br />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br/>
</div>
protected void Page_Load(object sender, EventArgs e)
{
            if (!Page.IsPostBack)
            {
                //生成数据
                DataSet ds = new DataSet();
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "王聪", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "李宁", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "菡萏", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "青松", 100 });
                //绑定到ListBox控件
                this.ListBox1.DataSource = ds.Tables["stu"];
                this.ListBox1.DataValueField = "stuNo";
                this.ListBox1.DataTextField = "stuName";
                this.ListBox1.DataBind();
            }
}
                                                                                                                                protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
            this.Label1.Text = "你选择的学生是:" + this.ListBox1.SelectedItem.Text.ToString();
}

            运行结果:

 

DropDownList控件

<div>
        <h1>DropDownList控件</h1>
        学生列表:
        <br/>
        <asp:DropDownList ID="DropDownList1" Font-Size="Large" runat="server" AutoPostBack="true" Width="146px"                              OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br/>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <hr/>
        <hr/>
</div>
protected void Page_Load(object sender, EventArgs e)
{
            if (!Page.IsPostBack)
            {
                //生成数据
                DataSet ds = new DataSet();
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "王聪", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "李宁", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "菡萏", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "青松", 100 });
                //绑定到ListBox控件
                this.DropDownList1.DataSource = ds.Tables["stu"];
                this.DropDownList1.DataValueField = "stuNo";
                this.DropDownList1.DataTextField = "stuName";
                this.DropDownList1.DataBind();
            }
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
            this.Label1.Text = "你选择的学生是:" + this.DropDownList1.SelectedItem.Text.ToString();
}

            运行结果:

 

CheckBoxList控件

<div>
        <h1>CheckBoxList控件</h1>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"                      AutoPostBack="true" Width="180px">
        </asp:CheckBoxList>
        <br/>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <hr/>
        <hr/>
</div>
protected void Page_Load(object sender, EventArgs e)
{
            if (!Page.IsPostBack)
            {
                //生成数据
                DataSet ds = new DataSet();
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "王聪", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "李宁", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "菡萏", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "青松", 100 });
                //绑定到ListBox控件
                this.CheckBoxList1.DataSource = ds.Tables["stu"];
                this.CheckBoxList1.DataValueField = "stuNo";
                this.CheckBoxList1.DataTextField = "stuName";
                this.CheckBoxList1.DataBind();
            }
}

            运行结果:

RadioButtonList控件

<div>
        <h1>RadioButtonList控件</h1>
        <asp:RadioButtonList ID="RadioButtonList1" runat="server" Width="180px" AutoPostBack="true">
        </asp:RadioButtonList>
        <hr/>
        <hr/
</div>
protected void Page_Load(object sender, EventArgs e)
{
            if (!Page.IsPostBack)
            {
                //生成数据
                DataSet ds = new DataSet();
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "王聪", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "李宁", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "菡萏", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "青松", 100 });
                //绑定到ListBox控件
                this.RadioButtonList1.DataSource = ds.Tables["stu"];
                this.RadioButtonList1.DataValueField = "stuNo";
                this.RadioButtonList1.DataTextField = "stuName";
                this.RadioButtonList1.DataBind();
            }
}

            运行结果:

BulletedList控件

<div>
        <h1>BulletedList控件</h1>
        <asp:BulletedList ID="BulletedList1" runat="server" BulletStyle="Square">
        </asp:BulletedList>
        <hr/>
        <hr/>
</div>
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //生成数据
                DataSet ds = new DataSet();
                ds.Tables.Add("stu");
                ds.Tables["stu"].Columns.Add("stuNo", typeof(int));
                ds.Tables["stu"].Columns.Add("stuName", typeof(string));
                ds.Tables["stu"].Columns.Add("stuScore", typeof(int));
                ds.Tables["stu"].Rows.Add(new object[] { 1, "王聪", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 2, "李宁", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 3, "菡萏", 100 });
                ds.Tables["stu"].Rows.Add(new object[] { 4, "青松", 100 });
                //绑定到ListBox控件
                this.BulletedList1.DataSource = ds.Tables["stu"];
                this.BulletedList1.DataValueField = "stuNo";
                this.BulletedList1.DataTextField = "stuName";
                this.BulletedList1.DataBind();
            }
}

            运行结果:

Table控件

<div>
        <h1>Table控件</h1>
        动态操作表控件<br/>
        行数:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        列数:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br/>
        <asp:Button ID="Button1" runat="server" Text="动态生成" OnClick="Button1_Click" />
        <br/>
        <asp:Table ID="Table1" runat="server" Caption="动态操作表控件" CellPadding="1" CellSpacing="1" GridLines="Both">                  </asp:Table> 
        <hr/>
        <hr/>
</div>
protected void Button1_Click(object sender, EventArgs e)
{
            int rowNum = 0;         //定义行数
            int rowCount = 0;       //定义当前行数
            int cellNum = 0;        //定义列数
            int cellCount = 0;      //定义当前列数

            //获取用户输入的行数和列数
            try
            {
                rowNum = int.Parse(TextBox1.Text);
                cellNum = int.Parse(TextBox2.Text);
            }
            catch
            {
                rowCount = 1;
                cellCount = 1;
            }
            for (rowCount = 1; rowCount <= rowNum; rowCount++)
            {
                //为Table1表添加一行
                TableRow tRow = new TableRow();
                Table1.Rows.Add(tRow);

                for (cellCount = 1; cellCount <= cellNum; cellCount++)
                {
                    //创建单元格并添加到表中
                    TableCell tCell = new TableCell();
                    tRow.Cells.Add(tCell);

                    //添加一个用来包含文本的Literal类,作为控件添加到单元格中
                    tCell.Controls.Add(new LiteralControl("当前位置:"));

                    //创建一个Hyperlink控件并把它添加到单元格中
                    System.Web.UI.WebControls.HyperLink h = new HyperLink();
                    h.Text = rowCount + ":" + cellCount;
                    h.NavigateUrl = "http://www.microsoft.com/net";
                    tCell.Controls.Add(h); 
                }
            }
}

            运行结果:

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值