主题:添加动态按钮事件响应问题
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public int pagenum;
public int allnum;
protected void Page_Load(object sender, EventArgs e)
{
addlink(1);
addpagenum(1);
}
protected void link_Command(Object sender, CommandEventArgs e)
{
Response.Redirect("contents.aspx?SearchExpression=" + e.CommandName, true);
}
protected void addlink(int i)
{
sql newssqlconn = new sql();
SqlDataReader reader = newssqlconn.connop(newssqlconn.showstr);
int num = 0;
while (reader.Read())
{
num++;
if (num >= (i * 5 - 4)&& num <= (i * 5))
{
LinkButton link = new LinkButton();
Label lable = new Label();
link.Text = reader["title"].ToString();
lable.Text = "<br/>" + " " + reader["datetime"].ToString() + "<br/>";
this.Panel1.Controls.AddAt(0, link);
this.Panel1.Controls.AddAt(1, lable);
link.CommandName = reader["id"].ToString();
link.Command += new CommandEventHandler(this.link_Command);
}
}
allnum = num;
newssqlconn.sqlclose();
}
protected void addpagenum(int pagenum)
{
int j=1;
while(j<=(allnum/5+1))
{
if (j == pagenum)
{
Label lab = new Label();
lab.Text = j.ToString();
this.Panel2.Controls.Add(lab);
}
else
{
LinkButton lin = new LinkButton();
lin.Text = j.ToString();
lin.CommandName = j.ToString();
this.Panel2.Controls.Add(lin);
lin.Command += new CommandEventHandler(lin_Command);
}
j++;
}
}
[color=FF0000]此处根据数据库内数据每五行数据添加一个页面,运行后,第一次点击选择第二页可以运行,但第二次点击第三页却回到第一页了,再点击第三页才会实现,等于每两次点击才会实现一次。[/color]
void lin_Command(object sender, CommandEventArgs e)
{
int j = Convert.ToInt32(e.CommandName);
this.Panel1.Controls.Clear();
this.Panel2.Controls.Clear();
addlink(j);
addpagenum(j);
}
}
我把我的源代码给大家看看吧,数据库直接附加就可以了,在代码里改下登录密码就行了。
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
public int pagenum;
public int allnum;
protected void Page_Load(object sender, EventArgs e)
{
addlink(1);
addpagenum(1);
}
protected void link_Command(Object sender, CommandEventArgs e)
{
Response.Redirect("contents.aspx?SearchExpression=" + e.CommandName, true);
}
protected void addlink(int i)
{
sql newssqlconn = new sql();
SqlDataReader reader = newssqlconn.connop(newssqlconn.showstr);
int num = 0;
while (reader.Read())
{
num++;
if (num >= (i * 5 - 4)&& num <= (i * 5))
{
LinkButton link = new LinkButton();
Label lable = new Label();
link.Text = reader["title"].ToString();
lable.Text = "<br/>" + " " + reader["datetime"].ToString() + "<br/>";
this.Panel1.Controls.AddAt(0, link);
this.Panel1.Controls.AddAt(1, lable);
link.CommandName = reader["id"].ToString();
link.Command += new CommandEventHandler(this.link_Command);
}
}
allnum = num;
newssqlconn.sqlclose();
}
protected void addpagenum(int pagenum)
{
int j=1;
while(j<=(allnum/5+1))
{
if (j == pagenum)
{
Label lab = new Label();
lab.Text = j.ToString();
this.Panel2.Controls.Add(lab);
}
else
{
LinkButton lin = new LinkButton();
lin.Text = j.ToString();
lin.CommandName = j.ToString();
this.Panel2.Controls.Add(lin);
lin.Command += new CommandEventHandler(lin_Command);
}
j++;
}
}
[color=FF0000]此处根据数据库内数据每五行数据添加一个页面,运行后,第一次点击选择第二页可以运行,但第二次点击第三页却回到第一页了,再点击第三页才会实现,等于每两次点击才会实现一次。[/color]
void lin_Command(object sender, CommandEventArgs e)
{
int j = Convert.ToInt32(e.CommandName);
this.Panel1.Controls.Clear();
this.Panel2.Controls.Clear();
addlink(j);
addpagenum(j);
}
}
我把我的源代码给大家看看吧,数据库直接附加就可以了,在代码里改下登录密码就行了。