locked
creat link button in code behind RRS feed

  • Question

  • User-646447061 posted

    hello, i create table use this code 
    html.Append("<table>");

    html.Append("<tr>");

    html.Append("<td>");

    html.Append("<td>");

    html.Append("</tr>");

    html.Append("</table>");

    and i wan't create tool link button in the tag <td> there</td>, how to ?

    i want like this 

    <td>

    <asp:LinkButton ID="lnkbtndelete" runat="server"  CommandArgument='row["id"]'  UseSubmitBehavior="false" >Delete</asp:LinkButton>

    </td>

    Thursday, October 13, 2016 10:58 AM

Answers

  • User-1838255255 posted

    Hi motorreve00,

    As far as I know , you can instantiate a new control in code behind , then add attribution into it . I make a sample for your reference :

    Sample code:

     protected void Page_Load(object sender, EventArgs e)
            {
                Table tab = new Table();
                tab.ID = "box";
                tab.Style.Add("border", "1px solid red");
                TableRow tr = new TableRow();
                tab.Rows.Add(tr);
                TableCell cell = new TableCell();
                tr.Cells.Add(cell);
                this.form1.Controls.Add(tab);
    
                PlaceHolder ph = new PlaceHolder();
                ph.ID = "placeHolder1";
                tab.Rows[0].Cells[0].Controls.Add(ph);
    
                LinkButton likbt = new LinkButton();
                likbt.Text = "Delete";
                likbt.ID = "lnkbtndelete";
                likbt.CommandArgument = "row['id']";
                likbt.Attributes.Add("UseSubmitBehavior", "false");
                ph.Controls.Add(likbt);
            }

    Screenshot:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 13, 2016 1:51 PM
  • User-1716253493 posted
    likbt.CommandName = "hapus";
    likbt.CommandArgument = "123";
    likbt.Command += btn_Command;
        protected void btn_Command(object sender, CommandEventArgs e)
        {
            Session["id"] = e.CommandArgument;
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 1:02 AM

All replies

  • User-1838255255 posted

    Hi motorreve00,

    As far as I know , you can instantiate a new control in code behind , then add attribution into it . I make a sample for your reference :

    Sample code:

     protected void Page_Load(object sender, EventArgs e)
            {
                Table tab = new Table();
                tab.ID = "box";
                tab.Style.Add("border", "1px solid red");
                TableRow tr = new TableRow();
                tab.Rows.Add(tr);
                TableCell cell = new TableCell();
                tr.Cells.Add(cell);
                this.form1.Controls.Add(tab);
    
                PlaceHolder ph = new PlaceHolder();
                ph.ID = "placeHolder1";
                tab.Rows[0].Cells[0].Controls.Add(ph);
    
                LinkButton likbt = new LinkButton();
                likbt.Text = "Delete";
                likbt.ID = "lnkbtndelete";
                likbt.CommandArgument = "row['id']";
                likbt.Attributes.Add("UseSubmitBehavior", "false");
                ph.Controls.Add(likbt);
            }

    Screenshot:

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 13, 2016 1:51 PM
  • User-646447061 posted

    thank you Eric Du, i was already the code from you and success ..
    but when i running my program and click the link button i get message like this 

    Multiple controls with the same ID 'lnkbtndelete' were found. FindControl requires that controls have unique IDs.

    Thursday, October 13, 2016 5:16 PM
  • User-646447061 posted

    i try combine the id  >> likbt.ID = "likbt"+row["id_order"];

    but when i want add event handler and set session >> 

    protected void likbt_Click(object sender, EventArgs e)
    {
    Session["id"] = likbt.CommandArgument;
    }

    i get message the name 'likbt' does not exist in the current context

    Thursday, October 13, 2016 5:56 PM
  • User-1716253493 posted
    likbt.CommandName = "hapus";
    likbt.CommandArgument = "123";
    likbt.Command += btn_Command;
        protected void btn_Command(object sender, CommandEventArgs e)
        {
            Session["id"] = e.CommandArgument;
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 14, 2016 1:02 AM
  • User-646447061 posted

    work, thanks pa Laughing

    Friday, October 14, 2016 2:00 AM