62,266
社区成员
发帖
与我相关
我的任务
分享if(! page.isPostBack)
{
....
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SBindRe();
dltProduct.DataSource = pds();
dltProduct.DataBind();
}
}
//导航栏绑定
private void SBindRe()
{
string qstr = HttpContext.Current.Request.QueryString["CategoryID"].ToString();
SqlConnection sqlConn = new SqlConnection(constr);
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = sqlConn;
//查询子类别,显示导航栏
sqlComm.CommandText = "select * from SecCategory where CategoryID=" + qstr;
SqlDataAdapter sqlAptS = new SqlDataAdapter(sqlComm);
DataSet dss = new DataSet();
sqlAptS.Fill(dss, "VSecCategory");
repSMenu.DataSource = dss.Tables["VSecCategory"].DefaultView;
repSMenu.DataBind();
}
//动态链接,
public void LinkButton_Click(Object sender, EventArgs e)
{
//我想一夜,别人一句话OK!
LinkButton tmpLbt = new LinkButton();
tmpLbt = ((LinkButton)sender);
Label1.Text = tmpLbt.Text;
SecCName = tmpLbt.Text.ToString();
//Click事件触发在DataList控件上绑定产品内容
dltProduct.DataSource = pds();
dltProduct.DataKeyField = "ProductID";
dltProduct.DataBind();
}
private PagedDataSource pds()
{
SqlConnection sqlConn = new SqlConnection(constr);
SqlCommand sqlComm = new SqlCommand();
sqlComm.Connection = sqlConn;
PagedDataSource pds = new PagedDataSource();
//1.首次加载页面,页面显示首子类产品内容
if (!IsPostBack)
{
//string qstr = HttpContext.Current.Request.QueryString["CategoryID"].ToString();
sqlComm.CommandText = "select SecCategoryID from SecCategory where CategoryID=" + qstr;
//返回子类表首条记录,首列即Id
sqlConn.Open();
string Id = (sqlComm.ExecuteScalar()).ToString();
SecCId = Id;
sqlConn.Close();
//根据SecCategoryID查询产品绑定到DataList控件
sqlComm.CommandText = "select * from Product where SecCategoryID=" + Id;
SqlDataAdapter sqlAptP = new SqlDataAdapter(sqlComm);
DataSet dsp = new DataSet();
sqlAptP.Fill(dsp, "VProduct");
//PagedDataSource pds = new PagedDataSource();
pds.DataSource = dsp.Tables["VProduct"].DefaultView;
}
//2.点击导航时根据导航显示产品信息
else
{
//根据SecCategory Name 查询出SecCategoryID
sqlComm.CommandText = "select SecCategoryID from SecCategory where Name='" + SecCName + "'";
sqlConn.Open();
string Id = (sqlComm.ExecuteScalar()).ToString();
SecCId = Id;
sqlConn.Close();
//根据SecCategoryID查询产品绑定到DataList控件
sqlComm.CommandText = "select * from Product where SecCategoryID=" + Id;
SqlDataAdapter sqlAptP = new SqlDataAdapter(sqlComm);
DataSet dsp = new DataSet();
sqlAptP.Fill(dsp, "VProduct");
//PagedDataSource pds = new PagedDataSource();
pds.DataSource = dsp.Tables["VProduct"].DefaultView;
}
//PagedDataSource属性设置
pds.AllowPaging = true;//允许分页
pds.PageSize = 2;//单页显示项数
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
return pds;
}
protected void dltProduct_ItemDataBound(object sender, DataListItemEventArgs e)
{
//限制在Datalist脚模板中查找
if (e.Item.ItemType == ListItemType.Footer)
{
DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");
HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");
HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");
HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");
HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");
pds().CurrentPageIndex = ddlp.SelectedIndex;
int n = Convert.ToInt32(pds().PageCount);//n为分页数
int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页索引
Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);
//根据总页数加载DropList列表
if (!IsPostBack)
{
for (int j = 0; j < n; j++)
{
ddlp.Items.Add(Convert.ToString(j + 1));
}
}
if (i <= 0)//IsFirstPage
{
lpfirst.Enabled = false;
lpprev.Enabled = false;
lplast.Enabled = true;
lpnext.Enabled = true;
}
else
{
lpprev.NavigateUrl = Request.CurrentExecutionFilePath +"?CategoryID="+qstr+"&page=" + (i - 1);
}
if (i >= n - 1)//IsLastPage
{
lpfirst.Enabled = true;
lplast.Enabled = false;
lpnext.Enabled = false;
lpprev.Enabled = true;
}
else
{
// Request.CurrentExecutionFilePath为当前请求虚拟路径
lpnext.NavigateUrl = Request.CurrentExecutionFilePath + "?CategoryID=" + qstr + "&page=" + (i + 1);
}
lpfirst.NavigateUrl = Request.CurrentExecutionFilePath + "?CategoryID=" + qstr + "&page=0";//向本页传递参数page
lplast.NavigateUrl = Request.CurrentExecutionFilePath + "?CategoryID=" + qstr + "&page=" + (n - 1);
ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号
}