1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Link Button

Discussion in 'C#' started by kharearch, Mar 4, 2014.

  1. #1
    I am dynamically creating links button and attaching events to it. It is working fine when I am invoking it from page_load or page_init event. but when I am calling it from a button click event it is not working. could any body solve my problem. thanks.
    my code is following

    public partial class Default4 : System.Web.UI.Page
    {

    SqlConnection conn = new SqlConnection("Data Source=COMPUTER6\\SQLEXPRESS;Initial Catalog=test_SRS;Integrated Security=True");
    string ConnnectionString = "Data Source=COMPUTER6\\SQLEXPRESS;Initial Catalog=test_SRS;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
    test2();
    }

    protected void test2()
    {
    DataSet d = new DataSet();
    LinkButton[] links = new LinkButton[5];
    d = binddata();
    int r = d.Tables["House_hold"].Rows.Count;
    for (int i = 0; i < r; i++)
    {


    links = new LinkButton();

    links.ID = "link" + i;


    links.Text = d.Tables["House_hold"].Rows[6].ToString() + "<br /><br />";

    links.Click += new System.EventHandler(Lnkbut_Click);
    Panel1.Controls.Add(links);

    }

    }
    protected DataSet fetchdata(string a)
    {

    DataSet ds = new DataSet();

    if (conn.State == ConnectionState.Open)
    conn.Close();

    string connectionString = "Data Source=COMPUTER6\\SQLEXPRESS;Initial Catalog=test_SRS;Integrated Security=True";


    conn.ConnectionString = ConnnectionString;
    conn.Open();
    // Create a data adapter
    String s = "SELECT * FROM HH_Member where HH_ID='" + a.Substring(0,2) + "'";
    //where State='" + DropDownList1.Text.Trim() + "' and SUB_EB='" + DropDownList5.Text.Trim() + "'";
    SqlDataAdapter da = new SqlDataAdapter(s, conn);
    da.Fill(ds, "HH_Member");
    return ds;

    }

    void Lnkbut_Click(object sender, EventArgs e)
    {
    DataSet d2 = new DataSet();
    Label1.Text = "hello";

    Response.Write(e.ToString());
    Response.Write("hello");
    LinkButton b = (LinkButton)sender;
    string s = b.Text;
    Label1.Text = s;
    d2=fetchdata(b.Text);
    GridView1.DataSource = d2.Tables["HH_Member"].DefaultView;
    GridView1.DataBind();


    }

    protected DataSet binddata()
    {
    if (conn.State == ConnectionState.Open)
    conn.Close();

    ConnnectionString = "Data Source=(local);Initial Catalog=SRS;Integrated Security=True";



    conn.ConnectionString = ConnnectionString;
    conn.Open();
    // Create a data adapter
    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM House_hold", conn);
    // Create DataSet, fill it and view in data grid
    DataSet ds = new DataSet();
    da.Fill(ds, "House_hold");

    return ds;
    }
     
    kharearch, Mar 4, 2014 IP
  2. advis

    advis Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #2
     protected void Page_Init(object sender, EventArgs e)
            {
                this.AddControl();
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            private void AddControl()
            {
                LinkButton links = new LinkButton();
    
                links.ID = "lnk1";
                links.Text = "ClickMe";           
                links.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
    
                links.Click += new EventHandler(links_Click);
                this.Panel1.Controls.Add(links);
            }
    
            void links_Click(object sender, EventArgs e)
            {
                Response.Write("hello");
            }
    Code (markup):
    Try that code.
    However, If I were you, I would replace that with a repeater, easier to maintain.
    Cheers,
     
    advis, Mar 4, 2014 IP
  3. kharearch

    kharearch Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
     
    kharearch, Mar 4, 2014 IP
  4. kharearch

    kharearch Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Sir, I have seen your code in which you have called addcontrol method from page_init event. but I want to dynamically create control from button_click or some other event. Because its based on table. I am searching in the table then based on that filter I want to show matching record, and number of records shown will depend on some parameter. So I want to say I can not use page_init event. event should be called after accepting parameter. So will you please help me.
     
    kharearch, Mar 4, 2014 IP
  5. advis

    advis Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    why not use repeater and populate your repeater with data from your table ?
     
    advis, Mar 4, 2014 IP
  6. kharearch

    kharearch Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    I am told to use link button. and after accepting certain parameter I have to show data from table based on those parameter. so I can not use page_load and page_init event.
     
    kharearch, Mar 4, 2014 IP
  7. advis

    advis Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #7
    Repeater is a template based control to render collection of data.
    Use a repeater, put your link button into repeater and load the data from your table into repeater in your onclick event.
     
    advis, Mar 4, 2014 IP