ASP.Net学习之常用模块整理(2)
作者:admin 日期:2006-05-17
5.DataList分页(其实Repeart也可采用这个,只是改一下控件名而已)
用户控件
DataList: DlstInfolist
Label:lblCurrentPage
HyperLink:lnkNext LnkPrev
存储过程:supplyinfo_select
/*
查询大类下所有的supplyinfo
2006-4-10
*/
create procedure supplyinfo_select
@bigclassid int
as
select * from supply
where
Supply_CatID=@bigclassid
GO
c#.Net
private void dlstsupplyinfolist()
{
SqlConnection conndb = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
string bigid=Request.QueryString["bigid"].Trim();
SqlDataAdapter strbuyinfo = new SqlDataAdapter("supplyinfo_select",conndb);
strbuyinfo.SelectCommand.CommandType = CommandType.StoredProcedure;
strbuyinfo.SelectCommand.Parameters.Add("@bigclassid",bigid);
DataSet ds = new DataSet();
strbuyinfo.Fill(ds);
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "Page: " + CurPage.ToString();
if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page=" + Convert.ToString(CurPage-1);
if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page="+ Convert.ToString(CurPage+1);
DlstInfolist.DataSource=objPds;
DlstInfolist.DataKeyField="Supply";
DlstInfolist.DataBind();
}
}
VB.Net
Private Sub dlstsupplyinfolist()
Dim conndb As SqlConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("conn"))
conndb.Open()
Dim bigid As String = Request.QueryString("bigid").Trim()
Dim strbuyinfo As SqlDataAdapter = New SqlDataAdapter("supplyinfo_select",conndb)
strbuyinfo.SelectCommand.CommandType = CommandType.StoredProcedure
strbuyinfo.SelectCommand.Parameters.Add("@bigclassid",bigid)
Dim ds As DataSet = New DataSet()
strbuyinfo.Fill(ds)
{
Dim objPds As PagedDataSource = New PagedDataSource()
objPds.DataSource = ds.Tables(0).DefaultView
objPds.AllowPaging = True
objPds.PageSize = 5
Dim CurPage As Integer
If Not Request.QueryString("Page") Is Nothing Then
CurPage=Convert.ToInt32(Request.QueryString("Page"))
Else
CurPage=1
End If
objPds.CurrentPageIndex = CurPage-1
lblCurrentPage.Text = "Page: " + CurPage.ToString()
If Not objPds.IsFirstPage Then
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page=" + Convert.ToString(CurPage-1)
End If
If Not objPds.IsLastPage Then
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page="+ Convert.ToString(CurPage+1)
End If
DlstInfolist.DataSource=objPds
DlstInfolist.DataKeyField="Supply"
DlstInfolist.DataBind()
}
End Sub
7.用户密码修改
用户控件:
Button:Btnsubmit
TextBox:TxtNewPwd
存储过程:userpwd_update
C#.Net
/*
作者:dezai
日期:2006-3-11
用途:用来修改会员的密码
*/
create procedure userpwd_update
@user_ID int,
@user_pwd char(50)
as
update [user]
set
[UserPwd]=@user_pwd
where
[userid]=@user_id
GO
c#.Net
private void Btnsubmit_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
string userid = Request.QueryString["userid"].Trim();
SqlConnection conndb = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
SqlCommand Strupdate = new SqlCommand("userpwd_update",conndb);
Strupdate.CommandType = CommandType.StoredProcedure;
Strupdate.Parameters.Add("@user_ID",userid);
Strupdate.Parameters.Add("@user_pwd",TxtNewPwd.Text.Trim());
Strupdate.ExecuteNonQuery();
conndb.Close();
Response.Write("");
}
else
{
Response.Write("");}
}
VB.Net
vb.net
Private Sub Btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim userid As String = Request.QueryString("userid").Trim()
Dim conndb As SqlConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("conn"))
conndb.Open()
Dim Strupdate As SqlCommand = New SqlCommand("userpwd_update",conndb)
Strupdate.CommandType = CommandType.StoredProcedure
Strupdate.Parameters.Add("@user_ID",userid)
Strupdate.Parameters.Add("@user_pwd",TxtNewPwd.Text.Trim())
Strupdate.ExecuteNonQuery()
conndb.Close()
Response.Write("")
End Sub
8.发送E-mail:
用户控件:
Butoon:Button1
TextBox: TxtSubject
Label:LblUsername
c#
注意:要记得引用Jmail组件
private void Button1_Click(object sender, System.EventArgs e)
{
jmail.Message Jmail=new jmail.Message();
DateTime t=DateTime.Now;
String Subject=TxtSubject.Text.Trim();;
String body="Hello"+LblUsername.Text.Trim();
String FromEmail="eastjazz@163.com";
String ToEmail=LblUseremail.Text.Trim();
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent=true;
//Jmail创建的日志,前提loging属性设置为true
Jmail.Logging=true;
//字符集,缺省为"US-ASCII"
Jmail.Charset="GB2312";
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
Jmail.ContentType="text/html";
//添加收件人
Jmail.AddRecipient(ToEmail,"","");
Jmail.From=FromEmail;
//发件人邮件用户名
Jmail.MailServerUserName="dezaiiloveu" ;
//发件人邮件密码
Jmail.MailServerPassWord="dezaistudio" ;
//设置邮件标题
Jmail.Subject=Subject;
//邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
//邮件内容
Jmail.Body=body+t.ToString();
//Jmail发送的方法
Jmail.Send("smtp.163.com",false);
Jmail.Close() ;
InkBack.Visible=true;
InkBack.NavigateUrl="index.aspx";
}
}
VB.Net
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Jmail As jmail.Message = New jmail.Message()
Dim t As DateTime = DateTime.Now
Dim Subject As String = TxtSubject.Text.Trim()
Dim body As String = "Hello"+LblUsername.Text.Trim()
Dim FromEmail As String = "eastjazz@163.com"
Dim ToEmail As String = LblUseremail.Text.Trim()
'Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent=True
'Jmail创建的日志,前提loging属性设置为true
Jmail.Logging=True
'字符集,缺省为"US-ASCII"
Jmail.Charset="GB2312"
'信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
Jmail.ContentType="text/html"
'添加收件人
Jmail.AddRecipient(ToEmail,"","")
Jmail.From=FromEmail
'发件人邮件用户名
Jmail.MailServerUserName="dezaiiloveu"
'发件人邮件密码
Jmail.MailServerPassWord="dezaistudio"
'设置邮件标题
Jmail.Subject=Subject
'邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
'邮件内容
Jmail.Body=body+t.ToString()
'Jmail发送的方法
Jmail.Send("smtp.163.com",False)
Jmail.Close()
InkBack.Visible=True
InkBack.NavigateUrl="index.aspx"
End Sub
}
9.asp.net获取复选框所选的值
c#.net
方法1:
:
public static string GetCheckBoxListValue(CheckBoxList CBList)
{
string strTemp = "";
for (int i = 0; i < CBList.Items.Count; i++)
{
if (CBList.Items[i].Selected)
{
if (strTemp == "")
{
strTemp = CBList.Items[i].Value;
}
else
{
strTemp += "," + CBList.Items[i].Value;
}
}
}
return strTemp;
}
方法2:
CheckBoxListMessage.Text="";
for (int i=0; i {
if (checkboxlist1.Items[i].Selected)
{
CheckBoxListMessage.Text += checkboxlist1.Items[i].Text + "
";
}
}
10数据库的备份与恢复
<%@ Page %>
获取服务器端数据库列表示例
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
11.C#防注入函数
public string SafeRequest(string ParaName,int ParaType)
{
//如果是1为数字,0为字符串
string Paravalue="";
Paravalue = ParaName;
if (ParaType==1)
{
if(!(IsNumeric(Paravalue)))
{
Paravalue="0";
}
}
else
{
Paravalue=Paravalue.Replace("'","’");
}
return(Paravalue);
}
public static bool IsNumeric(string strData)
{
float fData;
bool bValid = true;
if(strData.Length>12)
{
bValid = false;
}
else
{
try
{
fData = float.Parse(strData);
}
catch (FormatException)
{
bValid = false;
}
}
return bValid;
}
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=SafeRequest(TextBox1.Text,1);
}
暂时先整理到这里,慢慢再补上吧,请大家多多指教:)[em02]
用户控件
DataList: DlstInfolist
Label:lblCurrentPage
HyperLink:lnkNext LnkPrev
存储过程:supplyinfo_select
复制内容到剪贴板 程序代码
/*
查询大类下所有的supplyinfo
2006-4-10
*/
create procedure supplyinfo_select
@bigclassid int
as
select * from supply
where
Supply_CatID=@bigclassid
GO
c#.Net
复制内容到剪贴板 程序代码
private void dlstsupplyinfolist()
{
SqlConnection conndb = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
string bigid=Request.QueryString["bigid"].Trim();
SqlDataAdapter strbuyinfo = new SqlDataAdapter("supplyinfo_select",conndb);
strbuyinfo.SelectCommand.CommandType = CommandType.StoredProcedure;
strbuyinfo.SelectCommand.Parameters.Add("@bigclassid",bigid);
DataSet ds = new DataSet();
strbuyinfo.Fill(ds);
{
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "Page: " + CurPage.ToString();
if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page=" + Convert.ToString(CurPage-1);
if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page="+ Convert.ToString(CurPage+1);
DlstInfolist.DataSource=objPds;
DlstInfolist.DataKeyField="Supply";
DlstInfolist.DataBind();
}
}
VB.Net
复制内容到剪贴板 程序代码
Private Sub dlstsupplyinfolist()
Dim conndb As SqlConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("conn"))
conndb.Open()
Dim bigid As String = Request.QueryString("bigid").Trim()
Dim strbuyinfo As SqlDataAdapter = New SqlDataAdapter("supplyinfo_select",conndb)
strbuyinfo.SelectCommand.CommandType = CommandType.StoredProcedure
strbuyinfo.SelectCommand.Parameters.Add("@bigclassid",bigid)
Dim ds As DataSet = New DataSet()
strbuyinfo.Fill(ds)
{
Dim objPds As PagedDataSource = New PagedDataSource()
objPds.DataSource = ds.Tables(0).DefaultView
objPds.AllowPaging = True
objPds.PageSize = 5
Dim CurPage As Integer
If Not Request.QueryString("Page") Is Nothing Then
CurPage=Convert.ToInt32(Request.QueryString("Page"))
Else
CurPage=1
End If
objPds.CurrentPageIndex = CurPage-1
lblCurrentPage.Text = "Page: " + CurPage.ToString()
If Not objPds.IsFirstPage Then
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page=" + Convert.ToString(CurPage-1)
End If
If Not objPds.IsLastPage Then
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?id=" + bigid +"&Page="+ Convert.ToString(CurPage+1)
End If
DlstInfolist.DataSource=objPds
DlstInfolist.DataKeyField="Supply"
DlstInfolist.DataBind()
}
End Sub
7.用户密码修改
用户控件:
Button:Btnsubmit
TextBox:TxtNewPwd
存储过程:userpwd_update
C#.Net
复制内容到剪贴板 程序代码
/*
作者:dezai
日期:2006-3-11
用途:用来修改会员的密码
*/
create procedure userpwd_update
@user_ID int,
@user_pwd char(50)
as
update [user]
set
[UserPwd]=@user_pwd
where
[userid]=@user_id
GO
c#.Net
复制内容到剪贴板 程序代码
private void Btnsubmit_Click(object sender, System.EventArgs e)
{
if(Page.IsValid)
{
string userid = Request.QueryString["userid"].Trim();
SqlConnection conndb = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
conndb.Open();
SqlCommand Strupdate = new SqlCommand("userpwd_update",conndb);
Strupdate.CommandType = CommandType.StoredProcedure;
Strupdate.Parameters.Add("@user_ID",userid);
Strupdate.Parameters.Add("@user_pwd",TxtNewPwd.Text.Trim());
Strupdate.ExecuteNonQuery();
conndb.Close();
Response.Write("");
}
else
{
Response.Write("");}
}
VB.Net
复制内容到剪贴板 程序代码
vb.net
Private Sub Btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim userid As String = Request.QueryString("userid").Trim()
Dim conndb As SqlConnection = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("conn"))
conndb.Open()
Dim Strupdate As SqlCommand = New SqlCommand("userpwd_update",conndb)
Strupdate.CommandType = CommandType.StoredProcedure
Strupdate.Parameters.Add("@user_ID",userid)
Strupdate.Parameters.Add("@user_pwd",TxtNewPwd.Text.Trim())
Strupdate.ExecuteNonQuery()
conndb.Close()
Response.Write("")
End Sub
8.发送E-mail:
用户控件:
Butoon:Button1
TextBox: TxtSubject
Label:LblUsername
c#
注意:要记得引用Jmail组件
复制内容到剪贴板 程序代码
private void Button1_Click(object sender, System.EventArgs e)
{
jmail.Message Jmail=new jmail.Message();
DateTime t=DateTime.Now;
String Subject=TxtSubject.Text.Trim();;
String body="Hello"+LblUsername.Text.Trim();
String FromEmail="eastjazz@163.com";
String ToEmail=LblUseremail.Text.Trim();
//Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent=true;
//Jmail创建的日志,前提loging属性设置为true
Jmail.Logging=true;
//字符集,缺省为"US-ASCII"
Jmail.Charset="GB2312";
//信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
Jmail.ContentType="text/html";
//添加收件人
Jmail.AddRecipient(ToEmail,"","");
Jmail.From=FromEmail;
//发件人邮件用户名
Jmail.MailServerUserName="dezaiiloveu" ;
//发件人邮件密码
Jmail.MailServerPassWord="dezaistudio" ;
//设置邮件标题
Jmail.Subject=Subject;
//邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
//邮件内容
Jmail.Body=body+t.ToString();
//Jmail发送的方法
Jmail.Send("smtp.163.com",false);
Jmail.Close() ;
InkBack.Visible=true;
InkBack.NavigateUrl="index.aspx";
}
}
VB.Net
复制内容到剪贴板 程序代码
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Jmail As jmail.Message = New jmail.Message()
Dim t As DateTime = DateTime.Now
Dim Subject As String = TxtSubject.Text.Trim()
Dim body As String = "Hello"+LblUsername.Text.Trim()
Dim FromEmail As String = "eastjazz@163.com"
Dim ToEmail As String = LblUseremail.Text.Trim()
'Silent属性:如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false
Jmail.Silent=True
'Jmail创建的日志,前提loging属性设置为true
Jmail.Logging=True
'字符集,缺省为"US-ASCII"
Jmail.Charset="GB2312"
'信件的contentype. 缺省是"text/plain") : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。
Jmail.ContentType="text/html"
'添加收件人
Jmail.AddRecipient(ToEmail,"","")
Jmail.From=FromEmail
'发件人邮件用户名
Jmail.MailServerUserName="dezaiiloveu"
'发件人邮件密码
Jmail.MailServerPassWord="dezaistudio"
'设置邮件标题
Jmail.Subject=Subject
'邮件添加附件,(多附件的话,可以再加一条Jmail.AddAttachment( "c:\\test.jpg",true,null);)就可以搞定了。[注]:加了附件,讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。
'邮件内容
Jmail.Body=body+t.ToString()
'Jmail发送的方法
Jmail.Send("smtp.163.com",False)
Jmail.Close()
InkBack.Visible=True
InkBack.NavigateUrl="index.aspx"
End Sub
}
9.asp.net获取复选框所选的值
c#.net
方法1:
复制内容到剪贴板 程序代码
:
public static string GetCheckBoxListValue(CheckBoxList CBList)
{
string strTemp = "";
for (int i = 0; i < CBList.Items.Count; i++)
{
if (CBList.Items[i].Selected)
{
if (strTemp == "")
{
strTemp = CBList.Items[i].Value;
}
else
{
strTemp += "," + CBList.Items[i].Value;
}
}
}
return strTemp;
}
方法2:
复制内容到剪贴板 程序代码
CheckBoxListMessage.Text="";
for (int i=0; i
if (checkboxlist1.Items[i].Selected)
{
CheckBoxListMessage.Text += checkboxlist1.Items[i].Text + "
";
}
}
10数据库的备份与恢复
复制内容到剪贴板 程序代码
<%@ Page %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
11.C#防注入函数
复制内容到剪贴板 程序代码
public string SafeRequest(string ParaName,int ParaType)
{
//如果是1为数字,0为字符串
string Paravalue="";
Paravalue = ParaName;
if (ParaType==1)
{
if(!(IsNumeric(Paravalue)))
{
Paravalue="0";
}
}
else
{
Paravalue=Paravalue.Replace("'","’");
}
return(Paravalue);
}
public static bool IsNumeric(string strData)
{
float fData;
bool bValid = true;
if(strData.Length>12)
{
bValid = false;
}
else
{
try
{
fData = float.Parse(strData);
}
catch (FormatException)
{
bValid = false;
}
}
return bValid;
}
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=SafeRequest(TextBox1.Text,1);
}
暂时先整理到这里,慢慢再补上吧,请大家多多指教:)[em02]
[本日志由 admin 于 2006-05-22 03:51 PM 编辑]
上一篇: ASP.Net学习之常用模块整理(1)下一篇: 德仔工作室web技术电子期刊第七期(2006.6)
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 513 | 查看次数: 44475
发表评论