读书人

Datalist_itemCommand不实施

发布时间: 2013-10-14 12:54:46 作者: rapoo

Datalist_itemCommand不执行
目的:点击Datalist里的linkbutton,跳转到相对应的页面。


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bind();
}
}
void bind()
{
string str = @"Data Source=.\sqlexpress;Initial Catalog=News;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
string sqlStr = "select * from News";
SqlDataAdapter sda = new SqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "nid");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["nid"].DefaultView;
this.DataList1.DataSource = pds;
this.DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Entity.News n = new News();
n.Nid = ((Label)(e.Item.FindControl("nidLabel"))).Text;
int r = Business.NewsBusiness.GetCommentAmount(n);
Label lbl = (Label)e.Item.FindControl("lblInfo");
if (lbl != null)
{
lbl.Text = r.ToString();
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
Entity.News n = new News();
n.Nid = ((Label)(e.Item.FindControl("nidLabel"))).Text;
string r = Business.NewsBusiness.GetNewsUrl(n);
if (e.CommandName == "detail")
{
((LinkButton)(e.Item.FindControl("LinkButton1"))).PostBackUrl = r;
}
Session["nid"] = ((Label)(e.Item.FindControl("nidLabel"))).Text;
}

在网上查了下,if (!Page.IsPostBack)判断了,绑定的也没错,但不执行。 datalist
[解决办法]
你在条件判断里面,设置了PostBackUrl,并没有触发要跳转啊。

应该点第二次的时候才 会跳吧。

个人认为,你应该在绑定阶段,就把linkbutton的PostBackUrl的值给绑定进去,那么后面点击了才会跳转吧.

个人看法..
[解决办法]
引用:
Quote: 引用:

string r = Business.NewsBusiness.GetNewsUrl(n);
if (e.CommandName == "detail")
{
((LinkButton)(e.Item.FindControl("LinkButton1"))).PostBackUrl = r;
}
把这些代码放到你的 DataList1_ItemDataBound 方法中,就是在绑定阶段设置linkbutton的PostBackUrl

不行,System.Web.UI.WebControls.DataListItemEventArgs”不包含“CommandName”的定义。

把if语句去掉不就行了,数据绑定阶段又不用判断用户执行了哪种操作

读书人网 >asp.net

热点推荐