初学.net之乱七八糟的记录

学校原因,居然在没有学C#,C++的时候让我们选.net....我就不吐槽什么了,此文章完全是为自己记录╮(╯▽╰)╭


实验一:

1、设计并实现一个用于查询教师课表的联动下拉列表页面

2、设计并实现一个用于单项选择题的测试页面


IsPostBack

看页面是不是第一次运行,是的话就返回false,不是第一次运行就返回true。

 

DropDownList 相关

DropDownListID.Iteams.Clear()  清空值

DropDownListID.Iteams.Add( newListItem ( 所添加的内容(类型是string)) );

 添加值

DropDownListID.Iteams.SelectedValue= 字符串 ; 设置默认选值。

DropDownListID.Iteams.SelectedIndex= 数字 ;

上面这两个不仅可以用来设置还可以用来传值,告诉我们已经选择的项的信息。

 

如果某一个DropDownList的值依赖于另一个DropDownList所选的值,那么被依赖的那个DropDownList的autopostback需要被设置为true。

 

扩展:

/*  在没有选定任何项的情况下,SelectedValue默认值是string.Empty,而SelectedItem默认值是null(也就是说通过SelectedItem.Value可能发生异常)

1. selectedIndex——指的是dropdownlist中选项的索引,为int,从0开始,可读可写

2. selectedItem——指的是选中的dropdownlist中选项,为ListItem,只读不写

3. selectedValue——指的是选中的dropdownlist中选项的值,为string, 只读不写

4.selectedItem.Text——指的是选中的dropdownlist中选项的文本内容,与selectedItems的值一样为string,可读可写

5.selectedItem.value——指的是选中的dropdownlist中选项的值,与selectedValue的值一样,为string,可读可写  */

 

PlaceHolder 相关:

容器,可以加入任何新建的控件。

1、PlaceHolderID.Controls.Add(控件id );

2、FindControl

RadioButtonList ans = (RadioButtonList)PlaceHolder1.FindControl("question");

FindControl的使用方法 Control.FindControl (String):在当前的命名容器中搜索带指定 id,注意强制转换。

 

新建控件相关:

如1:

RadioButtonList answerList = newRadioButtonList();

        answerList.ID = "answerList";

        answerList.Items.Add(new ListItem("A.xxx"));

        answerList.Items.Add(new ListItem("B.xxx"));

        answerList.Items.Add(new ListItem("C.xxx"));

        answerList.Items.Add(new ListItem("D.xxx"));

 

如2:

Label question = new Label();

        question.ID = "question";

        question.Text = "XXXXX";

 

C#语言相关:

1: switch case break 一定不能省略break,这点和C不一样。


实验二:

1、设计并实现一个带验证控件的用户注册页面

2、设计并实现一个同一个页面的分组验证功能

主要是验证控件的设置


实验三:

1、设计一个聊天室


1、textBoxName.Focus();  将鼠标定位


2、string类型的比较可以直接用 =  =

 

3、Session["变量名"]= string;  将string存在session中。

 

4、//Response.Redirect("XXX.htm"); 页面的跳转

Response.Redirect("chat.htm");

 

5、对于:

Response.Write("<script type = 'text/javascript'> alert(用户名或者密码错误!</script>)");

1)Response.Write

Write 方法将指定的字符串写到当前的 HTTP 输出

但如果你希望弹出信息并单击确定后,页面跳转到另一个页面,你需要在后台这样写:

Response.Write("<script>alert('修改成功!');location.href='index.aspx';</script>");

如果是:

Response.Write("<script>alert('修改成功!')</script>");

Response.Redirect('index.aspx');

但很不幸,前台根本就不会弹出窗口,所以我最后找出后面的方式改正的。但现在我想说的是,之所以弹窗不显示的原因是:

在程序执行Response.Write("<script>alert('修改成功!')</script>")之前,response.Redirect先导致页面跳转了。这是因为虽然response.Redirect也借助了客户端脚本实现,但是其优先级是浏览器级别的,要高于用户自定义脚本级别。所以我输出的js脚本还没有执行就已经跳转到index.aspx页面中了。

 

2)嵌入javascript语句:

<script type = 'text/javascript'>alert(用户名或者密码错误!</script>

尖括号

 

3)JavaScript alert()

JavaScript alert()函数,alert--弹出消息对话框(对话框中有一个OK按钮)

alert函数语法——alert(str); //str--要显示在消息对话框中的文本

alert消息对话框通常用于一些对用户的提示信息,例如在表单中输入了错误的数据时。

提示:消息对话框是由系统提供的,因此样式字体在不同浏览器中可能不同。

提示:消息对话框是排它的,也就是在用户点击对话框的按钮前,不能进行任何其它操作。

提示:消息对话框通常可以用于调试程序。

 

6、<meta http-equiv = "reflash"content = "4" />

在一对head之间,每4S刷新一次页面

 

7、Label1.Text =Application["message"].ToString();

application中保存的值

 

Application["message"] +=TextBox1.Text + "<br/>";

向Application存值

 

Application.Lock();

Application.UnLock();


实验四:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Session["pet"].ToString());
        if (!IsPostBack)
        {
           // Session["pet"] = "";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < petlist.Items.Count; i++)
        {
            if (petlist.Items[i].Selected)
            {
                Session["pet"] += petlist.Items[i].Text + ",";
            }
        }
        Response.Write(Session["pet"].ToString());
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCart.aspx");
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;


public partial class ViewCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Session["pet"].ToString());
        if (!IsPostBack)
        {
            if (Session["pet"] == "" | Session["pet"] == null)
            {
                Label1.Text = "你还没有选任何宠物的说╮(╯▽╰)╭";
                clear.Enabled = false;
            }
            else
            {
                String petstr = Session["pet"].ToString();
                ArrayList pets = new ArrayList();
                int position = petstr.IndexOf(",");
                while (position != -1)
                {
                    string apet = petstr.Substring(0, position);
                    if (apet != "")
                    {
                        pets.Add(apet);
                        petstr = petstr.Substring(position + 1);
                        position = petstr.IndexOf(",");
                    }
                }
                Label1.Text = "购物车现在已有的萌宠(>▽<):";
                CheckBoxList1.DataSource = pets;
                CheckBoxList1.DataBind();
            }
        }
    }
    protected void clear_Click(object sender, EventArgs e)
    {
        
        Session["pet"] = "";
        Label1.Text = "你还没有选任何宠物的说╮(╯▽╰)╭";
        CheckBoxList1.Visible = false;
        clear.Enabled = false;
        Response.Write(Session["pet"].ToString());
    }
    protected void con_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}


button.Enabled = false;
运行一个button不可用


Session["pet"] == "" | Session[pet] == null
看是不是有值


String pets = Session["pet"].ToString();
session中取值要先转换为string类型


ArrayList pets = new ArrayList();
pets.Add(apet);
CheckBoxList1.DataSource = pets;
CheckBoxList1.DataBind();


int position = petstr.IndexOf(" ");
查找索引


CheckBoxList1.Visible = false;不可见



实验5:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        //GridView绑定数据源
        GridView1.DataSourceID = "SqlDataSource1";
        GridView1.DataBind();
    }
    protected void btnFuzzy_Click(object sender, EventArgs e)
    {
        Response.Redirect("Fuzzy.aspx");
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        Response.Redirect("Insert.aspx");
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Edit.aspx?CategoryId="+TextBox1.Text);
    }
    protected void btnEditLinq_Click(object sender, EventArgs e)
    {
        Response.Redirect("LinqEdit.aspx?CategoryId="+TextBox1.Text);
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        SqlDataSource1.Delete();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Fuzzy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void retu_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (e.AffectedRows == 0)
        {
            Label.Text = "没有符合要求的数据";
        }
        else
        {
            Label.Text = "";
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Insert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ok_Click(object sender, EventArgs e)
    {
        SqlDataSource1.Insert();
    }
    protected void retu_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataView dv = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            DataTable dt = dv.ToTable();
            ID.Text = dt.Rows[0]["CategoryId"].ToString();
            DES.Text = dt.Rows[0]["Descn"].ToString();
            NAME.Text = dt.Rows[0]["Name"].ToString();
        }
    }
    protected void SET_Click(object sender, EventArgs e)
    {
        SqlDataSource1.Update();
    }

    protected void RYTU_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


public partial class LinqEdit : System.Web.UI.Page
{
    MyPetDataContext db = new MyPetDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var cate = from c in db.Category
                       where c.CategoryId == int.Parse(Request.QueryString["CategoryId"])
                       select c;
            if (cate != null)
            {
                foreach (Category ca in cate)
                {
                    ID.Text = ca.CategoryId.ToString();
                    NAME.Text = ca.Name.ToString();
                    DES.Text = ca.Descn.ToString();
                }
            }
        }
    }
    protected void OK_Click(object sender, EventArgs e)
    {
        var cata = from c in db.Category
                   where c.CategoryId == int.Parse(ID.Text)
                   select c;
        if (cata != null)
        {
            foreach (Category ca in cata)
            {
                ca.Name = NAME.Text;
                ca.Descn = DES.Text;
            }
            db.SubmitChanges();
        }
    }
    protected void RETU_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值