📄 usereditmodule.ascx.cs
字号:
namespace KhfwWeb.Modules
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// UserEditModule 的摘要说明。
/// </summary>
public class UserEditModule : ModuleBase
{
protected System.Web.UI.WebControls.Label CreateLabel;
protected System.Web.UI.WebControls.Label UserIdLabel;
protected System.Web.UI.WebControls.TextBox NameTextBox;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
protected System.Web.UI.WebControls.RadioButtonList GenderList;
protected System.Web.UI.WebControls.TextBox AddrTextBox;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator6;
protected System.Web.UI.WebControls.TextBox PhoneTextBox;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator7;
protected System.Web.UI.WebControls.DropDownList TypeList;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.Button Submit;
protected System.Web.UI.WebControls.HyperLink Return;
protected System.Web.UI.WebControls.Label ShowMsg;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
//显示用户信息
UserIdLabel.Text=Request.QueryString ["UserId"].ToString ();
//从文件Web.config中读取连接字符串
string sqldb = ConfigurationSettings.AppSettings["ConnectionString"];
//连接JdglSys数据库
SqlConnection Conn = new SqlConnection (sqldb);
Conn.Open ();
//定义sql语句
String selsql="select Name,Gender,Address,Telephone,Type from UsersInfo where UserId = @UserId";
//创建mycommand对象,调用selsql
SqlCommand mycommand=new SqlCommand(selsql,Conn);
mycommand.Parameters .Add ("@UserId",SqlDbType.Int);
mycommand.Parameters ["@UserID"].Value = int.Parse(UserIdLabel.Text);
SqlDataReader dr=mycommand.ExecuteReader ();
if(dr.Read ())
{
NameTextBox.Text =dr["Name"].ToString ();
GenderList.SelectedIndex =int.Parse(dr["Gender"].ToString());
AddrTextBox.Text =dr["Address"].ToString ();
PhoneTextBox.Text=dr["Telephone"].ToString ();
TypeList.Items.FindByValue(dr["Type"].ToString ()).Selected =true;
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Submit.Click += new System.EventHandler(this.Submit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Submit_Click(object sender, System.EventArgs e)
{
if(Page.IsValid )
{
//从文件Web.config中读取连接字符串
string sqldb = ConfigurationSettings.AppSettings["ConnectionString"];
//连接JdglSys数据库
SqlConnection Conn = new SqlConnection (sqldb);
Conn.Open ();
//定义sql语句
String updatesql="update UsersInfo set Name= @Name,Gender=@Gender,Address= @Address,Telephone= @Telephone,Type=@Type where UserId = @UserId";
//利用Command对象调用updatesql
SqlCommand mycommand=new SqlCommand (updatesql,Conn);
//添加参数
mycommand.Parameters .Add ("@UserId",SqlDbType.Int);
mycommand.Parameters .Add ("@Name",SqlDbType.VarChar);
mycommand.Parameters .Add ("@Gender",SqlDbType.Int);
mycommand.Parameters .Add ("@Address",SqlDbType.VarChar);
mycommand.Parameters .Add ("@Telephone",SqlDbType.VarChar);
mycommand.Parameters .Add ("@Type",SqlDbType.Int );
//给存储过程的参数赋值
mycommand.Parameters ["@UserId"].Value =UserIdLabel.Text;
mycommand.Parameters ["@Name"].Value =NameTextBox.Text.Trim();
mycommand.Parameters ["@Gender"].Value =GenderList.SelectedIndex;
mycommand.Parameters ["@Address"].Value =AddrTextBox.Text.Trim();
mycommand.Parameters ["@Telephone"].Value =PhoneTextBox.Text.Trim();
mycommand.Parameters ["@Type"].Value =TypeList.SelectedItem .Value ;
try
{
mycommand.ExecuteNonQuery();
ShowMsg.Text="用户信息修改成功";
ShowMsg.Style["color"]="green";}
catch(SqlException error)
{
ShowMsg.Text="修改未成功,请稍后再试。原因:"+error.Message;
ShowMsg.Style["color"]="red";
}
//关闭连接
Conn.Close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -