Event on dynamically created linkbutton in C#


A

Alice Lee

Hi,

My web from has one button and by clicking this button a list of
linkbuttons must be dynamically displayed based on information in
database. Then click any one of these linkbuttons another set of
linkbuttons will be displayed, and so forth... My code works, but I have
to click twice every time to get it running correctly. I can not figure
out what's wrong in my code.

My code:

public class WebForm1 : System.Web.UI.Page
{
static string conn = ConfigurationSettings.AppSettings.Get("ConnStr");
protected SqlConnection mysqlConn = new SqlConnection(conn);
protected System.Web.UI.WebControls.Button button;
protected System.Web.UI.WebControls.Panel panel;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
Session["parentId"] = 0;
else
CreateDynamicButtons();
}

private void CreateDynamicButtons()
{
int parentId = (int)Session["parentId"];
SqlCommand cmd = mysqlConn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select categoryname from mycategory where
parentid=" + parentId;

SqlDataAdapter myAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
myAdapter.SelectCommand = cmd;
myAdapter.Fill(ds, "mycategory");

DataTable dt = new DataTable();
dt = ds.Tables[0];

if(panel.Controls.Count!=0)
panel.Controls.Clear();

Table table = new Table();
for (int i=0; i<dt.Rows.Count; i++)
{
LinkButton lbt = new LinkButton();
TableCell cell = new TableCell();
TableRow row = new TableRow();

lbt.Text = dt.Rows.ItemArray[0].ToString();
lbt.Font.Size = FontUnit.Medium;
lbt.CommandName = "CategoryName";
lbt.CommandArgument = lbt.Text;
lbt.Command += new
System.Web.UI.WebControls.CommandEventHandler(lbt_click);
cell.Controls.Add(lbt);
row.Cells.Add(cell);
table.Rows.Add(row);
}

panel.Controls.Add(table);
panel.Visible = true;
}

private void lbt_click(object sender, CommandEventArgs e)
{
LinkButton lbtSender = (LinkButton)sender;

string query = "Select CategoryId from mycategory where
categoryname='" + lbtSender.CommandArgument + "'";

SqlCommand cmd = new SqlCommand ( query, mysqlConn );
cmd.Connection.Open();
Session["parentId"] = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
CreateDynamicButtons();
}

private void button_Click(object sender, System.EventArgs e)
{
Session["parentId"] = 0;
CreateDynamicButtons();
}
}

Thank you in advance.

Alice



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Ad

Advertisements

M

Mohamoss

Hi Alice
I have seen behavior like this when you are creating more that one instance
of the same control ( calling the new statement " the one that allocate
memory " more than one time). Mostly this is the cause of what you have.
try to check for that by searching for the new keyword . Make sure also
that the new keyword is not called inside a loop block .
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
A

Alice Lee

Hi,Mohamoss

What does "new keyword" mean?

I guess the problem is somewhere in attaching event handler to
dynamically created linkbuttons between pages postback. But I'm not
sure...

Please give me more detailed info. Thank you!

Alice


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Ad

Advertisements

M

Mohamoss

Hi Alice
You saying that the link button is created dynamically make me almost sure
that the problem is what I think it is . You are calling the new
constructor two times. What I mean by the new keyword Is when you say
Object name;
name = new Object(); // you allocate memory to the object
or it could all be done once
Object name = new Object();
For sure you know what this is but may be I was not clear in my previous
post . my advice again is
Make sure that the constructor is not called twice for I have seen many
cases where this was the cause of such problem. The problem is , you have
two objects with the same name , only one of them has the event associated
with the handler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top