Dynamically create ASP.net Submit LinkButton using panel

I am trying to implement an application that will dynamically create a list box with a button next to each item in that list. I can partially do this by using a Panel control in the aspx page and dynamically adding the html to the code behind. I am having problems dynamically adding a LinkButton that will do database work based on what id it is. Is this possible with what I have ?:

ASPX:

<asp:Panel ID="ItemPanel" runat="server">
</asp:Panel>

      

code behind:

...
StringBuilder sb = new StringBuilder();
string UserID;

while (dr.Read())
{
    UserID = Convert.ToInt32(dr["UserID"]);
    sb.Append("<div><b class='template'></b>");
    //Create LinkButton with event and code behind function
}

ItemPanel.Controls.Add(new LiteralControl(sb.ToString()));

      

+1


a source to share


1 answer


You might want to take a look at this very recent question and if you have additional requests please edit your question.

Edit (after OP's comment)




The purpose of posting this link is to give you an idea of ​​how to create a control dynamically. Since you are asking, here is a simple bare bone ASPX page that creates a LinkButton and attaches an event handler for the event Click

. Not sure what you mean by "server changes".

<%@ Page Language="C#" %>

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    LinkButton lnk1 = new LinkButton();
    lnk1.Text = "Click me!";

    //lnk1.PostBackUrl = "SomeOtherPage.aspx";

    // Use the eventhandler to perform redirection, 
    // instead of the PostBackUrl to show it works.
    lnk1.Click += new EventHandler(lnk1_Click);

    // Add control to container:
    pnl1.Controls.Add(lnk1);
  }

  void lnk1_Click(object sender, EventArgs e)
  {
    Response.Redirect("SomeOtherPage.aspx");
  }

</script>

<html>
<head>
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:Panel ID="pnl1" runat="server">
    </asp:Panel>
  </form>
</body>
</html>

      

+1


a source







All Articles