I have an aspx page made with ASP.NET Web Forms. What i need to create is two asp:textbox fields. I want to be able to dynamically add two new fields below that with the press of a button.
So basically i want to be able to add an infinite amount of "new" textfields. But i'm not sure how to do this in ASP.NET.
Is there perhaps a way to create an arrat of those textfields? So that when a form is posted i can easily iterate over them?
How can i do this?
In your aspx file:
<asp:TextBox runat="server" ID="textbox1"/>
<asp:TextBox runat="server" ID="textbox2"/>
<asp:Button runat="server" ID="btnAdd" OnClick="btnAdd_Click" />
<asp:PlaceHolder runat="server" ID="ph" />
In your aspx.cs file:
protected void btnAdd_Click(object sender, EventArgs e)
{
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
ph.Controls.Add(tb1);
ph.Controls.Add(tb2);
}
You can add this fields and make them hidden(invisible).
And you can show fields on button click (with javascript).
Dynamically adding serverside controls is problem.
Related
I want to add some asp.net linkbutton controls to a literal on a c# web form application like code below:
StringBuilder sb = new StringBuilder("<ul>");
sb.Append("<li>");
sb.AppendFormat("<asp:LinkButton runat='server' class='add-row' ID='BtnAdd' OnClick='BtnAdd_Click' CommandArgument='test'>{0}</asp:LinkButton>", "Text on the link");
sb.Append("</li></ul>");
this.Literal.Text = sb.ToString();`
and also i want to fire event of click and get CommandArgument's value of that.
private void BtnAdd_Click(object sender, EventArgs e)
{
//get the value of CommandArgument
}
I tried these and the linkbutton added to the literal successfully. but the linkbutton didn't convert to <a> tag and i found that with the firebug like this and no any events couldn't fire:
I want make a tree with leafs as linkbuttons using <ul><li> tags overall.
It would be very helpful if someone could explain solution for this problem.
The correct approach here is via the WebControl api and not from the template.
Your approach may work for HTML content only.
What you need to do is handle a page lifecycle event like OnInit, get a reference to your container control (this.MyPanel), and dynamically add items to that control's children property (this.MyPanel.Children.Add(new....))
The suggested Repeater solution is not suited for tree structure data sources, works for a list though...
As suggested by Andrei using linkbuttons in this way will not work.
I have used a similar solution in the past by creating a query parameter for each a tag in the literal and redirecting to either the current page or a different page and extracting the query parameter from there.
I would however advise you to first try and see if there is not a different way to accomplish what you are trying to do as the literal solution ends up very cluttered and code heavy.
I think a Repeater would better suit your purpose.
<ul>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<li><asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnClick="BtnAdd_Click">Text on the link</asp:LinkButton></li>
</ItemTemplate>
</asp:Repeater>
</ul>
And if you want to use CommandArgument you need an OnCommand, not a OnClick
<asp:LinkButton ID="BtnAdd" runat="server" CssClass="add-row" OnCommand="BtnAdd_Command" CommandArgument='<%# Eval("value") %>'>
protected void BtnAdd_Command(object sender, CommandEventArgs e)
{
string commandArgument = e.CommandArgument.ToString();
}
If you have single <asp:LinkButton ..> then you can set it's property at run time. Like
BtnAdd.Id="";
or if you multiple then you can use AddControl method below is link
How to add controls dynamically when click button in asp.net?
How to add controls dynamically to ASP.NET form?
If you have to use asp: LinkButton just for one time then do it in this way.
<asp:Literal runat="server" ID="DocLink"></asp:Literal>
<asp:LinkButton runat="server" ID="LinkButton1" OnCommand='LinkButton1_Click'></asp:LinkButton>
and in .cs file
LinkButton1.Text = CurrentListItemAbsoluteUrl;
LinkButton1.CommandArgument = CurrentListItemAbsoluteUrl;
public void LinkButton1_Click(Object sender, CommandEventArgs e)
{
e. CommandEventArgs //To get your parameter
I have entered a value in text box and click the button the value should bind in a label.
The code must be in c# not in query. There is no database also.how please say the code behind in c#. It is button click event. while click the button the page should post back and bind the data how to bind a textbox value in a label box using c#. The value must bind in postback method.
easy , take a look at the below code:
<asp:TextBox Id="txt1" runat="server" />
<asp:button Id="btn1" runat="server" onClick="Button1_Click"/>
<asp:Label Id="lbl1" runat="server"/>
//in the code behind implement the Button1_Click
protected void Button1_Click (object sender, EventArgs e)
{
lbl1.Text= txt1.Text;
}
It as easy as the following:
LabelID.Text = TextBoxID.Text;
Where LabelID is the ID of your Label control and TextBoxID is the ID of your TextBox control.
You should place this code inside the button click's event.
Update
Please use for your purpose ASP.NET Web Server Controls.
<asp:TextBox ID="textBoxId" runat="server"/>
<asp:Label ID="labelId" runat="server"/>
Then you can access the values of Text property of both the above controls, like above:
labelId.Text = textBoxId.Text;
I'm using two drop down and bind values to that drop down.
Now am adding a new button add_new.
I want to create the above drop downs below when I click the add button and maintain the previous selected values. Please help me to do this.
You can achieve the desired result using Repeater control of ASP.Net. You can create any type of template as you wish, see the code below:
ASPX:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" />
<asp:DropDownList ID="DropDownList2" runat="server" />
<br />
</ItemTemplate>
</asp:Repeater>
CodeBehind:
protected void Button1_Click(object sender, EventArgs e)
{
// data fetching logic
Repeater1.DataSource = data;
Repeater1.DataBind();
}
Hard to answer without a better explanation and code samples but from experience doing anything with dropdowns over postbacks is better controlled with hidden fields. Set the value of the hidden field with javascript. Please provide more detail.
I am writing a card game app using Ajax, c# and .NET 3.5. Due to the nature of the interface I have numerous update panels that Im trying to manage and update across various user action. I'm having problems with one though.
The players current hand is built by binding a list of Card objects to a repeater and then dynamically creating a Card UserControl and adding it to the Controls of a PlaceHolder when each item is databound. The code is roughly as follows:
On the page
<asp:UpdatePanel ID="pnlInHand" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rptInHand" runat="server" onitemdatabound="rptInHand_ItemDataBound">
<ItemTemplate>
<asp:PlaceHolder ID="plcInHandCard" runat="server" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
In code behind
protected void rptInHand_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Card card = (Card)e.Item.DataItem;
PlaceHolder plcCard = (PlaceHolder)e.Item.FindControl("plcInHandCard");
plcCard.Controls.Add(CreateCardControl());
}
private CardControl CreateCardControl()
{
CardControl cardControl = (CardControl)Page.LoadControl("~/UserControls/CardControl.ascx");
//Set control properties here
return cardControl;
}
The Card Control includes a Button. The ClickEvent for this button calls a Method of the Parent Page that needs to update a seperate UpdatePanel as well as remove the card Control from the Panel that it is sitting within.
I have two issues.
When I click the Card Control Button, because it has been created as part of a repeater within an updatePanel, it no longer exists when the page is posted back and so the Click event for the button within the control never fires. I can obviously rebind the repeater on page load, but does this mean I have to essentially do this on every postback?
More importantly I need a way to trigger the update of another updatepanel in the parent page when the Card control's click event is raised. Is there a way of setting a trigger on an update panel that listens out for an event within a dynamicaly loaded UserControl?
Many thanks
Stewart
Sample code from ASP.net site that should address your point 2 problem follows.
I'll leave the translation to your code to you.
I may be misunderstanding what you are trying to do but I believe once you get this working your issue with point 2 is no longer relevant as you'll get the AJAX postback you want from your parent update panel.
Good luck!
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="itemDataBound">
<ItemTemplate>
<mycontrol:user ID="user1" runat="server" OnCausePostBack="user1_CausePostBack" /> <br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
protected void itemDataBound(object sender, RepeaterItemEventArgs e)
{
ModalPopup_WebUserControl mw=(ModalPopup_WebUserControl)e.Item.FindControl("user1");
AsyncPostBackTrigger at = new AsyncPostBackTrigger();
at.ControlID = mw.ID;
at.EventName = "CausePostBack";
UpdatePanel2.Triggers.Add(at);
}
protected void user1_CausePostBack(object sender, EventArgs e)
{
// do something
}
just an idea for point 2 : what about add a property in the cardControl to set a reference to the updatepanel/s ? from there you can add triggers or call panel.update in the button event
for point one yes u will have to do it. u will have to re create the controls
for point 2 a simple updatePanel.update() would do the job insode of any event if you are using an event that was binded to a dynamically created control then u will have to rebind the event on every page postback
I'm brand new at this, using vs2010 with asp.net and c#, and I'm trying to use a button click event to display forms on my "Add Product" page (the forms will be textbox/label based for inputting data), using items from a dropdownlist of products. Which methods are available/is there a 'best practice' for this sort of thing? I've been fooling around with an if (Dropdownlist.SelectedIndexChanged) statement, but I'm not quite clear on why the syntax requires the SelectedIndexChanged method to preclude a += or -=. Thoughts?
The SelectedIndexChanged method has the += because you are adding an event handler to a specific elements event. Is the button you click on the Add Product page? If so for the OnClick event you could just set your form details based on the DropDownList SelectedItem or SelectedIndex. You may also need to wrap that panel in an UpdatePanel so that it can update visibility without reloading the whole page.
<asp:UpdatePanel ID="updateFormPanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblName" runat="server" Text="" />
<asp:TextBox ID="txtDetails" runat="server" Text="" />
//More TextBox's or whatever you want
</ContentTemplate>
</asp:UpdatePanel?
<asp:DropDownList ID="ddlProductCategory" runat="server" />
<asp:Button ID="btnAddProduct" runat="server" OnClick="AddProduct_Click" Text="Add Product" />
//Code File Behind
protected void AddProduct_Click(Object sender, EventArgs e)
{
lblName.Text = ddlProductCategory.SelectedItem.Text;
txtDetails.Text = ddlProductCategory.SelectedItem.Value;
}