Asp.Net代码备忘

该篇代码适用于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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值