How to access usercontrol's values from page? - c#

Hi I've created user control named test.ascs with one textbox. Now I added this user control in my default.aspx page. How can i access that textbox value from my default.aspx page?
is there any chance?

I usually expose the textbox's text property directly in test.ascx code behind like this:
public string Text
{
get { return txtBox1.Text; }
set { txtBox1.Text = value; }
}
Then you can get and set that textbox from the code behind of default.aspx like:
usrControl.Text = "something";
var text = usrControl.Text;

From your default page try to find the TextBox using your user control.
TextBox myTextBox = userControl.FindControl("YourTextBox") as TextBox;
string text = myTextBox.text;

If this is the purpose of the control, then create a public property on your user control that exposes this value, you can then access that from your page:
string textBoxValue = myUserControl.GetTheValue;

How to access the value of a textbox from an USERCONTROL in a page which is using this usercontrol
step 1: in user control make an event handler
public event EventHandler evt;
protected void Page_Load(object sender, EventArgs e)
{
txtTest.Text = "text123";
evt(this, e);
}
2: in page call the eventhandler
protected void Page_Load(object sender, EventArgs e)
{
userCntrl.evt += new EventHandler(userCntrl_evt);
}
void userCntrl_evt(object sender, EventArgs e)
{
TextBox txt = (TextBox)userCntrl.FindControl("txtTest");
string s = txt.Text;
}

Related

Generic MouseButtonEventHandler for TextBox and Label

Learning WPF with MacDonald's "Pro WPF 4.5 in C#," focusing on Ch5, Events.
How would I write a generic event handler that works with both Labels and TextBoxes to process the MouseDown event initializing a drag & drop procedure?
Here is my TextBox handler:
private void tBSource_MouseDown(object sender, EventArgs e) {
TextBox tBox = (TextBox)sender;
DragDrop.DoDragDrop(tBox, tBox.Text, DragDropEffects.Copy);
}
And my Label handler:
private void lblSource_MouseDown(object sender, EventArgs e) {
Label lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
}
As you can see, I'm using the Content property and the Text property, depending on which object starts the event. If I try to use the same property for both senders, I get build errors (regardless of which I use). If I can avoid duplication, I would be very happy. Should I chunk a conditional out into another function and call that in the handler to determine what property should be used?
you could do something like this:
private void Generic_MouseDown(object sender, EventArgs e)
{
object contentDrop = string.Empty;
//Label inherits from ContentControl so it doesn't require more work to make work for all controls inheriting from ContentControl
if (sender is ContentControl contentControl)
{
//If you don't want to filter other content than string, you can remove this check you make contentDrop an object
if (contentControl.Content is string)
{
contentDrop = contentControl.Content.ToString();
}
else
{
//Content is not a string (there is probably another control inside)
}
}
else if (sender is TextBox textBox)
{
contentDrop = textBox.Text;
}
else
{
throw new NotImplementedException("The only supported controls for this event are ContentControl or TextBox");
}
DragDrop.DoDragDrop((DependencyObject)sender, contentDrop, DragDropEffects.Copy);
}
Let me know if you have any question

How can I show some text on a textbox in a user control by clicking a button in another user control?

please be kind enough to note that I'm new to visual studio.I have a form named form1 in it I have 2 user controls namely uctxt and ucbtn. uctxt has a textbox named txt1 and ucbtn has a button named btn1.
I need to fill txt1 with some text by clicking btn1(I tried public modifiers). I searched everywhere on the internet for a solution for this and I found nothing.
I tried:
public void Btn1_Click(object sender, EventArgs e)
{
uctxt ucText = new uctxt();
ucText.txt1.Text = "welcome";
}
You need at least to expose the OnClick event on the ucbtn:
public event EventHandler UserControlButtonClick;
protected void Btn1_Click(object sender, EventArgs e) =>
if (this.UserControlButtonClick != null)
this.UserControlButtonClick(this, e);
And the label on the uctxt:
public String UserControlLabelText
{
get{return txt1.Text;}
set{txt1.Text = value;}
}
After that you can use them both from the main form like this:
ucbtn1.UserControlButtonClick += new EventHandler(ucbtn1_ButtonClick);
protected void ucbtn1_ButtonClick(object sender, EventArgs e)
{
uctxt1.UserControlLabelText = "your text";
}

using C# solution for Listview label

I want to use this solution to convert URLs to link in a listview label.
private string ConvertUrlsToLinks(string text)
{
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return r.Replace(text, "$1").Replace("href=\"www", "href=\"http://www");
}
I have tried this in the listview databound but its not working.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label ProjectPostLabel = (Label)e.Item.FindControl("ProjectPostLabel");
ProjectPostLabel = ConvertUrlsToLinks({0});
}
Thank you
According to your code you are using ASP.NET Web Forms, not MVC.
You must use your ProjectPostLabel instance with Text property - no need to create a new label that is not assign to anywhere.
From your event you must retrieve Url property, not the label control. I have used NorthwindEmployee class with URL property in my ListView. You must cast it to your own class that is used in the list view.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL);
}
And you must remember that only the last item from your list view will be displayed in the label (unless you expect that behavior). If you want to list of URLs from the list you can write this:
protected void Page_Load(object sender, EventArgs e)
{
ProjectPostLabel.Text = string.Empty;
}
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
}

How to pass value from user control to aspx page on pageload?

The scenario is very simple. I have an aspx page with a user control. I want to set a value and get a response from usercontrol on aspx page's pageload. The SET job is done, but can't GET the response. It's always empty. I tried two methods, but none of them worked.
ASPX PAGE
<uc1:ucContent ID="Content1" runat="server" />
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
// SET job is working without any problem
Content1.ItemName = "X";
//METHOD ONE:
Page.Title = Content1.MetaPageTitle;
//METHOD TWO:
HiddenField hdnPageTitle = (HiddenField)Content1.FindControl("hdnMetaPageTitle");
Page.Title = hdnPageTitle.Value;
}
USER CONTROL
protected void Page_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(itemName))
{
// GET DATA FROM DB
// METHOD ONE:
hdnTitle.Value = DB.DATA;
// METHOD TWO:
metaPageTitle = DB.DATA;
}
}
private string metaPageTitle;
public string MetaPageTitle
{
// METHOD ONE:
get { return metaPageTitle; }
// METHOD TWO:
get { return hdnTitle.value; }
}
EDIT
itemName is a UserControl property to get a value from Parent Page:
private string itemName;
public string ItemName
{
set { itemName = value; }
}
Thanks for your kind help in advance!
I think that the problem is that the page's Page_Load is triggered before the UserControl(Have a look: asp.net: what's the page life cycle order of a control/page compared to a user contorl inside it?).
So you could set the property in Page_init:
In your UserControl:
protected void Page_Init(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(itemName))
{
// GET DATA FROM DB
hdnTitle.Value = DB.DATA;
}
}
Now this should work in your page's Page_Load:
Page.Title = Content1.MetaPageTitle;
Normally i would prefer another kind of communication between a Page and a UserControl. It's called event-driven communication and means that you should trigger a custom event in your UC when the MetaPageTitle changed (so in the appopriate event-handler).
Then the page can handle this specific event and react accordingly.
Mastering Page-UserControl Communication - event driven communication

Problem in event generation of dynamically generated CheckBox

I have a datagrid with dynamically generated checkbox column..I am not able to
generate the checkedChanged event for the checkbox..
Here is my code:
public class ItemTemplate : ITemplate
{
//Instantiates the checkbox
void ITemplate.InstantiateIn(Control container)
{
CheckBox box = new CheckBox();
box.CheckedChanged += new EventHandler(this.OnCheckChanged);
box.AutoPostBack = true;
box.EnableViewState = true;
box.Text = text;
box.ID = id;
container.Controls.Add(box);
}
public event EventHandler CheckedChanged;
private void OnCheckChanged(object sender, EventArgs e)
{
if (CheckedChanged != null)
{
CheckedChanged(sender, e);
}
}
}
and Here is the event
private void OnCheckChanged(object sender, EventArgs e)
{
}
Thanks In advance
Typically we've used the "CommandName" property on the control. This will pass through to the RowCommand event of the GridView. You can then inspect the value of CommandName and act accordingly.
since you add the control dynamically, you also need to add it to the viewState (see overrides for LoadViewState and SaveViewState).
when you do postback, the page has no information about the checkbox you've added and that's why you don't get any event.
please check this article: http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx
it describes those issues very well.

Categories