一、分页代码
.cs页面中的代码
(1)、数据的绑定
//从传到该页面的请求对象的参数集合中获取kindId
int kindId = 0;
try
{
kindId = Int32.Parse(Request["typeid"]);
}
catch
{
kindId = 0;
}
//接收kindId参数的值来调用GetNewsRows函数来控制more.aspx里的新闻列表
GridView1.DataSource = GetNewsRows("|" + kindId.ToString() + "|");
GridView1.DataBind();
(2)
/*GridView分页控件的使用*/
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
if (GridView1.EditIndex != -1)
{
e.Cancel = true;
int newPageNumber = e.NewPageIndex + 1;
}
else
{
//main codes go here
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
.aspx中该事件的调用为: OnPageIndexChanging="GridView1_PageIndexChanging"
二、前台.aspx中的GridView
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" Width="550px" CellPadding="3" CellSpacing ="3" BorderStyle="None"
GridLines="None" Height="150px" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging">
<Columns>
<asp:TemplateField HeaderText="新闻标题">
<ControlStyle CssClass="bb" />
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="images/shipin_dian.gif"
/>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("newID", "info.aspx?id={0}")
%>'
Text='<%# Eval("title") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="addTime" HeaderText="发布时间" />
</Columns>
</asp:GridView>
三、截取字符串的代码
(1)成功
cs页面中定义函数为:
public static string GetFirstString(string stringToSub, int length)
{
Regex regex = new Regex("[/u4e00-/u9fa5]+", RegexOptions.Compiled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
bool isCut = false;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
}
if (nLength > length)
{
isCut = true;
break;
}
}
if (isCut)
return sb.ToString() + "..";
else
return sb.ToString();
}
aspx页面中调用:
<%# GetFirstString(DataBinder.Eval(Container.DataItem, "Title").ToString(),6)%> 就可以了
CSDN摘选代码:
四、用web.config配置验证登陆 尚未测试过 摘自:http://www.soso.com/q?w=RedirectFromLoginPage%
28Server&ch=t.d
1、新建一个文件夹,名为user(表示仅是用户才可以访问)
2、新建anonymous文件夹(表示匿名可以访问)
3、在根web.config中加入以下内容(在/system.web后面)
<location path ="user">
<system.web>
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
</system.web>
</location>
4、把需要是用户才能访问的东东全部移到user下。
5、自己写验证逻辑。select count(id) from tablename where user=@user and pwd=@pwd
如果通过你的验证逻辑,然后,
'下面发放票据
FormsAuthentication.SetAuthCookie(Server.HtmlEncode(txt_usr.Text), cbx_member.Checked '这是一个checkbox控件)
'转到默认页 FormsAuthentication.RedirectFromLoginPage(Server.HtmlEncode(txt_usr.Text),
cbx_member.Checked '一样)
五、
关于文件下载系统的问题,怎么在未登陆的情况下,即使知道下载文件的地址,也不能让下载?
(1)、web.config中
<authentication mode="Forms">
<forms defaultUrl="admin/?" loginUrl ="login/" name="study" path="/" protection="All"
timeout="60">
<credentials passwordFormat ="Clear">
<user name ="wyp" password ="wyp"/>
</credentials>
</forms>
</authentication>
(2)
1、新建login,admin,download目录
login 中 含 default.aspx(登录页面)
admin 中 随意文件
download中 放 下载文件 如*.rar
2、web.config,最后一个</configuration>前加入
<location path ="download">
<system.web>
<authorization>
<deny users ="?"/>
</authorization>
</system.web>
</location>
3、iis中找到你的这个应用名,属性,配置,添加,可执行文件中填写
C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll(我的xp系统 .net 2.0)
扩展名中填写.rar,确定。就可以了。
六、不同文件夹可以用不同的web.config吗,为什么我的总是出错,
同文件夹可以用不同的web.config吗?
----------------------------------------
可以
<configuration>
<system.web>
A项目的配置
</system.web>
<location path="B的目录">
<system.web>
将B项目的配置移到这里
</system.web>
</location>
</configuration>