日期:2014-05-17  浏览次数:20427 次

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)判断了,绑定的也没错,但不执行。