Access control on other page in asp.net - asp.net

How to access control in other aspx page from other aspx page in asp.net

you can use findcontrol
Example to get the textbox text:
string controlValue =((TextBox)( Page.FindControl("uc").FindControl("TextBox1"))).Text;
Note: us is id of usercontrol registered in the aspx page. TextBox1 is the textbox in the usercontrol

Button btn = ((Button)PreviousPage.FindControl("Button1"));
Button1.Text = btn.Text;
Use : PreviousPage
Note : PreviousPage property Gets the page that transferred control to the current page.

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

how change control values from one user control to other user control?

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";

asp.net usercontrol SetFocusOnError

I have a aspx page that several of the same usercontrols on the page. The usercontrol houses a textbox that has a Required field validator on it. The validator works but the setonfocus="true" does not seem to be working, further more, the button the aspx page when the validator shows the error message, the button still fires the code behind.
Here is what the aspx page looks like as far as the user control and the the button.
ucTB:ucTextBox ID="ucTextR" runat="server" ValidationGroup="txtRequired" Required="_true"
asp:Button ID="btnSave" runat="server" Text="Click" ValidationGroup="txtRequired"
and the usercontrol validator
asp:RequiredFieldValidator ID="rfTextBox" runat="server" ControlToValidate="txtTextBox"
SetFocusOnError="true" ErrorMessage="Required Field" EnableClientScript="false"
the user control has been wired to grab the validator from the aspx page and use it in the usercontrol... something like this
Public Property ValidationGroup() As String
Get
Return CType(ViewState("ValidationGroup"), String)
End Get
Set(ByVal Value As String)
ViewState("ValidationGroup") = Value
End Set
End Property
Protected Sub AssignValidation()
For Each control As Control In Me.Controls
Dim [property] As PropertyInfo = control.[GetType]().GetProperty("ValidationGroup")
If [property] Is Nothing Then
Continue For
End If
[property].SetValue(control, ValidationGroup, Nothing)
Next
End Sub
and i load the AssignValidation on page_load
anyway.. hope this is the info you need to point me in the right direction.
What i'm looking to do is if the required field validator to put the focus on the usercontrol if there is nothing in the usercontrol text box and also for the button on the aspx page not to fire.. like i think it behaves if you use a validator on a aspx page with no usercontrol
thanks
shannon
You can't set the user control to visible because it's not a visible container. You can set the focus yourself programmatically. See this:
http://forums.digitalpoint.com/showthread.php?t=282224
Or, you can programmably set the validator to the ID of the textbox within the user control; expose a textboxID property in the user control code-behind which returns the textbox's ID, and have your page assign the validator's controltovalidateID to this.

Resources