Binding dynamic datalist in C#

I am trying to create a dynamic Data List bind with database. I can easily create this but I am not able to make the item Command of this Data List. Please help me. Here is my code below

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            categorybinding();
        }
    }

    public void categorybinding()
    {
        int totalcate = (from x in ebooks.books_category select x).Count();

        var ra = (from x in ebooks.books_category select x);

        DataList dl = new DataList();
        dl.ItemTemplate = new DatalistLabelColumnBind();
        dl.DataSource = ra;
        dl.DataBind();

        form1.Controls.Add(dl);
        dl.ItemCommand += new DataListCommandEventHandler(this.ItemCommandHandler);

    }

    public void ItemCommandHandler(object sender, DataListCommandEventArgs e)
    {
        Response.Redirect("NewPage.aspx?"+e.CommandArgument.ToString());
    }


//Create a new class implementing ITemplate
public class DatalistLabelColumnBind : ITemplate
{
    public DatalistLabelColumnBind()
    {
        //Add constructor 
    }

    public void InstantiateIn(Control container)
    {
        LinkButton label1 = new LinkButton();
        label1.DataBinding += new EventHandler(this.BindLabelColumn);
        container.Controls.Add(label1);
    }
    public void BindLabelColumn(object sender, EventArgs e)
    {

        LinkButton lbl = (LinkButton)sender;
        DataListItem  container = (DataListItem)lbl.NamingContainer ;
        String strVals = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "books_category1"));
        lbl.CommandArgument = Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem, "id_books"));
        lbl.Text = strVals;
    }
}

My Problem :

  1. My Data List easily added on the page but when I click on the Link Button which is added in the Data List is does not Redirect to the NewPage.aspx

Help me out..

Answers


I think your Response.Redirect is not resolving to the intended page.

Try:

Response.Redirect("~/NewPage.aspx?"+e.CommandArgument.ToString());

Write categorybinding() function after the if condition in Page_Load. And ItemCommandHandler will definitely work.


Need Your Help

Specify relative path server in file_put_content() in CakePHP

php cakephp curl relative-path

I've been looking for a method for putting a relative path into the file_put_content() function of PHP. Basically I want to copy one file to one of the directory of my server. Here is what I've don...

Accessing the tomcat manager console in an EC2 instance in the private subnet of VPC

tomcat amazon-ec2 vpc

We have an environment where in we have two EC2 instances, one in the public subnet of the VPC which is our web server and the other in the private subnet of the VPC, which is pur Application serve...