Asp.Net碎知识
在aspx页面 获取值: UserModel user=new UserModel();实例化 user.Address=context["txtAddress"]; 如果前台不需要的字段,但是数据库中有:user.Money=0;
------------------加密加密加密加密加密加密加密加密加密加密加密加密加密加密加密加密加密加密加密加密------------------------------
MD5加密 放在一个Tool公共类库(别的项目也可以用)里就ok了
public string GetMd5Code(string str) { //MD5是抽象类 ,System.Security.Cryptograph.MD5 MD5:HashAlgo MD5继承哈希 MD5 md5 = MD5.Create(); ////////////////////.制定编码格式--Default byte[] oldStr = system.Text.Encoding.Default.getBytes(str); //将传过来的str进行编码 byte[] md5str = md5.ComputerHash(oldStr); StringBuilder sb = new StringBuilder(); for(int i = 0; i < md5str.Lenght; i++) { sb.Append(md5str[i].ToString("x2")); //转换成16(x)进制,2保证加密后的密文长度一样 } return sb.Tostring(); }
绑定数据++++修改删除++++提示
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="list_table"> <tr> <th width="100">序号</th> <th width="100">标题</th> <th width="100">内容</th> <th>关键字</th> <th>描述</th> <th>添加时间</th> <th>操作</th> </tr> <asp:Repeater ID="Repeater12" runat="server" OnItemCommand="Repeater12_ItemCommand"> <ItemTemplate> <tr> <td><%#Eval( "ID") %></td> <td><%#Eval( "Title") %></td> <td><%#Eval( "Cont") %></td> <td><%#Eval( "Keys") %></td> <td><%#Eval( "Des") %></td> <td><%#Eval( "AddTime") %></td> <td> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Del" CommandArgument='<%#Eval("ID") %>'>删除</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("ID") %>'>修改</asp:LinkButton> <asp:Label ID="Label1" runat="server" Text='<%#Eval("NewID") %>' Visible="false"></asp:Label> <asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn82 btn_del" CommandName="del" OnClientClick="return confirm('要删除吗?')">删除</asp:LinkButton> <asp:Label ID="Label2" runat="server" Text='<%#Eval("NewID") %>' Visible="false"></asp:Label> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("NewID")%>'>修改</asp:LinkButton> </td> </tr> </ItemTemplate> </asp:Repeater> </table>
if(e.CommandName == "Del") { //string strid = ((Label)e.Item.FindControl("Label1")).Text; // int Id = Convert.ToInt32(strid); int id = Convert.ToInt32(e.CommandArgument.ToString()); int result = BLL.UserBLL.DelUser(id); if(result > 0) { // Response.Write("你已经删除了该用户"); Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('你已经删除了该用户')</script>"); bindUserList(); } }
--------------------------------------------------------------------------------------------------------------------------------------------
private int _age; piblic int Age; { get { return _age; } set { if(value < 0 || value > 100) { _age = 18; } else { _age = value; } } }
-------------------------------------------------------------------------------------------------------------------------------------------
隐藏传递修改的ID
<asp:HiddenField ID="hide" runat="server" /> int id = Convert.ToInt32(hide.Value);
if(!IsPostBack) { if(Request.QueryString["NewsID"] != null) { hd_id.Value = Request.QueryString["NewsID"].ToString(); int id = Convert.ToInt32(Request.QueryString["NewsID"]); NewsType(); //将下拉列表中的值娶过来 GetSingNews(id); } }
---------------------------------------------------------------------------------------------------------------
弹出框Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('请选择图片')</script>");
--------------------------------------------------------------------------------------------------------------------------------------------
配置数据库文件:
<connectionStrings> <add name="str" connectionString="server=.;database=JinKeLi;Integrated Security=True " />
</connectionStrings> <appSettings> <add key="sqlcon" value="server=.;database=JinKeLi;Integrated Security=True" />
</appSettings>
public static string constr = System.Configuration.ConfigurationSettings.AppSettings["sqlcon"];
---------------------------------------------------------------------------------------------------------------------------
验证码知识:
<script> function getVCode() { var vcode = document.getElementById("vcode"); vcode.src = "ValidateCode.ashx?i=" + Math.random(); } </script>
<p> 验证码: <input type="text" name="txtvcode" class="txt" /> <img src="ValidateCode.ashx" id="vcode" align="middle" alt="看不清,换一张" onclick="getVCode()" /> <%--后台生成的图片--%> <a href="javascript:getVCode()" class="hyz"> 换一张</a> </p>
------------------------------------------------------------------------------------------------------------------------------------
百度编辑器知识
<!-- 引用文件 顺序不能出错--> <script type="text/javascript" src="../../ueditor/ueditor.config.js"> </script> <script type="text/javascript" src="../../ueditor/ueditor.all.js"> </script> <link rel="stylesheet" href="../../ueditor/themes/default/dialogbase.css" /> <!-- 引用文件结束--> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <tr style="width:500px; height:230px;"> <td class="td_right"> 内容页:</td> <td class="" style="width:150px;height:80px;"> <textarea name="MyEditor" style="width: 500px; height:200px;" id="MyEditor" runat="server" onblur="setUeditor()" cols="30" rows="10"> </textarea> <script type="text/javascript"> < !--new一个实体--> var editor = new baidu.editor.ui.Editor(); editor.render("MyEditor"); function setUeditor() { var myEditor = document.getElementById("MyEditor") myEditor.value = editor.getContent(); } </script> <asp:Button ID="Btn_submit" runat="server" Text="输出editor的值" OnClick="Btn_submit_Click" /> <!-- Lable是显示文字--> <asp:Label ID="Label1" runat="server" Text="Label"> </asp:Label> </td> </tr>
----------------------------------------------------------------------------------------------------------------------------------------------------------------
关于新闻类别的传递和修改,接受的方法
接受用: DropDownList1.SelectedValue = dt.Rows[0]["NewsType"].ToString(); 但是先绑定类型源 DropDownList1.SelectedItem.Text = dt.Rows[0]["ProName"].ToString(); 获取值: DropDownList1.Text = DropDownList1.SelectedValue; DropDownList1.Text = DropDownList1.SelectedValue; 添加的时候用
-------------------------------------------------------------------------------------------------------------------------
从客户端(MyEditor="<p><img src="/uedito...")中检测到有潜在危险的 Request.Form 值。 ValidateRequest="false"
------------------------------------------------------------------------------------------------------------------------------------
public string GetImgType(string typeid) //将类型装换成文字 { DataTable dt = database.GetDt("select * from tb_ImgageType where id=" + typeid); if(dt != null && dt.Rows.Count > 0) { return dt.Rows[0]["TypeName"].ToString(); } else { return ""; } }
------------------------------------------------------------------------------------------------------------------------------
将路径转换成图片
<td class="text-l"><img src='<%#Eval("ConfigLogo") %>' /></td>
----------------------------------------------------------------------------------
sqlparmeter 防止参数注入的另一种写法:
StringBuilder strSql = new StringBuilder(); strSql.Append("select Id,LoginId,LoginPwd,Name,Address,Phone,Mail,UserRoleId,UserStateId,Money from Users "); strSql.Append(" where LoginId=@LoginId"); SqlParameter[] parameters = { new SqlParameter("@LoginId", SqlDbType.NVarChar, 50) }; parameters[0].Value = loginid;
----------------------------------------------------------------------------------------------------
<span style="color:Red"><%=LoginIdMsg %></span>前台 public string LoginIdMsg { get; set; } 后台 LoginIdMsg = loginidmsg; 这样把后台要返回的数据,就可以在前台展示出来
--------------------------------------------------------------------------------------------------------
记住我:
<asp:CheckBox ID="cbAutoLogin" runat="server" />使我保持登录状态
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { if(Request.Cookies["username"] != null && Request.Cookies["pwd"] != null) { this.username.Text = Request.Cookies["username"].Value; this.pwd.Text = Request.Cookies["pwd"].Value; } } } protected void Button2_Click(object sender, EventArgs e) { Response.Cookies["username"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(-1); if(cbAutoLogin.Checked) { Response.Cookies["username"].Expires = DateTime.Now.AddDays(7); Response.Cookies["pwd"].Expires = DateTime.Now.AddDays(7); } Response.Cookies["username"].Value = this.username.Text.Trim(); Response.Cookies["pwd"].Value = this.pwd.Text.Trim(); }
-------------------------------------------------------------------------------------------------------------------------------------
Session赋值,取值
登录的时候用--Session记录住用户名 Session["username"] = name; 赋值 < li > 欢迎 <%= Session["username"] %> < /li> if( Session["name"] == null || Session["name"].Tostring() == "") { reposnse.Redicrt("Login"); } 关于Session记录登录用户的情况: 在其他页面程序进行 操作时, 要判断是否在登录状态, 只要在同一个作用域内, 就可以引用Session了
-------------------------------------------------------------------------------------------------------------
性别:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="sex" Text="男" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="sex" Text="女" />
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="性别" />
protected void Button4_Click(object sender, EventArgs e) { string s = ""; if(RadioButton1.Checked) { s = RadioButton1.Text; } if(RadioButton2.Checked) { s = RadioButton2.Text; } Response.Write("<script>alert('你的性别是:" + s + "')</script>"); }
------------------------------------------------------------------------------------------------------------------
sqlparmeter的另外一种写法:
StringBuilder strSql = new StringBuilder(); strSql.Append("delete from Books "); strSql.Append(" where ISBN=@ISBN and Id=@Id "); SqlParameter[] parameters = { new SqlParameter("@ISBN", SqlDbType.NVarChar, 50), new SqlParameter("@Id", SqlDbType.Int, 4) }; parameters[0].Value = ISBN; parameters[1].Value = Id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if(rows > 0) { return true; } else { return false; }
------------------------------------------------------------------------------------------------------
获取用户输入的值
string name=context.request["txtName"];
string pwd=context.request["txtPwd"];
创建模板
string path = context.request["Login.html"]; string html = system.IO.File.MapPath(path); string sql = "select * from users where name=@name and pwd=@pwd"; SqlParsms[] sps = { new Sqlparams("@name", name), new Sqlparams("@pwd", pwd) } int result = Convert.Into32(sqlhelper.ExectuScalar(sql, sps)); if(result > 0) { context.request.write("<script>alert('登陆成功')</script>"); context.requset.Redict..... } else { context.request.write("<script>alert('登录失败')</script>"); html = html.replace("@txtname", name).replace("$txtpwd", ""); }
-------------------------------------------------------------------------------------------------------------------------
if(UserBLL.CheckUsers(mm) <= 0) { MessageBox.Show("用户不存在"); DialogResult qq = MessageBox.Show("确定注册吗", "不", MessageBoxButtons.YesNoCancel); if(qq == DialogResult.Yes) { AddAdmin AAA = new AddAdmin(); AAA.Show(); this.Hide(); } else { txtBoxUser.Text = ""; pwd = txtBoxPwd.Text = ""; } return; }
---------------------------------------------------------------------
ViewState用法:
<form action="Login.ashx" method="post"> <table style=" margin:150px auto"> <input type="hidden" name="ViewState" value="123" /> <tr><td colspan="2" style=" text-align:center"> 登录系统</td></tr> <tr><td><label for="username">用户名:</label></td> <td><input type="text" name="username" id="username" value="$name" /></td> </tr> <tr> <td><label for="pwd">密码:</label></td> <td><input type="text" name="pwd" id="pwd" value="$pwd" /></td> </tr> <tr> <td colspan="2" style=" text-align:center"><input type="submit" value="登陆" id="btn1" /></td> </tr> </table> </form>
string viewstate = context.Request["ViewState"]; bool IsPostBack = !string.IsNullOrEmpty(viewstate); if(!IsPostBack) {} else { if() {} else { context.Response.Write("<script>alert('登陆失败!');</script>"); loginhtml = loginhtml.Replace("$name", name).Replace("$pwd", ""); context.Response.Write(loginhtml); } }
-------------------------------------------------------------------------------------
性别知识点:
protected void Button3_Click(object sender, EventArgs e) { string s = ""; if(RadioButton1.Checked) { s = RadioButton1.Text; } if(RadioButton2.Checked) { s = RadioButton2.Text; } Response.Write("<script>alert('你的相别是" + s + "')</script>"); }
------------------------------------------------
public void InitDropDownList1() //下拉列表添加数据 { //仅仅定义了一个方法,InitDropDownList1 //单个填 DropDownList1.Items.Add("141"); DropDownList1.Items.Add("142"); DropDownList1.Items.Add("143"); ListItem item1 = new ListItem("141", "1"); ListItem item2 = new ListItem("141", "1"); ListItem item3 = new ListItem("141", "1"); ListItem item4 = new ListItem("141", "1"); string[] items = { "141", "142", "143", "144" }; DropDownList1.DataSource = items; DropDownList1.DataBind(); //绑定数据 //绑定数据库中的某张表 string sql = "select * from Province"; DataSet da = sqlHelper.GetDataSet(sql); DropDownList1.DataSource = da; DropDownList1.DataTextField = "ProvinceName"; //获取市区 DropDownList1.DataValueField = "ProvinceID"; DropDownList1.DataBind(); }
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9
Visual Studio Premium 2013 KEY(密钥):FBJVC-3CMTX-D8DVP-RTQCT-92494
Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY
Team Foundation Server 2013 KEY(密钥):MHG9J-HHHX9-WWPQP-D8T7H-7KCQG
---------------------------------------------------------------------
IsPostBack 是Page类有一个bool类型的属性,用来判断针对当前Form的请求是第一次还是非第一次,IsPostBack=false时表示是第一次请求,当IsPostBack=true时,表示是非第一次请求。因
为第一次请求的时候会执行Page_Load,在非第一次请求的时候也会执行Page_Load。为什么对同一个Form有多次请求呢?asp.net中引入了服务器端事件,支持服务器端事件的控件,会发出对当前Form的请求,这样在很多情形下我们就需要区别是否是对这个Form的第一次请求。
二、IsPostBack结论
1、对于使用Server.Transfer进行进行迁移时迁移到的页面其IsPostBack=false 每次刷新页面都是第一次加载页面; 2. Post方式如果Request中没有请求值,即Request.Form =null则IsPostBack=false;Get方式如果Request中没有请求值,即Request.QueryString =null则IsPostBack=false。
-------------------------------------------------------