关于ASP控件对象的一些简单操作

本文汇总了ASP.NET中常用的应用技巧,包括在线人数统计、Session与Application数据传递、Cookie操作、下拉列表与多选框处理等。还介绍了如何通过存储过程更新数据库,并对比了Server.Transfer与Response.Redirect的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在线人数
     Application.Lock();
        Application["num"] =(Convert.ToInt32(Application["num"]) + 1).ToString();
        Application.UnLock();
      
        Label1.Text = Application["num"].ToString();


Session  Application传值
Session["user"] = TextBox1.Text;
        Response.Redirect("Default2.aspx");
Server进行编码和解码
  Response.Write(Server.UrlEncode("http://www.abc.com"));
虚拟路径转为绝对路径
Server.MapPath()

使用cookie来获取用户名或者密码
 Response.Cookies["user"].Value = "zhangsan";
        Response.Cookies["pass"].Value = "1234";
        String str=Response.Cookies["pass"].Value;
        Label2.Text = "写入成功!";

读取数据
Label2.Text = Server.HtmlEncode(Request.Cookies["user"].Value);
        Label1.Text = Server.HtmlEncode(Request.Cookies["pass"].Value);


超链接传值过程 使用占位符:string.Format
        Response.Redirect(string.Format("Default2.aspx?username={0},pass={1}", TextBox2.Text, TextBox3.Text));
接受使用Request.QueryString["user"].ToString();


dropdownlist控件
 switch (DropDownList1.SelectedItem.Text.Trim())
        {
            case "福建":
                {
                    ListBox1.Items.Clear();
                    ListBox1.Items.Add("福州");
                    ListBox1.Items.Add("厦门");
                    ListBox1.Items.Add("泉州");
                    break;
                }

            case "广东":
                {
                    ListBox1.Items.Clear();
                    ListBox1.Items.Add("广州");
                    ListBox1.Items.Add("汕头");
                    ListBox1.Items.Add("中山");
                    break;
                }
        }

ListItem
  foreach (ListItem l in ListBox1.Items)
        {
            if (l.Selected == true)
            {
                Label1.Text = l.Text;
            }
        }


集合单选的控件
 for (int i = 0; i < CheckBoxList1.Items.Count; i++)
        {
            if (CheckBoxList1.Items[i].Selected)
            {
                Label1.Text += CheckBoxList1.Items[i].Text + "  ";
            }
        }

MApkongjian控件 需要三个条件 半径 X,Y  就可以实现点击中国地图跳转页面
  <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="~/img/p3.jpg">
        <asp:CircleHotSpot Radius="15" X="10" Y="10" />
        <asp:CircleHotSpot Radius="100" X="200" Y="500" />
        <asp:CircleHotSpot Radius="40" X="40" Y="90" />
        <asp:CircleHotSpot Radius="300" X="400" Y="600" />
    </asp:ImageMap>

server对象的Transfer方法和respone的Redirect()
两者的区别就 跟JSP的 重定向和URL不改变相似
第一个不该变地址

在Transfer跳转页面后也可以传值

在页面出传值有多种方法 
Request.QueryStrnng()  Session Application  PreviousPage.FindControl
 TextBox tb1 = (TextBox)PreviousPage.FindControl("TextBox2");
        TextBox tb2 = (TextBox)PreviousPage.FindControl("TextBox3");
        Label1.Text = tb1.Text;
        Label2.Text = tb2.Text;

master母版页使用

存储过程使用
ALTER PROCEDURE dbo.update_user
    /*
    (
    @parameter1 int = 5,
    @parameter2 datatype OUTPUT
    )
    */
    @sid int,
    @sname varchar(20),
    @spassword varchar(12),
    @sex char(10),
    @saddress varbinary(50),
    @img varbinary(50)
AS
    /* SET NOCOUNT ON */
    update tb_user set sname=@sname,spassword=@spassword,sex=@sex,saddress=@saddress,img=@img
    RETURN
后台更新数据库值要说明是存储过程

 

转载于:https://www.cnblogs.com/codemouserman/p/4154007.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值