Get Value From Dynamic Control in Page Asp.Net - asp.net

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);
}
}

Related

set focus on first textbox of listview in insert mode using ajax

Here's a nice one:
Page using an UpdatePanel. Inside is a ListView. It has 1 TextBox. When the user clicks a button i set the ListView's InsertItemPosition to top.
Q: How do i set focus on the TextBox?
If you have a ScriptManager control on the page you could use it to SetFocus to the TextBox control. You would need to do this in the code behind within your button click event.
EDIT: This should allow you to get to the TextBox control in the InsertItemTemplate. NOTE: You will need to change the control name in the FindControl method to the name of the TextBox in your InsertItemTemplate.
Example:
TextBox tb = ListView1.InsertItem.FindControl("tbName") as TextBox;
if (tb != null)
{
ScriptManager1.SetFocus(tb);
}

dialogue between two user control based on user events

In an ASP.NET page I have added two user control with a dropdownlist each one. The selection of a dropdownlist should be changed the query to the second user control.
What is the most efficient way to pass the selected value of dropdownlist to the second?
I initially thought of creating a public event (public string OnClientSelectedIndexChanged) while the first user control and outsource through a public string SelectedValue the selected value of the second user control:
public string SelectedValue
{
get
{
rcg.SelectedValue return;
}
set
{
rcg.SelectedValue = value;
}
}
rcg is the dropdownlist.
Could be corrected or have alternatives?
Thanks in advance
Alternative is maybe to use AJAX.
Surround the two dropdownlist with an ASP update panel and trigger this one with the first dropdownlist 'OnSelectedIndexChanged'. In the event's code you can bind your second dropdownlist..
Like this it is not necessary to reload the entire page for each first dropdownlist change..
Yes, you are on right track.
1: You expose an event from UC1(e.g. uc1ddlchanged) and a property from UC2 (e.g. uc1ddlSelectedValue).
2: In the OnSelectedIndexChanged of ddl1 in UC1 your raise the event uc1ddlchanged.
3: On your page that has UC1 and UC2 you handle this event and set the UC2.uc1ddlSelectedValue = UC1.SelectedValue
4: In the UC2 -> uc1ddlSelectedValue -> set{} you can set the value and rebind your ddl and or do any other thing needed to update the UC2.

Trigger function inside a usercontrol from a page

I have created a usercontrol to capture education details, it contains 5 textboxes and an functionto insert that values into my db. I have added the usercontrol 5 times to a page. I have a button on my aspx page which I want to be able to click and call the function to insert the values.
Can anyone suggest how I can do this?
The function which inserts values into your db should not be in your usercontrols but in your page. Does your usercontrol has a save button itself or only the page? On first variant you should raise an event and the page could catch it to save the values. On second variant you should iterate through your usercontrols(added dynamically or fixed?). You should add public properties which return the textbox's values to the page, then you can save these values into the db.
Put a public method on your user control.
// Inside MyUserControl
public void SaveIt() {
// logic to perist the values to db.
}
From your ASPX page (or code behind) when the button is clicked, loop over your user control instances and call the method:
// On Click...
MyUserControl[] arrControls = new MyUserControl[] {myUC1, myUC2, myUC3, myUC4, myUC5};
foreach (MyUserControl c in arrControls)
c.SaveIt();

How to simulate postback in nested usercontrols?

I'm doing an asp.net application with one page. In this page, I have one usercontrol defined. This usercontrol has a menu (three buttons) and 3 usercontrols defined too. Depending on the clicked button one of the three usercontrols turn to visible true or false.
In these three usercontrols I have a button and a message, and I want to show the message "It's NOT postback" when the button of the menu is clicked, and when the button of the usercontrol is clicked the message will be "YES, it's postback!!!".
The question is that using property "IsPostBack" of the usercontrol or the page the message will never be "It's NOT postback" because of the clicked button of the menu to show the nested usercontrol.
This is the structure of the page:
page
parent usercontrol
menu
nested usercontrol 1
message
button
nested usercontrol 2
nested usercontrol 3
I know it can be done using ViewState but, there is a way to simulate IsPostBack property or know when is the true usercontrol postback?
Thanks.
I have the solution for this problem. When an element fires an event is sent to the server in the Request.Form collection, so I created a property in nested usercontrols that checks if there is a child control (defined in the usercontrol itself) in Request.Form collection:
public bool IsUserControlPostBack
{
get
{
foreach (Control c in Controls)
foreach(string key in Page.Request.Form.AllKeys)
if( c.ClientID == key.Replace('$','_'))
return true;
return false;
}
}
This code can be a property of the usercontrol or, if it's called a lot of times, a variable that it's set on the OnInit event of the usercontrol.

ASP.NET WebControl & Page - Adding controls (like TextBox) dynamically

I'm trying to create a custom server control (WebControl) with a text box.
I add asp.net textbox to the custom control in CreateChildControls override. In OnInit override I add event handler to TextBox.TextChanged.
Everything works, except that TextChanged never fires. I looked at viewstate and it looks like my textbox never saves its Text property in the viewstate. I've tried to set Text in various places, including constructor, but nothing works.
How can I get TextBox dynamically added to WebControl to save it's Text in viewstate and get TextChanged event to fire?
I would greatly appreciate an example of WebControl code behind with TextBox being added dynamically and TextChanged event being fired.
The dynamically created control must be created again in each post back, (the pageInit event is the better option) for the event to be fired.
BTW, if you want the TextChanged event to generate a postback you must also set the AutoPostback of the control to true.
fixed it. dynamic control must be created and added in Init event. It must be assigned an ID without special ASP.NET symbols ('$' or ':' inside custom ID will break things). All properties must be assigned after control is added to the controls tree.
here's a working example for Page codebehind:
private readonly TextBox _textBoxTest = new TextBox();
protected void Page_Init( object sender, EventArgs e )
{
this.form1.Controls.Add( _textBoxTest );
_textBoxTest.Text = "TestBoxTest";
_textBoxTest.ID = "TestBoxTestId";
_textBoxTest.TextChanged += this._textBoxTest_TextChanged;
}
void _textBoxTest_TextChanged( object sender, EventArgs e )
{
_textBoxTest.Text = "Worked";
}
for WebControl place init code in OnInit override
This will help you out. In short, you need to handle the viewstate for your Dynamically added control on your own.

Resources