0
votes

I am trying to generate a dynamic html button in c#,But buttons are being getting generated when i ran the code.I am trying to generate a dynamic buttons in 2 different ways but none is working out for me. Please let me if anything wrong in code.I tried in page _Load and page_init events.Here is my code i am using it .........

protected void page_Init(object sender, EventArgs e)

{             

    Table tblControls = new Table();

    TableRow trControl1 = new TableRow();

    TableCell tdControl1 = new TableCell();

    tdControl1.Text = "<input id='Submit" + "' runat='server' type='button' value='Submit' onclick='alert('Yahooooo');' onserverclick='Submit_Click'></input>";

    trControl1.Cells.Add(tdControl1);                   

    tblControls.Rows.Add(trControl1);





    int i = 1;

    string strMessage = "hi";

    HtmlButton htmSubmit = new HtmlButton(); 

    htmSubmit.ID = "Submit" + i; 

    htmSubmit.InnerHtml = "Submit";



    htmSubmit.Attributes.Add("onclick", "alert('Yahoooooo');");





    htmSubmit.Attributes.Add("GuestId", "GuestUserId" + i.ToString()); 

    htmSubmit.Attributes.Add("UserId", "UserId" + i.ToString()); 

    htmSubmit.Attributes.Add("Message", strMessage); 

    htmSubmit.ServerClick += new System.EventHandler(this.Submit_Click);

}





protected void Submit_Click(object sender, EventArgs e)

{

    HtmlButton btn = (HtmlButton)sender; 

    string strGuestUserId = btn.Attributes["GuestId"]; 

    string strUserId = btn.Attributes["UserId"]; 

    string strMessage = btn.Attributes["Message"]; 

    Response.Write("Your have clicked " + btn.ID.ToString() + "<br/>" + strGuestUserId + "<br/>" + strUserId + "<br/>" + strMessage);

}
1
The question is a bit messy, please tidy it up. - Eran Betzalel
Thanks dandres109.... Yes u are right i was misssing page.control.add() statement.When i added it started working.... Thank u very much...... - vinodacharyabva
It's still messy. Please tidy it up. Your first sentence is in "code mode," your first line of code is in "text mode," and your code has extra line breaks. - Joe Chung

1 Answers

1
votes

It looks like you're missing the statement where you add tblControls to the Page.Controls collection. You probably also intend for the dynamic button to be added to the tdControl1 control.