Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i write some text in textbox.i want to click (add)one button ,then create one link button with textbox value at the same form.and,i write some text in same textbox, again click (add) same button then create another link button .with textbox value.advance thanks ....
Posted

Hi,

Use the below code in on click method of the button.

HTML Code :

XML
<body>
    <form id="form1" runat="server">
    <div>
<asp:textbox id="txtBox" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <asp:Button ID="AddControlButton" runat="server" Text="Add Control"
            onclick="AddControlButton_Click" />
        <br />
        <asp:PlaceHolder ID="DynamicControlsHolder" runat="server"></asp:PlaceHolder>
        <br />
        <br />
    </div>
    </form>
</body>


Code behind file code :

C#
protected voin AddControlButton_Click(object sender, EventArg e)
{
   string str = textbox.Text;
   LinkButton lnkBtn = new LinkButton();
   lnkBtn.Id = "lnkBtn"+str;
   lnkBtn.Text = str;
   //Add some other prpertry of link button.
   
}

Hope this will help you.
 
Share this answer
 
v2
You can place a placeholder in your aspx file and add linkButtons to it Dynamically. But since you are adding linkButtons dynamically and during a button click event they will be gone in the next postback. You have to manually retain/re-add the linkButtons in each postback. I have used Viewstate in this sample.

HTML Code:
<asp:textbox id="txtLinkText" runat="server" ></asp:textbox>
    <asp:button id="btnAddLink" runat="server" text="Add"
        onclick="btnAddLink_Click" /></asp:button>
    <asp:placeholder id="placeHolderLinks" runat="server">
        
    </asp:placeholder>   

Code Behind:
C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (ViewState["LinkButtonTextList"] != null)
           {
               List<string> LinkButtonTextList = (List<string>)ViewState["LinkButtonTextList"];
               foreach (string lnkText in LinkButtonTextList)
               {
                   LinkButton lnkDynamic = new LinkButton();
                   lnkDynamic.Text = lnkText;
                   lnkDynamic.Click += lnkDynamic_Click;
                   placeHolderLinks.Controls.Add(lnkDynamic);

               }
           }
       }

       protected void btnAddLink_Click(object sender, EventArgs e)
       {
           if (txtLinkText.Text != "")
           {
               LinkButton lnkDynamic= new LinkButton();
               lnkDynamic.Text = txtLinkText.Text;
               lnkDynamic.Click += lnkDynamic_Click;
               placeHolderLinks.Controls.Add(lnkDynamic);

               List<string> LinkButtonTextList;
               if (ViewState["LinkButtonTextList"] == null)
                   LinkButtonTextList = new List<string>();
               else
                   LinkButtonTextList = (List<string>)ViewState["LinkButtonTextList"];
               LinkButtonTextList.Add(txtLinkText.Text);
               ViewState["LinkButtonTextList"] = LinkButtonTextList;
           }
       }

       protected void lnkDynamic_Click(object sender, EventArgs e)
       {
              //Do Work();
       }


Hope this helps.

if you do not understand why we have to re-add the controls
this article[^] is a great place to start.
 
Share this answer
 
v6

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