Accessing controls added programmatically on postback - c#

On postback: How can I access ASP.NET controls in my code-behind file, which are added programmatically?
I am adding a CheckBox control to a Placeholder control:
PlaceHolder.Controls.Add(new CheckBox { ID = "findme" });
Controls added in the ASPX file are showing up fine in Request.Form.AllKeys except the ones I add programatically. What am I doing wrong?
Enabling the use of the ViewState on the controls does not help. If only it was that simple :)

You should recreate your dynamic control on postback:
protected override void OnInit(EventArgs e)
{
string dynamicControlId = "MyControl";
TextBox textBox = new TextBox {ID = dynamicControlId};
placeHolder.Controls.Add(textBox);
}

CheckBox findme = PlaceHolder.FindControl("findme");
Is that what you mean?

You will need to add the dynamically add the control during Page_Load to build the page up correctly each time. And then in your (i am assuming button click) you can use an extension method (if you are using 3.5) to find the dynamic control you added in the Page_Load
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder.Controls.Add(new CheckBox {ID = "findme"});
}
protected void Submit_OnClick(object sender, EventArgs e)
{
var checkBox = PlaceHolder.FindControlRecursive("findme") as CheckBox;
}
Extension Method found here
public static class ControlExtensions
{
/// <summary>
/// recursively finds a child control of the specified parent.
/// </summary>
/// <param name="control"></param>
/// <param name="id"></param>
/// <returns></returns>
public static Control FindControlRecursive(this Control control, string id)
{
if (control == null) return null;
//try to find the control at the current level
Control ctrl = control.FindControl(id);
if (ctrl == null)
{
//search the children
foreach (Control child in control.Controls)
{
ctrl = FindControlRecursive(child, id);
if (ctrl != null) break;
}
}
return ctrl;
}
}

Related

I'm getting an exception of InvalidCastException

I want to get asp button ID from previous page and I'm getting an exception.
Here is my code for C#
public partial class ADD_MOBILE : System.Web.UI.Page
{
string BUTN_ID;
protected void Page_Load(object sender, EventArgs e)
{
Button button = (Button)sender;
string BUTTON_CLICKER_ID = button.ID;
BUTN_ID = BUTTON_CLICKER_ID;
}
protected void saveMOBILE_Click(object sender, EventArgs e)
{
if(BUTN_ID == "samsung"){ ... }
}
}
I'm getting exception at this point Button button = (Button)sender; why?
Okay, after going through your code it seems you want to get the button id so you can process some code based on that. Well, Let me make something clear, Page Load event will never give you the control that caused postback in sender object even if it gets triggered when you click a button and it posts back but it will NOT have the information in sender object for the control that posted it back.
For that you might want to use this approach from this James Johnson's answer to know which control caused postback:
/// <summary>
/// Retrieves the control that caused the postback.
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
private Control GetControlThatCausedPostBack(Page page)
{
//initialize a control and set it to null
Control ctrl = null;
//get the event target name and find the control
string ctrlName = page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
ctrl = page.FindControl(ctrlName);
//return the control to the calling method
return ctrl;
}
This will return the Control object that you can further dig more into.
Otherwise, the suitable and neat approach in your case would be to do it like this:
public partial class ADD_MOBILE : System.Web.UI.Page
{
string BUTN_ID; // I do not think it is necessary here.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void saveMOBILE_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if(button is null) return; // you can use == instead of keyword 'is'
if(button.ID.Equals("samsung"))
{
// DoStuff();
}
}
}
I hope you find it useful.

How to access controls on hosted form in a user control WinForm

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

how do I invoke a post-back from code behind?

I am having an issue with my usercontrol being loaded too late in the "post back timeline", because they are being loaded in as a result of a custom event.
As a result the button click events on this usercontrol don't fire on the first click (the entire post-back occurs only the event-handlers for the click don't get raised). On the second click (and hence second post-back), however, the event-handlers work fine.
How can I invoke a second post-back automatically right after one has just finished? So my usercontrol gets loaded correctly.
Default Page
public interface IEventProvider
{
void TriggerEvent(String path);
}
public partial class Default : System.Web.UI.Page, IEventProvider
{
private string LastLoadedControl
{
get
{
return Session[Paths.CURRENTCTRL] as string;
}
set
{
Session[Paths.CURRENTCTRL] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;
ContentPlaceholder.Controls.Clear();
if (string.IsNullOrEmpty(controlPath))
controlPath = Utils.Paths.USERCTRL_BASE + "Main.ascx";
Control uc = Page.LoadControl(controlPath);
ContentPlaceholder.Controls.Add(uc);
}
protected void Page_Load(object sender, EventArgs e)
{
LoadUserControl();
}
public void TriggerEvent(String path)
{
if (path.Equals("logout"))
{
Session.Clear();
Session.Abandon();
LastLoadedControl = null;
}
else LastLoadedControl = Paths.USERCTRL_BASE + path + ".ascx";
LoadUserControl();
}
}
Usercontrol code
protected void profileBtn_Click(object sender, EventArgs e)
{
Utils.Events.triggerRedirectPage(this.Page, "Login");
}
events code
public static void triggerRedirectPage(Page p, String path)
{
IEventProvider eventProvider = p as IEventProvider;
if (eventProvider != null)
eventProvider.TriggerEvent(path);
}
You can add a button (or another control) with AllowPostBack=true then trigger click event on this button.

How to access selected index change on dropdown?

I am new to C# and I created a user control similar to the person from this thread:
add user control to a form
only, I used 4 dropdowns. I created a custom user control whose class is called CustomBaseUserControl.cs. It has all the selected index changed events for each dropdown. From the the form, call it TheFormControl, that the CustomBaseUserControl was dropped into, how do I access those event change values?
Thanks in advance!
If you need to retrieve selected index on TheFormControl, you could either
Use variables to store the value on CustomBaseUserControl, in this case you'll have to listen the SelectedIndexChanged events and updates your values.
Trigger a custom selected index changed from CustomBaseUserControl to TheFormControl
--
class CustomBaseUserControl: UserControl{
int idx1=-1;
public CustomBaseUserControl()
{
Initialize();
//Fill ComboBox
//Suscribe Event
combobox1.SelectedIndexChanged += combobox1_SelectedIndexChanged;
}
void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = combobox1.SelectedIndex;
if (index != idx1)
{
idx1=index;
RaiseIndexChanged(e);
}
}
public virtual void RaiseIndexChanged(EventArgs ea)
{
var handler = OnIndexChanged;
if (OnIndexChanged != null)
OnIndexChanged(this, ea);
}
public event EventHandler OnIndexChanged;
}
Caller class would be
class TheFormControl: Form
{
CustomBaseUserControl cb;
public TheFormControl()
{
Initialize();
cb = new CustomBaseUserControl();
cb.OnIndexChanged +=cb_OnIndexChanged;
}
void cb_OnIndexChanged(object sender, EventArgs e)
{
// Here you know index has changed on CustomBaseUserControl
}
}

How to change button background image on mouseOver?

I have img1, and img2 in my resources. I have easily set btn.backgroundImage as img1 in btn properties. Images paths are: c:\Project\Resources...
Now I don't know how to set btn.backgroundImage to be img2, I want to do it on event "MouseEnter". So I would apreciate complete code, because I am pretty green about this...
I apreciate any given idea...
In the case of winforms:
If you include the images to your resources you can do it like this, very simple and straight forward:
public Form1()
{
InitializeComponent();
button1.MouseEnter += new EventHandler(button1_MouseEnter);
button1.MouseLeave += new EventHandler(button1_MouseLeave);
}
void button1_MouseLeave(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
void button1_MouseEnter(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
I would not recommend hardcoding image paths.
As you have altered your question ...
There is no (on)MouseOver in winforms afaik, there are MouseHover and MouseMove events, but if you change image on those, it will not change back, so the MouseEnter + MouseLeave are what you are looking for I think. Anyway, changing the image on Hover or Move :
in the constructor:
button1.MouseHover += new EventHandler(button1_MouseHover);
button1.MouseMove += new MouseEventHandler(button1_MouseMove);
void button1_MouseMove(object sender, MouseEventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
void button1_MouseHover(object sender, EventArgs e)
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
To add images to your resources: Projectproperties/resources/add/existing file
I think something like this:
btn.BackgroundImage = Properties.Resources.*Image_Identifier*;
Where *Image_Identifier* is an identifier of the image in your resources.
I made a quick project in visual studio 2008 for a .net 3.5 C# windows form application and was able to create the following code. I found events for both the enter and leave methods.
In the InitializeComponent() function. I added the event handler using the Visual Studio designer.
this.button1.MouseLeave += new System.EventHandler( this.button1_MouseLeave );
this.button1.MouseEnter += new System.EventHandler( this.button1_MouseEnter );
In the button event handler methods set the background images.
/// <summary>
/// Handles the MouseEnter event of the button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_MouseEnter( object sender, EventArgs e )
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
}
/// <summary>
/// Handles the MouseLeave event of the button1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void button1_MouseLeave( object sender, EventArgs e )
{
this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
}
You can create a class based on a Button with specific images for MouseHover and MouseDown like this:
public class AdvancedImageButton : Button {
public Image HoverImage { get; set; }
public Image PlainImage { get; set; }
public Image PressedImage { get; set; }
protected override void OnMouseEnter(System.EventArgs e) {
base.OnMouseEnter(e);
if (HoverImage == null) return;
if (PlainImage == null) PlainImage = base.Image;
base.Image = HoverImage;
}
protected override void OnMouseLeave(System.EventArgs e) {
base.OnMouseLeave(e);
if (HoverImage == null) return;
base.Image = PlainImage;
}
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
if (PressedImage == null) return;
if (PlainImage == null) PlainImage = base.Image;
base.Image = PressedImage;
}
}
This solution has a small drawback that I am sure can be fixed: when you need for some reason change the Image property, you will also have to change the PlainImage property also.

Categories

Resources