how change control values from one user control to other user control? - asp.net

i have two user controls uc1 and uc2.
uc1 as button and uc2 has label,
so default.aspx contain both uc1 and uc2,
How can i change value of label by clicking on button???
remember both are in the different user control.

Try to use FindControl method of your page and get the control that has the label, and set the text to this control by an exposed property.
// On your button's click :
UserControl2 uc2 = Page.FindControl("uc2") as UserControl2;
uc2.LabelText = "Some Value";

Related

How do I add a value to a TextBox that's inside an ASP:LoginView

I have some DropDownLists and some TextBoxes on a page. Normally I use this to clear and add value to the items:
Clear control:
txtEventNote.Text = String.Empty
ddlHours.SelectedIndex = 0
Add value:
ddlHours.SelectedValue = dtEvents.Rows(0)("EventDate")
txtEventTitle.Text = dtEvents.Rows(0)("EventHome").ToString()
But if I do this for items thats inside an ASP:LoginView I get an error because the ID's cant be found.
So my question is:
How do I clear the ddlSize DropDownList ID and how do i add String.Empty to txtEventName.Text that's inside the LoginView which has an ID of "loginview2"?
And how do I add a value to a TextBox and DropDownList inside an LoginView called "loginview2" where I have a TextBox with the ID textEventName.Text and the DropDownList have ID ddlSize.
If I delete the LoginView it's easy for me to do, but when I add the LoginView the ID's can't be found.
The LoginView control is a container control. It can hold other ASP.NET WebForm controls. To get a reference to the controls contained within the LoginView control use the FindContol method of the LoginView control.
TextBox tb = lcLoginView.FindControl("txtEventNote") as TextBox
if (tb != null)
{
tb.Text = "My New Value For This TextBox control";
}
Do the same for your DropDownList.
I found In Search Of ASP.Net Controls to be helpful for more about finding ASP.NET controls in other ASP.NET controls.

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

Get Value From Dynamic Control in Page 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);
}
}

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.

Dyamically Change DefaultButton present inside UpdatePanel

How to dynamically Change DefaultButton Property of the Form from UserControl present in a page.Page is loaded into a master page contains Update Panel.
When Try to change the Default button in onload event of the UserControl, It is not changing.
if(lastpage)
{
this.Page.Form.DefaultButton = btnSave.UniqueID;
}
else
{
this.Page.Form.DefaultButton = btnNext.UniqueID;
}
Quote from msdn:
The following scenarios for setting
the default postback button on a page
are not supported during asynchronous
postbacks:
* Changing the DefaultButton programmatically during an asynchronous post back.
http://msdn.microsoft.com/en-us/library/bb386454.aspx
Could you not postback the state of the form and then work out which action to take rather than passing this information based on which button was clicked?

Resources