博客 (236)

string chkCode = string.Empty;
//颜色列表,用于验证码、噪线、噪点
//Color[] color ={Color.White, Color.Yellow, Color.LightBlue, Color.LightGreen,
//                Color.Orange, Color.LightCyan, Color.LightPink, 
//                Color.LightSalmon };
Color[] color = {Color.Black, Color.Blue, Color.Red, Color.DarkViolet, 
                 Color.Chocolate, Color.DarkGreen, Color.DeepSkyBlue, 
                 Color.DarkCyan };
//字体列表,用于验证码 
string[] font = {"Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh",
                 "PMingLiU", "Impact" };
//验证码的字符集,去掉了一些容易混淆的字符 
char[] character ={'2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E',
                   'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T',
                   'W', 'X', 'Y' };
Random rnd = new Random();
//生成验证码字符串 
for (int i = 0; i < 4; i++)
{
    chkCode += character[rnd.Next(character.Length)];
}
//把验证码存入session
Session["chkCode"] = chkCode;
Bitmap bmp = new Bitmap(65, 20);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);//背景色
//画噪线 
for (int i = 0; i < 0; i++)
{
    int x1 = rnd.Next(65);
    int y1 = rnd.Next(20);
    int x2 = rnd.Next(65);
    int y2 = rnd.Next(20);
    Color clr = color[rnd.Next(color.Length)];
    g.DrawLine(new Pen(clr), x1, y1, x2, y2);
}
//画验证码字符串
for (int i = 0; i < chkCode.Length; i++)
{
    string fnt = font[rnd.Next(font.Length)];
    Font ft = new Font(fnt, 15);
    Color clr = color[rnd.Next(color.Length)];
    g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr),
        (float)i * 15 + 2, (float)0);
}
//画噪点 
for (int i = 0; i < 50; i++)
{
    int x = rnd.Next(bmp.Width);
    int y = rnd.Next(bmp.Height);
    Color clr = color[rnd.Next(color.Length)];
    bmp.SetPixel(x, y, clr);
}
//清除该页输出缓存,设置该页无缓存 
Response.Buffer = true;
Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0);
Response.Expires = 0;
Response.CacheControl = "no-cache";
Response.AppendHeader("Pragma", "No-Cache");
//将验证码图片写入内存流,并将其以 "image/Png" 格式输出 
MemoryStream ms = new MemoryStream();
try
{
    bmp.Save(ms, ImageFormat.Png);
    Response.ClearContent();
    Response.ContentType = "image/Png";
    Response.BinaryWrite(ms.ToArray());
}
finally
{
    //显式释放资源 
    bmp.Dispose();
    g.Dispose();
}
xoyozo 17 年前
5,395

CatalogEdit.aspx

<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" DataTextField="catalog"
DataValueField="ID" Rows="10"></asp:ListBox>
<asp:LinkButton ID="LinkButton_up" runat="server" OnClick="LinkButton_up_Click">上移</asp:LinkButton>
<asp:LinkButton ID="LinkButton_down" runat="server" OnClick="LinkButton_down_Click">下移</asp:LinkButton>
<asp:LinkButton ID="LinkButton_del" runat="server" OnClick="LinkButton_del_Click">删除</asp:LinkButton>
<asp:Label ID="Label_alert" runat="server" ForeColor="Red" Text="Label"></asp:Label><br />
<asp:TextBox ID="TextBox_newCatalog" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButton_add" runat="server" OnClick="LinkButton_add_Click">新增</asp:LinkButton>
<asp:LinkButton ID="LinkButton_edit" runat="server" OnClick="LinkButton_edit_Click">修改</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringLink %>"
DeleteCommand="DELETE FROM [xLinkCategory] WHERE [ID] = ?" InsertCommand="INSERT INTO [xLinkCategory] ([username],[catalog],[order]) VALUES (?,?,0)"
ProviderName="<%$ ConnectionStrings:ConnectionStringLink.ProviderName %>"
SelectCommand="SELECT [ID], [catalog] FROM [xLinkCategory] WHERE ([username] = ?) ORDER BY [order],[ID] desc"
UpdateCommand="UPDATE [xLinkCategory] SET [catalog] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="catalog" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:SessionParameter Name="username" SessionField="username" Type="String" />
</SelectParameters>
<InsertParameters>
<asp:SessionParameter Name="username" SessionField="username" Type="String" />
<asp:Parameter Name="catalog" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

CatalogEdit.aspx.cs

/**//// <summary>
/// 上移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton_up_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null && ListBox1.SelectedIndex > 0)
{
string tempValue = ListBox1.SelectedValue;
string tempText = ListBox1.SelectedItem.Text;
int index = ListBox1.SelectedIndex;

ListBox1.SelectedItem.Value = ListBox1.Items[index - 1].Value;
ListBox1.SelectedItem.Text = ListBox1.Items[index - 1].Text;
ListBox1.Items[index - 1].Value = tempValue;
ListBox1.Items[index - 1].Text = tempText;

ListBox1.SelectedIndex--;

operateDB_move();
}
}
/**//// <summary>
/// 下移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton_down_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null && ListBox1.SelectedIndex < ListBox1.Items.Count - 1)
{
string tempValue = ListBox1.SelectedValue;
string tempText = ListBox1.SelectedItem.Text;
int index = ListBox1.SelectedIndex;

ListBox1.SelectedItem.Value = ListBox1.Items[index + 1].Value;
ListBox1.SelectedItem.Text = ListBox1.Items[index + 1].Text;
ListBox1.Items[index + 1].Value = tempValue;
ListBox1.Items[index + 1].Text = tempText;

ListBox1.SelectedIndex++;

operateDB_move();
}
}
/**//// <summary>
/// 添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton_add_Click(object sender, EventArgs e)
{
if (TextBox_newCatalog.Text != "")
{
//检查同名
bool goon = true;
foreach (ListItem li in ListBox1.Items)
{
if (li.Text == TextBox_newCatalog.Text)
{
goon = false;
}
}

if (goon)
{
//操作
SqlDataSource1.InsertParameters["catalog"].DefaultValue = TextBox_newCatalog.Text;
SqlDataSource1.Insert();

//设置selected
if (ListBox1.Items.Count > 0)
{
ListBox1.SelectedIndex = 0;
}
}
else
{
Label_alert.Text = "已存在";
}
}
}
/**//// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton_edit_Click(object sender, EventArgs e)
{
if (TextBox_newCatalog.Text != "" && ListBox1.SelectedItem != null)
{
int index = ListBox1.SelectedIndex;

//检查同名
bool goon = true;
foreach (ListItem li in ListBox1.Items)
{
if (li.Text == TextBox_newCatalog.Text)
{
goon = false;
}
}

if (goon)
{
//操作
SqlDataSource1.UpdateParameters["catalog"].DefaultValue = TextBox_newCatalog.Text;
SqlDataSource1.UpdateParameters["ID"].DefaultValue = ListBox1.SelectedItem.Value;
SqlDataSource1.Update();

//设置selected
ListBox1.SelectedIndex = index;
}
else
{
Label_alert.Text = "已存在";
}
}
}
/**//// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton_del_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
int index = ListBox1.SelectedIndex;
int count = ListBox1.Items.Count;

//检查该 Catalog 下是否有 Link
bool goon = true;
goon = !checkSubLink(ListBox1.SelectedValue); //下有 Link 则返回真,注意前面有个"!",有则不继续

if (goon)
{
//操作
SqlDataSource1.DeleteParameters["ID"].DefaultValue = ListBox1.SelectedItem.Value;
SqlDataSource1.Delete();

//设置selected
if (index < count - 1) //删的不是最末项
{
ListBox1.SelectedIndex = index;
}
if (index == count - 1 && index != 0) //删的是最末项,并且不是最首项
{
ListBox1.SelectedIndex = index - 1;
}
if (index == count - 1 && index == 0) //删的是最末项,并且是最首项
{
//不设索引
}
}
else
{
Label_alert.Text = "该节点下面存在Link(s),请删除后再删除该节点!";
}
}
}
xoyozo 17 年前
4,007
public static bool loadRss(string RssUrl, int RssCount)
{
bool ok = true;
XmlDocument doc = new XmlDocument();
if (RssUrl != "")
{
try
{
doc.Load(RssUrl);
XmlNodeList nodelist = doc.GetElementsByTagName("item");
XmlNodeList objItems1;

int i = 0;
int count = 0;

if (doc.HasChildNodes)
{
foreach (XmlNode node in nodelist)
{
string title = "";
string link = "";
string description = "";
string pubDate = "";
i += 1;
if (node.HasChildNodes)
{
objItems1 = node.ChildNodes;
foreach (XmlNode node1 in objItems1)
{
switch (node1.Name)
{
case "title":
title = node1.InnerText;
break;
case "link":
link = node1.InnerText;
break;
case "description":
description = node1.InnerText;
break;
case "pubDate":
pubDate = node1.InnerText;
break;
default: break;
}
}
}
if (i > RssCount)
{
break;
}
}
}
}
catch (Exception ex)
{
ok = false;
}
}

return ok;
}
xoyozo 17 年前
4,772

推荐参考更新的文章:.NET 5 / ASP.NET Core / ASP.NET Frameworks 从纯真 IP 数据库(QQWry.Dat)中查询 IP 归属地

纯真IP数据库官方下载地址:http://www.cz88.net/

之前我写过将 QQWry.Dat 转为 Access 格式后,在 ASP.NET 中通过简单算法去查询,缺点是 IP 数据库体积变大。现在有了更简单的方法:

1,下载 IPLocation.dll备用地址),将它放在网站的 Bin 目录下,将 QQWry.Dat 放在网站的任何位置(一般为 App_Data 目录)。刷新解决方案管理器。

2,在使用的页面加入引用

using IPLocation;

 

或者在 web.config 文件中,节点 pages -> controls 添加

<add tagPrefix="IPLocation" namespace="IPLocation" assembly="IPLocation"/>

 

3,调用方法:

Label1.Text = IPLocation.IPLocation.IPLocate(Server.MapPath("~/App_Data/QQWry.Dat"), TextBox1.Text.Trim());

 

方法 IPLocate() 参数一为 QQWry.Dat 路径,参数二为 IP 地址,返回 IP 归属地。

够简单吧,补充一点,在 VS2003 需要在项目中添加 IPLocation.dll 的引用。

xoyozo 17 年前
6,273

研究了两个小时,确认了一个BUG。

平台:VS2005,VS2008 (VS2003未知)

Framework:2.0,3.5 (1.1未知)

首先跟我做一遍,非常简单:

一、在 VS 中新建网站;
二、已有 Default.aspx,再建 Default2.aspx;
三、在这两个网页的 Page_Load 事件上分别加上断点
四、在 Default2.aspx 中拖入一个 ImageButton,并设该页为起始页
五、运行。

这时可以发现,程序在两处断点的地方都会停下来,而这两上网页根本就没有任何关系,只是在同一级目录而已。

经测试,ImageMap 控件也有同样的现象,其它的就没一一去试了。

建议大家尽量用Button代替ImageButton。按照下面的做法可以把Button美化成和ImageButton一样的效果,甚至更棒!

方法如下:

给 Button 加上 CssClass 属性来写样式,或在App_Themes 的 .skin 文件中定义 Button 的属性

<asp:Button runat="server" CssClass="ButtonStyle" />

 

然后在 .css 文件中处理它的样式,例如:

 

.ButtonStyle {
border:0;
color: #FFFFFF;
font-size: 14px;
font-weight:bold;
text-align: center;
vertical-align:middle;
line-height:27px;
height: 27px;
width: 77px;
background-color: transparent;
background-image: url(Images/button.gif);/*背景*/
background-position: center center;
}

 

这样就可以了,这时你会发现写在 Button 上的 Text 会随着你的鼠标按下而偏移,这个效果是不是 ImageButton 所没有的呢?

xoyozo 17 年前
5,792
方法 信息量大小 保存时间 应用范围 保存位置
Application 任意大小 整个应用程序的生命期 所有用户 服务器端
Session 小量、简单的数据 用户活动时间+一段延迟时间(一般为20分钟) 单个用户 服务器端
Cookie 小量、简单的数据 可以根据需要设定 单个用户 客户端
Viewstate 小量、简单的数据 一个Web页面的生命期 单个用户 客户端
Cache 任意大小 可以根据需要设定 所有用户 服务器端
隐藏域 小量、简单的数据 一个Web页面的生命期 单个用户 客户端
查询字符串 小量、简单的数据 直到下次页面的跳转请求 单个用户 客户端
Web.Config文件 不变、或极少改奕的小量数据 直到配置文件被更新 所有用户 服务器端
5,251