I created an item user_control that has a textbox, button, etc. which will intentionally collect the total quantity of items the user wants.
I dynamically create a few intances of user_control on page_load. If you click the add button for the item quantity it will add to a session variable. however when the user enters a different quantity in the textbox and clicks the add button the total is of the original value of the textbox.
How to get the value the the user has typed in to the textbox to add to total???
You need to assign the same ID to the dynamic control after each postback, when you recreate the control. If you don't assign it the same ID, ViewState cannot populate the value.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
TextBox txt = new TextBox();
txt.ID = "txt1";
PlaceHolder1.Controls.Add(txt);
}
Related
I have an webforms ASPX page, which has a dynamically added user control. The user control has two DropDownLists, one is in an UpdatePanel as its items depend on the selection in the first DropDownList.
The problem is that if you do not change the value of the first DropDownList the value of the second DropDownList does not get saved. If you do change the value of the first DropDownList then it works fine.
This is how it works briefly...
The first time the page loads, the previous values are set. This happens in the main ASPX page Page_Load event where the user control is dynamically added and the initial values are set through a property of the user control. The property sets the selected value of the first DropDownList, triggers the SelectedIndexChanged event which populates items in the second DropDownList with choices based on the first DropDownList selection, and then selects the previous value of the second DropDownList.
Then in the main ASPX page, the user control is dynamically added again on post back in the PreLoad event.
Viewstate is fully enabled throughout.
I have debugged and on post back the second DropDownList has no Items. Even during the UpdatePanel partial post back the items collection is empty. However, if the first DropDownList is changed then the Items collection is correct - it's only if the second DropDownList is last populated on the initial load that the problem happens.
Here's the relevant parts of the code...
Any help greatly appreciated.
ASPX page:
protected void Page_PreLoad(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
PopulateTemplateOptions();
}
}
protected void PopulateTemplateOptions(bool init = false)
{
//this is where the control is dynamically added
//If not post back then initial values are set by passing values to
//user control's TemplateOptions property
}
User control:
public string TemplateOptions
{
set
{
//this is where the initial values are set
}
}
protected void ddlEnquirySubjectDefault_SelectedIndexChanged(object sender, EventArgs e)
{
//this is where items are added to the second drop down list based on the selection in the first one.
}
(These are the only parts that are relevant, but there's another 7,000 lines of code I didn't include.)
Here are two of my ideas:
1) This line might be wrong in your code: if (Page.IsPostBack)
It must be if (!Page.IsPostBack), although I never used Page_PreLoad event so I am not sure if it is right.
2) Try to do what you want in Page_Init event instead. I use it on my projects, without problem.
I
have created 2 drop down list and 2 text boxes dynamically in asp.net .i disable text box at run time .i want that when i select item from drop down text box should be enable how to perform this task please help me :(
On SelectedIndexChanged on the dropDownList call a function that sets the textbox enabled = true. To access controls that have been dynamically added you can use FindControl as per C#, FindControl
I think something like this should help you:
In your page's OnInit event:
DropDownList ddl = new DropDownList();
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
placeholder.Controls.Add(ddl); //assuming this is what you use to dynamically show the dropdown list
TextBox yourTextbox = new TextBox(); //declare the variable outside to be able to be accessed by other methods, but it must be instantiated here. declaration here is for illustration purposes only
yourTextBox.Enabled = false;
placeholder.Controls.Add(yourTextBox);
Inside the instantiated event handler:
void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
yourTextbox.Enabled = true;
}
I have a Repeater control that outputs various forum posts on the page.
Within each repeater row, I have a LinkButton and 4 TextBoxes that contain values.
When I click on one of the LinkButtons, in my event handler code, I want to get the values that are in each of the 4 TextBoxes that correspond with that one particular repeater item/row.
I can repeat through each item within the repeater, but I am only interested in the values that exist in the 4 textboxes that sat alongside the LinkButton that was clicked/that triggered the event. I'm not interested in any of the Textbox values that belong to the other rows/items within the repeater.
What's the best way to do this?
You can use with ItemCommand event and e.Item.FindControl
Link : http://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.repeater.itemcommand.aspx
protected void Repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName == "YourCommand" ) //Adjust your CommandName
{
var textbox1 = (TextBox)e.Item.FindControl("YourIdTextBox1"); //Adjust your Id of TextBox in row
var textbox2 = (TextBox)e.Item.FindControl("YourIdTextBox2");
var textbox3 = (TextBox)e.Item.FindControl("YourIdTextBox3");
var textbox4 = (TextBox)e.Item.FindControl("YourIdTextBox4");
....
}
}
I'm developing an asp.net page that allows users to enter / edit multiple records. I am using a Repeater control to display the entry fields for each record. I would like to have an "Add" button that when clicked, inserts a new, blank set of entry fields. The user should be able to enter the new record, then click the add button multiple times and continue adding new records. Then, when the user clicks the Save button, those new records will be saved along with the changes to the existing records. If the user does not click Save, the records should be discarded and not written to the database.
Is there any way to implement this "add" functionality with the Repeater control?
There is a rather simple solution for this. Assuming you have some form of View Model that temporarily holds the data for the whatever the user is entering, you can simply add an additional "blank" ViewModel to the collection that the repeater uses as it's datasource. On save, you could simply save all the modifications to the database that exist in your View Model. A possible example would be:
protected void btnAdd_OnCommand(object sender, EventArgs e)
{
List<Item> items = (List<Item>)this.ItemRepeater.DataSource;
items.Add(new Item() { Id = -1 });
this.ItemRepeater.DataBind();
}
protected void btnSave_OnCommand(object sender, EventArgs e)
{
List<Item> items = (List<Item>)this.ItemRepeater.DataSource;
foreach (Item itm in items)
{
if (itm.Id == -1)
{
//Save New Item
}
else
{
//Update existing item
}
}
}
The major caveat here is that you must repopulate the ItemRepeater's datasource with the View Models updated with any changes the user made (that you would wish to keep/show).
I have a dynamically created bill of material with each line item being a dynamic user_control. the user_control has a textbox for quantity entry. When I click the submit button i'd like to get all the quantities from each textbox but the controls have all disappeard on the page load and the page shows zero controls.
I know you can turn on autopostback for the textbox then catch each individual text_changed_event but that doesn't seem efficient. I'd like to just loop through all of them when user clicks submit button, then take them back to the same bill of material page.
First of all the reason why controls disappear on postback is that they were added to your page dynamically and when postback occurred the information about the dynamic controls were lost and page had no information about these dynamic controls.
Now about getting the values from controls inside dynamic user controls, you have use the FindControl method or have to iterate through the Controls collection of user control to get the reference to TextBoxes.
An idea about how to do this:
//1. Using ID of user control
//inside button_click method
protected void btnSubmit_Click(...)
{
TextBox txt1 = idOfUserControl.FindControl(textBoxId);
}
//2. Using type of user control
//inside button_click method
protected void btnSubmit_Click(...)
{
foreach(Control c in Page.Controls)
if(c is YourUserControlClass)
{
YourUserControlClass control = (YourUserControlClass)c;
TextBox txt1 = c.FindControl(textBoxId);
}
}