Find Image Control from user control - c#

I am really new to ASP and C#.
I have a User control which has Two Radio Buttons, and and Image control.
I want to load this control dynamically on click of a button and at the same time give ImageURL to the image control.
FileUpload is on the aspx page.
Can anyone help me??
Control MyUserControl = LoadControl("MyControl.ascx"); PlaceHolder1.Controls.Add(MyUserControl);
I was able to load the user control.
Now how to provide the imageURL.
Thanks in advance.

public partial class MyUserControl:UserControl
{
public string ImageUrl
{
set{ image1.ImageUrl=value;}
get{return image1.ImageUrl;}
}
}
var ctrl=LoadControl("MyControl.ascx") as MyUserControl;
if(ctrl!=null)
{
ctrl.ImageUrl = "image.mpg";
}

Expose the image URL in your MyControl.ascx as public
Then casting MyUserControl to your usercontrol and assign the image
example :
Control MyUserControl1 = LoadControl("MyControl.ascx");
MyUserControl temp = (MyUserControl) MyUserControl1;
temp.ImageURL = "urlhere.jpg";

Related

How to get the label text of a dynamically created user control

I have a user control which contains a label called label1 , and I insert the user control into a panel using this code:-
var myControl = new WindowsFormsApp4.library();
panel1.Controls.Add(myControl);
So, the Label that is inside the user control will be registered into the panel.
How do I write a code that gets label1.Text value while the label is not generated into the panel yet?
Edit: I tried to use this code:-
var myControl = new WindowsFormsApp4.library();
string s = myControl.label1.Text
but it doesn't work because it creates a fresh user-control while the user control that is inserted into the panel value is changed in runtime.
Store your control in field instead of a local variable
public partial class Form1 : Form
{
MyControl _myControl; // Use the real type name for your control here.
public Form1()
{
InitializeComponent();
}
...
}
Then create it with
_myControl = new WindowsFormsApp4.library();
panel1.Controls.Add(_myControl);
and access it with
string s = _myControl == null ? "" : _myControl.label1.Text;

C# access Textbox of parent form from Listview control class

I created custom Listview and for usage I need to access Textbox on same Form where Listview is. Currently what I do everywhere in my Listview code is this:
Form frm = FindForm();
var text_ctl = frm.Controls.Find("Textbox1", true).FirstOrDefault() as Control;
TextBox Txt = (TextBox)text_ctl;
...
Txt.Text="Test";
But instead of repeating same code over and over I want to do It only once, like in OnCreateControl() and pass that reference to everywhere I need It in my Listview class. What is the easiest or most elegant solution for this ? Thanks for help in advance !
I complicated too much, here is what I did:
private TextBox _Txt;
public void GetTxt(TextBox ref_txt)
{
_Txt = ref_txt;
}
I run GetTxt() after Form loads, then listview class has reference to It.
alternative using property:
private TextBox _Txt;
public TextBox GetTxt
{
get { return _Txt; }
set { _Txt = value; }
}
Thanks for help, specially Sandeep. And sorry for any inconvenience, I got confused a bit...

Reading User control property inside Nested Master pages in Content page

I have a user control which is in the root master page. The content page connects to this root master page via nested master page
root.master>apps.master>content.aspx
the user control in the root.master has a drop downlist that sets a property when the dropdownlist selection is changed.
I need to access this user control property in the content page.
any help is appreciated
user control property
private string _userCurrentCity = string.Empty;
public string userCurrentCity
{
get { return _userCurrentCity; }
set { _userCurrentCity = value; }
}
protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e)
{
string CurrentCity = "";
CurrentCity = ddl_City.SelectedItem.Text;
lbl_CurrentCity.Text = CurrentCity;
HiddenField_CityID.Value = ddl_City.SelectedValue;
UpdatePanel2.Update();
userCurrentCity = CurrentCity;//this sets the usercontrol property
}
in my content page
UserControl cnt = this.Master.Master.FindControl("Change1") as UserControl;
lbl_Result.Text = cnt.userCurrentCity;
is this correct, i set the userCurrentCity property in the ddl selected change event. Your code looks logical but it is not working.
You need some code like this in code behind:
UserControl cnt = this.Master.FindControl("IDOfTheUserControl") as UserControl
after that:
cnt.Property //to access the wanted property.
EDIT: I don't understand yhat you have nested master pages.
Try this.Master.Master.FindControl and other things like before.
UserControl cnt = this.Master.Master.FindControl("IDOfTheUserControl") as UserControl

how to change master page label's text after a button click on child page

I am using shopping cart icon showing number of products in cart. but when i add item to cart shopping is not updated.so i want to know if there is any method to change master page label's text after a button click on child page.
I would recommend to provide a public property in your MasterPage that you can use to set/get the Label's Text.
in your Master(assuming it's type is called SiteMaster):
public String ShoppingCartNumber{
get{ return LblShoppingCart.Text; }
set{ LblShoppingCart.Text = value; }
}
In your button's click event handler:
SiteMaster master = (SiteMaster)Page.Master;
master.ShoppingCartNumber = "1234";
This approach is straight-forward, less prone to errors and easily readable. You could even change the control in your master without needing to change the pages (f.e. if you want to replace the Label with a TextBox).
Try this
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
Label1.Text = "Master page label = " + mpLabel.Text;
}
Try this:
Add in your masterpage.cs file:
public Label lbl
{
get { return YourLabelId; }
set { YourLabelId= value; }
}
Add this in your content page:
<%# MasterType VirtualPath="~/YourMasterPageName.Master" %>
Then access in content page in your button click event:
string name = Master.lbl.text;

Change panel "visible" property on MasterPage from Child pages

I want to show some panel with a label, both located on a MasterPage, from inside it's child pages.. I already did the coding on the MasterPage:
public class MyMaster : MasterPage
{
public void ShowPanel(string pMessage)
{
labelInside.Text = pMessage;
myPanel.visible = true;
}
}
Then I make the calls from child pages:
public void ShowPanel(string pMessage)
{
MyMaster masterPage = this.Master as MyMaster;
masterPage.ShowPanel(pMessage);
}
This "works" ok, but it won't show nothing, since I need the page to be "refreshed" in an "ajax-way" like an UpdatePanel, which I can't use because the Trigger is in another page, right?
I really need this to work.. even if you have another completely different way to do this, I would appreciate.
You must place your panel inside an UpdatePanel(UpdateMode conditional) and in ShowPanel call its Update method.
Have you considered having the masterpage just have a placeholder for the label, but having each child page put its own content label inside that placeholder, which it would then have full control over?
you can subClass your page, and expose a property say.. MyPage.FooVisible
than in your masterPage, you can:
myPage = this.Page as MyPage
if (myPage != null) myPage.FooVisble = false;
in your page you can handle that any way you like,
FooVisible {
set { SomeElement.Visible = value; }
}
pseudo code of course :)

Categories

Resources