Asp.Net代码备忘
来源:互联网 发布:facebook代理软件 编辑:程序博客网 时间:2024/06/04 17:42
该篇代码适用于VS2012与SQL2012的开发环境,其它环境未经测试(未标注则默认为后台代码)
引用连接字符串:
string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["BookConnectionString"].ConnectionString;
数据绑定(从控件读取的值与数据库中的列名绑定):
sqlInsertCommand.CommandText = @"Insert into [Bookdetail] ([book_name]) values (@book_name)";string book_name = TextBox1.Text;sqlInsertCommand.Parameters.AddWithValue("@book_name", book_name);
数据绑定至控件(从数据库中读取数据写入到控件):
using (SqlCommand cmd = new SqlCommand("select book_name from Bookdetail where book_id=@book_id ", myConnection)){ cmd.Parameters.AddWithValue("@book_id", book_id); TextBox1.Text = cmd.ExecuteScalar().ToString();}
执行数据库操作语句:
myCommand.ExecuteNonQuery();
获取前台控件中的控件值(如动态获取datalist中的label的值)
前台:
<asp:DataList ID="DataList1" runat="server" DataKeyField="shop_id" DataSourceID="SqlDataSource1"> <ItemTemplate> <asp:Label ID="shop_idLabel" runat="server" Text='<%# (Eval("shop_id")) %>' Visible="False" /> <asp:Button ID="Button1" CommandArgument='<%#Eval("shop_id") %>' ValidationGroup='<%#((DataListItem) Container).ItemIndex %>' runat="server" OnClick="Button1_Click" Text="Button1"/> </ItemTemplate></asp:DataList>
后台:
Button bt = sender as Button;int count = Convert.ToInt32(bt.ValidationGroup.ToString());/*获取要验证的组的值*/Label lb = (Label)DataList2.Items[count].FindControl("shop_idLabel");/*通过count值来定位shop_idlabel*/string shopid =lb.Text;int shop_id = Convert.ToInt32(shopid);
会员注册页代码:
string sqlconn = System.Configuration.ConfigurationManager.ConnectionStrings["BookConnectionString"].ConnectionString;SqlConnection myConnection = new SqlConnection(sqlconn);myConnection.Open();SqlCommand sqlSelectCommand = new SqlCommand("select [user_name] from [User] where [user_name]=@user_name", myConnection);/*在数据库中搜索TextBox1的值,如果返回的数据集表不为空,则已存在该用户,反之则插入新用户资料*/string user_name = TextBox1.Text;sqlSelectCommand.Parameters.AddWithValue("@user_name", user_name);SqlDataAdapter Adapter = new SqlDataAdapter();Adapter.SelectCommand = sqlSelectCommand;DataSet myDs = new DataSet();Adapter.Fill(myDs);DataTable myTable = myDs.Tables[0];if (myTable.Rows.Count!=0){ Label1.Text = "已存在用户名,请重新输入";}else{ SqlCommand sqlInsertCommand = new SqlCommand(); sqlInsertCommand.CommandText = @"insert into [User] ([user_name],[user_pwd],[user_phone], [user_address]) values (@user_name1,@user_pwd,@user_phone,@user_address)"; sqlInsertCommand.Connection = myConnection; /*数据绑定*/ string user_name1 = TextBox1.Text; sqlInsertCommand.Parameters.AddWithValue("@user_name1", user_name1); string user_pwd = TextBox2.Text; sqlInsertCommand.Parameters.AddWithValue("@user_pwd", user_pwd); string user_phone = TextBox4.Text; sqlInsertCommand.Parameters.AddWithValue("@user_phone", user_phone); string user_address = TextBox5.Text; sqlInsertCommand.Parameters.AddWithValue("@user_address", user_address); /*执行数据库操作语句,关闭数据库连接并跳转至指定页面*/ sqlInsertCommand.ExecuteNonQuery(); myConnection.Close(); Response.Redirect("congratulations.aspx");}
TextBox提示文字点击后自动消除:
<asp:TextBox ID="TextBox2" runat="server" BorderStyle="Solid" ForeColor="#2E81A9" Text="请输入反馈正文" OnFocus="javascript:if(this.value=='请输入反馈正文') {this.value=''}" OnBlur="javascript:if(this.value=='') {this.value='请输入反馈正文'}"></asp:TextBox>
0 0
- Asp.Net代码备忘
- ASP.net常用代码(常用技巧备忘)
- ASP.net常用代码(常用技巧备忘)
- asp.net 常用备忘
- ASP.Net资料备忘
- ASP.Net基础备忘
- ASP.Net资料备忘
- 初学ASP.Net备忘
- asp.net 备忘
- ASP.NET 备忘
- ASP.Net 备忘
- asp.net备忘
- ASP.NET Validator Control 备忘
- asp.net备忘(1)
- 初学ASP.Net时备忘
- 初学ASP.Net时备忘
- 初学ASP.Net时备忘
- asp.net验证控件备忘
- 深入解析unsigned int 和 int
- Java中Annotation(注释)系列学习笔记(1)
- git建立本地的代码版本管理
- LeetCode | Generate Parentheses(生成括号)
- FTP的主动模式(PORT Mode)及被动模式(Passive Mode)
- Asp.Net代码备忘
- Android实现下载图片并保存到SD卡中
- chrome 缓存问题
- Linux -- 运维人员必须熟悉的运维工具汇总
- networkx
- Java中Annotation(注释)系列学习笔记(2)
- IP地址分类
- .NET连接oracle数据库方式
- swift 问号和叹号的理解