Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 2nd, 2004, 02:39 PM
Registered User
 
Join Date: Dec 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default dynamic linkbutton disappear when click it

I dynamicly generate some linkbutton. When I click on linkbutton, it disappears and didn't execute MyLinkButton_Click(). I think when I click linkButton, it didn't do Page_Load, but I don't know how to fix it. I want to click the generated linkbutton and show which linkbutton is clicked in lblDisplayLinkButton.

code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class test : System.Web.UI.Page
{
        protected System.Web.UI.WebControls.Literal litResults;
        protected System.Web.UI.WebControls.TextBox txtNoOfLinkButtons;
        protected System.Web.UI.WebControls.RequiredFieldValidator rfvSearch;
        protected System.Web.UI.WebControls.Button btnSearch;
        protected System.Web.UI.WebControls.Label lblDisplayLinkButton;
        protected System.Web.UI.WebControls.Literal litArray;
        protected System.Web.UI.WebControls.PlaceHolder PutLabelHere;
        protected int Labels;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (ViewState["Labels"] != null)
            {
                int NoOfLabels = (int)ViewState["Labels"];
                ShowLabels(NoOfLabels);
            }
        }

        private void ShowLabels(int NoOfLabels)
        {
            LiteralControl MyLC = new LiteralControl("
");
            for (int i = 0; i < NoOfLabels; i++)
            {
                LinkButton MyLink = new LinkButton();
                MyLink.Text = "LinkButton" + (i + 1);
                MyLink.Click += new EventHandler(MyLinkButton_Click);
                PutLabelHere.Controls.Add(MyLink);
                PutLabelHere.Controls.Add(MyLC);
            }
            if (PutLabelHere.Controls.Count > 0)
            {
                ViewState["Labels"] = NoOfLabels;
            }
            else
            {
                ViewState.Remove("Labels");
            }
        }

        public void btnGenerateLinkButtons_Click(object sender, System.EventArgs e)
        {
            if (txtNoOfLinkButtons.Text != String.Empty)
            {
                try
                {
                    ShowLabels(System.Convert.ToInt32(txtNoOfLinkButto ns.Text));
                }
                catch
                {
                }
            }
        }

        private void InitializeComponent()
        {

        }

        private void MyLinkButton_Click(object sender, System.EventArgs e)
        {
            lblDisplayLinkButton.Text = ((LinkButton)sender).Text;
        }
}

Can someone check for me where the problem is? Thanks in advanced





Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is addhandler for linkbutton.click not working rsearing ASP.NET 2.0 Basics 7 November 12th, 2007 06:06 PM
Need Help with Dynamic Link Label Click Events chobo2 C# 4 November 8th, 2007 03:47 AM
Creation of dynamic LinkButton and Adding Event sonaldewle General .NET 2 April 14th, 2006 06:49 AM
Menu disappear! Tachyophan Excel VBA 0 December 19th, 2003 03:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.