I want to pass one of the textbox's(on the master page) value into the user control(.ascx) page. Here is my code shows how to open user control..
Control usrCnt= LoadControl("userControl.ascx");
usrCnt.ID = "usrCnt";
ASPxPanel1.Visible = true;
ASPxPanel1.Controls.Clear();
ASPxPanel1.Controls.Add(userCnt);
How can post the textbox's value to the user control? I can't do like this..
Control usrCnt= LoadControl("userControl.ascx?param=" + textbox.Text);
Create a method for your usercontrol like SetText
and then
usrCnt.SetText("textValue");
if it is your webusercontrol code behind
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void SetText(string theText)
{
this.Label1.Text = theText;
}
}
and if you've been added the control to the page
in page call it as
this.WebUserControl11.SetText(TextBox1.Text);
put these on the upper part of your usercontrol
private string _TextBoxValue = string.Empty;
public string TextBoxValue {
get { return _TextBoxValue; }
set { _TextBoxValue = value; }
}
then on your masterpage
usrCnt.TextBoxValue = TextBox1.Text;
For the quickest and dirty way is on your MasterPage
ViewState["TextBoxValue"] = TextBox1.Text();
and on UserControl, access ViewState["TextBoxValue"] to get the value.
Related
I have a User control with a textbox. I want to pass the text from textbox on the User control back to the parent form, when the text changes.
I found the following code in my user control which work well for doing this. This includes a TextChanged event.
public string pub_callsign
{
get { return textBox1.Text; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBoxContent = this.textBox1.Text;
var parent = this.Parent as form1;
parent.pub_callsign = pub_callsign;
}
In my parent form I have
public string pub_callsign
{
set { txtbx_callsign.Text = value; }
}
However I would like to use this User Control on more than 3 form,
So how do I get the form name from the form to the user control. Thereby what is shown below as form1 or form2 to be a variable containing the formname ??
var parent = this.Parent as form1;
var parent = this.Parent as form2;
var parent = this.Parent as formname_value;
hope this makes sense.
Dave
Add a property in the user control that gives you access to the text box contents.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string Contents { get => textBox1.Text; set => textBox1.Text = value; }
}
In the form use the property to read the contents of the text box
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ReadContents()
{
var contents = userControl11.Contents;
}
}
You need to be able to have the user control tell the parent - no matter if the parent is a form or other user control - that the value of the text box has changed.
I created a user control called MyUserControl with a single text box called textBox1.
I added a TextBoxTextChanged event to MyUserControl and ended up with the following code:
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
public event EventHandler<string> TextBoxTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.TextBoxTextChanged?.Invoke(this, textBox1.Text);
}
}
Now, on Form1, I added the an instance of MyUserControl called myUserControl1 and added the handler for TextBoxTextChanged. I ended up with this code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myUserControl1_TextBoxTextChanged(object sender, string e)
{
this.label1.Text = e;
}
}
When I run that the text in the user control is automatically updated in the label as I type.
Where do you want to put the text box text? If it's in the title bar, do this:
formName.Text = yourTextBox.Text;
If it's in a label or another text box, do the same thing but use the control with its text property, and set it to the text box's text property.
In visual studio how do you access a control on a form hosting a user control? For example, when text changes in a text-box in a user control, I want text in another text-box in another user control to change. Both these user controls are hosted on the same form. Thanks in advance!
If you need different UI for data entry, I prefer to have 2 controls with different UI, but I will use a single data source for them and handle the scenario using data-binding.
If you bind both controls to a single data source, while you can have different UI, you have a single data and both controls data are sync.
The answer to your question:
You can define a property in each control which set Text of TextBox. Then you can handle TextChanged event of the TextBox and then find the other control and set the text property:
Control1
public partial class MyControl1 : UserControl
{
public MyControl1() { InitializeComponent(); }
public string TextBox1Text
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (Parent != null)
{
var control1 = Parent.Controls.OfType<MyControl2>().FirstOrDefault();
if (control1 != null && control1.TextBox1Text != this.textBox1.Text)
control1.TextBox1Text = this.textBox1.Text;
}
}
}
Control2
public partial class MyControl2 : UserControl
{
public MyControl2() { InitializeComponent(); }
public string TextBox1Text
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (Parent != null)
{
var control1 = Parent.Controls.OfType<MyControl1>().FirstOrDefault();
if (control1 != null)
control1.TextBox1Text = this.textBox1.Text;
}
}
}
Ok, so I am new to custom controls, and need be able to get the contents of a text box that is part of a custom user control.
So say I want to access the text that the user has entered in the Name Textbox, how would I do this? What would I have to add to the code for the control? Or is there a way to just reference it from the form that the control is on?
Here is the code for the control:
public partial class CharacterControl : UserControl
{
public CharacterControl()
{
InitializeComponent();
}
private void get_start_Click(object sender, EventArgs e)
{
starttime.Text = DateTime.Now.ToString("HH:mm");
}
private void get_end_Click(object sender, EventArgs e)
{
endtime.Text = DateTime.Now.ToString("HH:mm");
}
}
Turns out all I had to do is set the access modifier to public.
I extended the button control to have also LabelName. When I press the button I need to write the name of the button in the label.
My first idea was using events - easy and simple.
The question is: Is there more elegant way to do it? (I've been asked to bind the button and the label)...
I think that the best way to do it would be to use an action listener and the best way to use the action listener would be to build it into your class that extends the button control so that the user doesn't have to do this on their own. It would look like this.
class Button2 : Button
{
public string LabelName = "";
public Button2()
{
this.Click += this.SetLabelName;
}
private void SetLabelName(object sender, EventArgs e)
{
this.LabelName = "Something?";
}
//You could also do this instead.
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
}
}
If you're talking about changing the Text property of an external Label control, then simply create a property in your Button to hold a reference to a Label. You can set this via the IDE like any other property:
Here's the Button class:
public class MyButton : Button
{
private Label _Label = null;
public Label Label
{
get { return _Label; }
set { _Label = value; }
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if (this.Label != null)
{
this.Label.Text = this.Name;
}
}
}
Here's the Label after I clicked the Button:
I want to change control value exists in Master page from user control and the scenario is like this:
On user control load method I use a query string parameter called "catID" to get category entity, then I want to set the category title as value of title tag (Category title) ,which locate in Master page .
I tried to change the title in user control page load control but the Master page load method executes earlier.
Any ideas ?
Provide a public method in your MasterPage that sets the title, for example:
public void setTitle(string title)
{
this.LblTitle.Text = title;
}
Then you can call it from your UserControl(YourMasterPage is the actual type of the MasterPage):
((YourMasterPage)this.Page.Master).setTitle("new Title");
If it's a query string parameter you should be able to read "catId" i.e from Masterpage code behind as well and set the title.
EDIT:
Try a property then:
In usercontrol
protected void Page_Load(object sender, EventArgs e)
{
this.MyTitle = "SomeTitle";
}
public string MyTitle { get; set; }
On masterpage:
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl.PreRender += new EventHandler(WebUserControl_PreRender);
}
void WebUserControl_PreRender(object sender, EventArgs e)
{
string str = WebUserControl.MyTitle;
this.Header.Title = str;
}
((Label)Master.FindControl("loadlbl")).Text = "your text";