Click here to Skip to main content
15,888,283 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have created a dynamic button and a click event for it.
That event is working.

The problem is that i have created 1 more button dynamically inside that , which has a click event.
This event is not triggering
C#
//Method1
void ChatList()
{
    DataTable dtNames = help.GetTable("select userid from tbluser where Login='1'");

    for (int i = 0; i < dtNames.Rows.Count; i++)
    {
        HtmlGenericControl table = new HtmlGenericControl("table");
        HtmlGenericControl tr = new HtmlGenericControl("tr");
        HtmlGenericControl tdleft = new HtmlGenericControl("td");
        HtmlGenericControl tdright = new HtmlGenericControl("td");
        HtmlGenericControl light = new HtmlGenericControl("img");
        light.Attributes.Add("src", "images/chaticon.png");
        light.Attributes.Add("class", "loglight");
        tdright.Controls.Add(light);

        LinkButton names = new LinkButton();
        names.Attributes.Add("class", "list");
        names.Attributes.Add("style", "text-decoration:none;color:#8997ef;");
        names.ID = dtNames.Rows[i].ItemArray[0].ToString();
        names.Click += new EventHandler(ShowChat_Click);
        names.Text = dtNames.Rows[i].ItemArray[0].ToString();
        tdleft.Controls.Add(names);
        tr.Controls.Add(tdleft);
        tr.Controls.Add(tdright);
        table.Controls.Add(tr);
        // tr.Controls.Add(new LiteralControl(""));
        plcChatList.Controls.Add(table);
    }
}

//My first click event(WORKING)
public void ShowChat_Click(object sender, EventArgs e)
{    
     LinkButton names = new LinkButton();
     names = sender as LinkButton;
     string Id = names.ID;
     Label1.Text = Id;

     TextBox ChatBox = new TextBox();
     ChatBox.ID = names.ID + "Txt";

     HtmlGenericControl table = new HtmlGenericControl("table");
     HtmlGenericControl tr = new HtmlGenericControl("tr");
     HtmlGenericControl tdL = new HtmlGenericControl("td");
     HtmlGenericControl tdR = new HtmlGenericControl("td");
   
     HtmlGenericControl sendBtnImg = new HtmlGenericControl("img");
     sendBtnImg.Attributes.Add("src", "images/sendbtn.png");
     sendBtnImg.Attributes.Add("class", "sendbtn");
    
     LinkButton Send = new LinkButton();
     Send.ID = names.ID;

     Send.Click += new EventHandler(ShowChatMsg_Click);
     if (!Page.IsPostBack)
     {
         Send.Click += new EventHandler(ShowChatMsg_Click);
    
     }

     Send.Controls.Add(sendBtnImg);
     tdL.Controls.Add(ChatBox);
     tdR.Controls.Add(Send);
     tr.Controls.Add(tdL);
     tr.Controls.Add(tdR);
     table.Controls.Add(tr);
     plcChatWindow.Controls.Add(table);
}

//My second click event (NOT WORKING)

public void ShowChatMsg_Click(object sender, EventArgs e)
{
    LinkButton Send = new LinkButton();
    Send = sender as LinkButton;
    string Id = Send.ID;
   
    DataTable dt = help.GetTable("select text from message where userID='"+Id+"'");

    Label lblText = new Label();
    lblText.ID = Id + "chat";
    Label2.Text = Id;
    lblText.Text = dt.Rows[0].ItemArray[0].ToString();
    plcAllChat.Controls.Add(lblText);
}
Posted
v2

1 solution

Ideally, event handlers should be added/registered in page_load event. Check the below sample code.

C#
Page_Load()
{
            Button b = new Button();
            b.ID = "btnMyButton"; 
            b.Text = "some text";
            
            b.Command += new CommandEventHandler(b_Command); //handler
}
 
Share this answer
 
v3
Comments
Athulk 18-May-13 2:40am    
Thanks for your response Hameed, But in b.ID=? what will be the id, im creating the button in a method and how can i get that id in the pageload. Please help me
Mohammed Hameed 18-May-13 5:27am    
You're welcome. b.ID is nothing but the assigned id for your button as in second line -> b.ID = 1;
You can assign any name for the ID, its not like you assign only 1 (that was only an example code for your reference). And its not compulsory to mention CommondArgument.
Ok, better I will modify this example code just for your clarity.
Athulk 19-May-13 23:24pm    
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