20151029adonet1

Login.aspx html+后台页面cs CodeBehind技术连接在一起

web.config 存储公共信息

服务端控件 客户端控件 自定义控件

服务端控件

特点:runat="server"

文本框 TextBox txt 文本内容:text TextMode:SingleLine  Password MultiLine

Button: btn 双击会有点击事件

page_load:程序入口

ID.Text设置/获取文本框的值

ID.Text.Trim()清空文本框中前后的空格

修改代码之后,需要进行编译

登录Login.aspx:

1、获取输入的用户名密码

2、判断是用户名密码否为空 string.IsNullOrEmpty

Respone.Write("<script>alert('用户名密码不能为空')</script>");

3、实现和数据库进行交互

  3.1查询语句要精简 select UserId

  3.2往字符串中传值string.Format("",)

  3.3在web.config里写数据库的连接字符串

  <connectionStrings>
      <add name="CGTdata" connectionString="Data Source=.;Initial Catalog=dataname;User ID=user;Password=pwd;Pooling=False"/>//False sqlserver验证
   </connectionStrings>

  3.4通过节点的名称获取连接字符串 System.Configuration;

   string strCon=ConfigurationManager.ConnectionStrings["CGTdata"].ToString();

  3.5通过连接字符串 连接数据库 System.Data.SqlClient

    SqlConnection con=new SqlConnection(strCon);

    con.open();打开

  3.6创建执行的Sql语句的对象 

    SqlCommand cmd=new SqlCommand(strsql,conn);

  3.7执行语句,将得到的到果赋值给read

    SqlDataReader read = cmd.ExecuteReader()

  3.8判断结果是否存在

    read.HasRows 返回ture false;

    true:Response.Redirect("RNewsM.aspx");跳转页面

    false:Response.Write("<script>alert('用户名或密码错误')</script>");

  3.9释放资源 SqlDataReader SqlConnection  read.Close(); read.Dispose(); con.Close(); read.Dispose(); 也可以用using IDisposable

RNewsM.aspx

1、sql语句

2、GridView gvRNews 绑定字段

3、页面打开之后执行方法 BindNews();得到read

4、设置gvRNews的数据源,然后进行数据绑定. 

  gvRNews.DataSource = read;
  gvRNews.DataBind();

5、设置gvRNews的CSS,它本身是一个table。

6、加入fieldset 查询界面 textbox DropDownList 添加分类 distinct

7、写一个sql语句 字符串拼凑

where 1=1 

值为空的时候,如果不加sql语句会出错。

为了满足各种情况,所以添加1=1

判断ID是否为空!string.IsNullOrEmpty(txtSRNewsId.Text.Trim()

sb.Append(string.Format("and NewsId={0}",Convert.ToInt32(txtSRNewsId.Text.Trim()));

判断分类是否选择ddlNewsClass.SelectedIndex > 0

sb.Append(string.Format("and NewsClass='{0}'", ddlNewsClass.SelectedValue));

上传文件:如果修改了后台文件,则需要重新编译,上传修改文件,以及整个网站的dll。

原文地址:https://www.cnblogs.com/16lily521/p/4936720.html