Click here to Skip to main content
15,887,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
using System.Collections;
using System.Text;

namespace WebApplication7
{
    public partial class viewad1 : System.Web.UI.Page
    {
        public string adv;
        public string usr;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDropDownList();
                FillDropDownList1();     
            }
          
            
            
        }

        protected void FillDropDownList()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sampledbConnectionString"].ConnectionString);
            conn.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter myda = new SqlDataAdapter("Select * FROM category", conn);
            myda.Fill(ds);
            conn.Close();
            catddlist.DataSource = ds;
            catddlist.DataTextField = "category_name";
            catddlist.DataValueField = "category_id";
            catddlist.DataBind();
            catddlist.Items.Insert(0, new ListItem("Select", "0"));
            subcatlist.Items.Insert(0, new ListItem("Select", "0"));
            
        }

        
        protected void FillDropDownList1()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sampledbConnectionString"].ConnectionString);
            conn.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter myda = new SqlDataAdapter("Select * FROM places", conn);
            myda.Fill(ds);
            conn.Close();
           citylist.DataSource = ds;
            citylist.DataTextField = "cityname";
            citylist.DataValueField = "city";
            citylist.DataBind();
            citylist.Items.Insert(0, new ListItem("Select", "0"));
            }

        //Double click on Company`s DropDown.;


        protected void catddlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sampledbConnectionString"].ConnectionString);
            conn.Open();

            int catid = Convert.ToInt32(catddlist.SelectedValue);
            DataSet ds = new DataSet();
            SqlDataAdapter myda = new SqlDataAdapter("Select subcategory_id,subcategoryname FROM subcategory where category_id=" + catid, conn);
            myda.Fill(ds);
            conn.Close();
            subcatlist.DataSource = ds;
            subcatlist.DataTextField = "subcategoryname";
            subcatlist.DataValueField = "subcategory_id";
            subcatlist.DataBind();
            subcatlist.Items.Insert(0, new ListItem("select", "0"));
        }

        protected void FillPage1()
        {
            ArrayList adlist = new ArrayList();
            string sub = subcatlist.SelectedValue.ToString();
            string cit = null;
            adlist = conclass1.getadbycat(sub, cit);

            Panel linkpanel = new Panel();
                
           
            int a = 0;
            foreach (adclass ad in adlist)
            {
                Label op = new Label();
                StringBuilder sb = new StringBuilder();
                HyperLink link = new HyperLink();

                sb.Append(
                    string.Format(
                        @"<table class="adtable">
            <tr>
                <th rowspan="6" width="150px"><img  runat="server" src='{6}' /></th>
                <th width="50px">Title: 
                <table><tbody><tr><table><tbody><tr><td>{1}</td></tr></tbody></table></tr></tbody></table>
            </th></tr>
            <tr>
                <th>Id: </th>
                <td>{0}</td>
            </tr>
<tr>
                <th>Price: </th>
                <td>{2} Rs</td>
            </tr>

            <tr>
                <th>Type: </th>
                <td>{3}</td>
            </tr>

            <tr>
                <th>Description: </th>
                <td>{4}</td>
            </tr>
            <tr>
                <th>Posted by: </th>
                <td>{5}</td>
            </tr>
            <tr>
                <td colspan="2">{5}</td>
            </tr>           
            
           </table> 
 ", ad.product_id, ad.title, ad.price, ad.type, ad.description, ad.email_username, ad.imagepath));


                if (sb.ToString() != "")
                    op.Text = sb.ToString();

                else
                    op.Text = "No Results";




                link.Text = "View ad";
                link.NavigateUrl = "~/userpage.aspx";

                linkpanel.Controls.Add(op);
                linkpanel.Controls.Add(link);
            }


            panelop.Controls.Add(linkpanel);
        }
            protected void FillPage()
        {   
            ArrayList adlist = new ArrayList();
            string sub = subcatlist.SelectedValue.ToString();
            string cit = citylist.SelectedValue.ToString();
            adlist = conclass1.getadbycat(sub,cit);
            int a = 0;
            Panel linkpanel = new Panel();
            foreach (adclass ad in adlist)
            {
               
                Label op = new Label();
                
                StringBuilder sb = new StringBuilder();
                Button viewbutton = new Button();
                adv = ad.product_id.ToString();
                usr=ad.email_username.ToString();
               sb.Append(
                    string.Format(
                        @"<table class="adtable">
            <tr>
                <th rowspan="6" width="150px"><img  runat="server" src='{6}' /></th>
                <th width="50px">Title: 
                <table><tbody><tr><table><tbody><tr><td>{1}</td></tr></tbody></table></tr></tbody></table>
            </th></tr>
            <tr>
                <th>Id: </th>
                <td>{0}</td>
            </tr>
<tr>
                <th>Price: </th>
                <td>{2} Rs</td>
            </tr>

            <tr>
                <th>Type: </th>
                <td>{3}</td>
            </tr>

            <tr>
                <th>Description: </th>
                <td>{4}</td>
            </tr>
            <tr>
                <th>Posted by: </th>
                <td>{5}</td>
            </tr>
            <tr>
                <td colspan="2">{5}</td>
            </tr>           
            
           </table> 
 ", ad.product_id, ad.title, ad.price, ad.type, ad.description, ad.email_username, ad.imagepath));
 
            if (sb.ToString() != "")
                op.Text = sb.ToString();

            else
                op.Text = "No Results";




            viewbutton.Text = "View Ad";
            viewbutton.Click += viewbutton_Click;
               
                
            linkpanel.Controls.Add(op);
            linkpanel.Controls.Add(viewbutton);
            }
            panelop.Controls.Add(linkpanel);
            }
           protected void viewbutton_Click(object sender, EventArgs e )
            {
                
                Response.Redirect("login.aspx");
            }
           
        
            
        protected void subcatlist_SelectedIndexChanged(object sender, EventArgs e)
        {

            FillPage1();
            if(!IsPostBack)
            FillDropDownList1();
            
}

        protected void citylist_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillPage();
        }


      
    }
}




Thanks in advance
Posted
Updated 25-Mar-14 5:43am
v3
Comments
Christian Amado 25-Mar-14 11:45am    
So, What's the problem? You paste some code and then???
Kiran Joseph Cdz 26-Mar-14 9:56am    
it is not redirecting as per button click event....

The "new EventHandler()" would work perfectly.

Also,
you can utilize ComandEventHandler as demonstrated in http://forums.asp.net/t/1286956.aspx?How+to+add+event+handlers+to+dynamic+controls[^]

as well you can refer to CommandEventHandler Delegate in
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandeventhandler(v=vs.110).aspx[^]
 
Share this answer
 
Comments
Kiran Joseph Cdz 25-Mar-14 14:14pm    
I tried it...but still not working...thanks for your answer
The line
C#
viewbutton.Click += viewbutton_Click;
is not adding the click event handler.
Try
C#
viewbutton.Click += new EventHandler(viewbutton_Click);
 
Share this answer
 
Comments
Kiran Joseph Cdz 25-Mar-14 12:46pm    
itz still not working......anyway thanks
CHill60 25-Mar-14 14:20pm    
Is the viewbutton actually appearing in linkPanel - in other words is that definitely the button you are clicking?
Kiran Joseph Cdz 26-Mar-14 14:04pm    
yeah...the button appears on my link panel....on clicking it is not redirecting
Hi Kiran,

Few questions, just to clarify couple of things in your scenario.
1. Can you see the button, repeating itself for each ad?
2. Try clicking the last 'view ad' button and does anything happen?
3. Please, change the code in your button handler to the below line and let me know what happens.
Response.Redirect("login.aspx");

Cheers
 
Share this answer
 
Comments
Kiran Joseph Cdz 26-Mar-14 0:16am    
yeah...i could see the button for each ad...but when i click it ..the page reloads and all the ads are not seen...just like the page is loaded for the first time....thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900