咨询电话:186 7916 6165 咨询电话:186 7916 6165 (微信同号)    在线QQ:181796286
NEWS BLOG ·
学无止境
关注开优网络 关注前沿
ASP.NET Web 开发之购物车
ASP.NET Web 开发之代码生成及增、删、改

ASP.NET常用服务器控件方法及属性

发表日期:2015-09-26    文章编辑:南昌开优网络    浏览次数:3914    标签:ASP.NET应用

//控件DorpDownList
ddltitle.DataSource=new Xiaobin.Shop.DAL.HelpDAL().GetList(""); //指定数据源
ddltitle.DataTextField="title";    //显示的文本
ddltitle.DataValueField="id";    //选择的字段
ddltitle.DataBind();    //绑定
ddltitle.Items.Add(new ListItem("选择类别", "0"));    //在数据源最后加上一项
ddltitle.Items.Insert(0,new ListItem("选择类别", "0"));    //在指定的位置加上一项

//绑定DataSet数据
string title=Request.QueryString["title"];
if(string.IsNullOrEmpty(title))
{
    title="关于我们";
}
litH1.Text=title;
title=xiaobin.Utility.Tool.GetSafeSQL(title);
DataSet ds=new DAL.HelpDAL().GetList("title='"+title+"'");    //绑定DataSet
if(ds.Tables.Count>0&&ds.Table[0].Rows.Count>0)    //如果表的数量和第一个表的行数大于0
{
    litbody.Text=ds.Table[0].Rows[0]["body"].ToString(); //取出第一个表的第一行的数据
}

//Repeater的内循环事件
外循环
private void BindRep()
{
rep.DataSource = dao.GetList("*", "createdate", "asc", anp.PageSize, anp.CurrentPageIndex, GetCond());
rep.DataBind();
}
内循环     双击事件中的ItemDataBound    如显示楼层方法
int x = 0;    
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Panel pan = e.Item.FindControl("pan") as Panel;    //FindControl 尝试转换及rep控件的服务器控件
Literal litx = e.Item.FindControl("litx") as Literal;
Literal litreply = e.Item.FindControl("litreply") as Literal;    
if (litreply.Text.Trim().Length == 0)    
{
pan.Visible = false;
}
else
{
pan.Visible = true;
}
litx.Text = (anp.StartRecordIndex + x).ToString();    //StartRecordIndex 为anp控件中的当前索引   升序排序
                litx.Text=(anp.RecordCount - (anp. StartRecordIndex + x).Tostring());    //降序排序
x++;
}
}

//Repeater控件的嵌套使用
//绑定顶级分类     
repca1.DataSource = new DAL.CategoryDAL().GetListArray("pid=0");
repca1.DataBind();
//循环repca1
protected void repca1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
        //ListItemType.Item 奇数行  ListItemType.AlternatingItem 偶数行
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater repca2 = e.Item.FindControl("repca2") as Repeater;
                //在rep1中加上隐藏域用于绑定rep2即 value='<%#Eval("id")>'
HiddenField hfid1 = e.Item.FindControl("hfid1") as HiddenField;
Repeater repca3 = e.Item.FindControl("repca3") as Repeater;
repca2.DataSource = new DAL.CategoryDAL().GetListArray("pid=" + hfid1.Value);
repca2.DataBind();
}
}
//循环repca2
protected void repca2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
Repeater repca3 = e.Item.FindControl("repca3") as Repeater;
HiddenField hfid2 = e.Item.FindControl("hfid2") as HiddenField;
repca3.DataSource = new DAL.CategoryDAL().GetListArray("pid=" + hfid2.Value);
repca3.DataBind();
}
}
//Repeater控件的分割属性
SeparatorTemplate

//通过Model获取RadioButtonList、DropDownList选中的值
ListItem li = rademail.Items.FindByValue(model.isopenemail.ToString()); //
if (li != null)
{
li.Selected = true;    //列表项等于model中的字段值时为真及显示选中的字段
}
li = radsex.Items.FindByValue(model.sex.ToString());
if (li != null)
{
li.Selected = true;
}
li = ddlprovince.Items.FindByValue(model.province);
ddlprovince.ClearSelection(); //清空dll再重新获取
if (li != null)
{
li.Selected = true;
}
li = ddlcity.Items.FindByValue(model.city);
ddlcity.ClearSelection();
if (li != null)
{
li.Selected = true;
}

//DropDownList的条件绑定
选择查看方式:
<asp:DropDownList ID="ddlorder" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlorder_SelectedIndexChanged">
<asp:ListItem Text="↓按上架时间降序排列" Value="0"></asp:ListItem>
<asp:ListItem Text="↑按上架时间升序排列" Value="1"></asp:ListItem>
<asp:ListItem Text="↓按会员价降序排列" Value="2"></asp:ListItem>
<asp:ListItem Text="↑按会员价升序排列" Value="3"></asp:ListItem>
</asp:DropDownList>
//绑定列表
private void BindRep()
{
string order = "createdate";
string ordertype = "desc";
switch (ddlorder.SelectedValue)
{
case "0":
order = "createdate";
ordertype = "desc";
break;
case "1":
order = "createdate";
ordertype = "asc";
break;
case "2":
order = "memberprice";
ordertype = "desc";
break;
case "3":
order = "memberprice";
ordertype = "asc";
break;
default:
break;
}
rep.DataSource = new DAL.ProductDAL.GetList("*", order, ordertype, anp.PageSize, anp.CurrentPageIndex, GetCond());
rep.DataBind();
}
//选择排序方式
protected void ddlorder_SelectedIndexChanged(object sender, EventArgs e)
{
BindRep();
}

TextMode="MultiLine"  前台显示原样显示出box.Replace("\n", "<br />")    //把换行替换成br

.Net中页面刷新时保持滚动条不动
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="Default" Title="" MaintainScrollPositionOnPostback="true" %>
MaintainScrollPositionOnPostback属性可以让滚动条回到原位置