C# Enhanced Listbox - remembers additional attributes after postback - c#

I'm in a situation where I need a listbox to remember after postback the attributes I've added to a number of the listitems in it. I found this solution online which appears to solve the issue but am not sure how to implement it.
List box solution
He says he wrote a class that inherits from Listbox which is fine, I've done that and have called it EnhancedListBox but how do I then apply that to the Listbox I'm using on the page?
I can't just substitute
<asp:ListBox >
with
<asp:EnhancedListBox>
but how else do I let the page know I want to use my inherited code?
Thanks.

SaveViewState and LoadViewState are virutal methods that you can override. What you want to try is creating "your own" ListBox:
class EnhancedListBox : ListBox
{
protected override object SaveViewState()
{
// code here from the tutorial
}
protected override void LoadViewState(object savedState)
{
// code here from the tutorial
}
}
This is also called "Creating a custom control", it's very common to do it this way and it gives you great flexibility.

you need to create a custom control for this
check this out http://msdn.microsoft.com/en-us/library/ms366537.aspx for the startup.

Related

what the equivalent to OnResume on Xamarin.forms

Right now, I am using Xamarin.Forms PLC project.
I have a label[x] on Page[x], then I will press button and go to Page[xx] ,then I will go back to Page[x] but I need to update Label[x] Text upon some Choices selected on Page[xx].
Which Event should i use to update Label.Text?
I was overriding OnResuem()Function on Xamarin.android, but it is not working on Xamarin.forms, I have no idea which is the best solution.
Some quick solution for this is:
-overriding the OnAppearing() method of the page and change the label.Text property once you change it on the other page
-Change the property to be public and change it on the other page
-Send the property to the next page as a parameter
but what you should do! is bind your property to a ViewModel and use OnPropertyChange() (Xamarin.Forms way and MVVM architecture) Events: a couple of tutorials how to understand this better:
https://blog.xamarin.com/advanced-data-binding-for-ios-android-and-windows/
https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_bindings_to_mvvm/
https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_binding_basics/
I'm not sure how youre code works because you havent said. So I'm not sure how Page[x] knows about Page[xx] but it sounds to me like you want to use the OnAppearing() override.
Which from the Xamarin.Forms Page API documentation states:
When overridden, allows application developers to
customize behavior immediately prior to the Page becoming visible.
You could do this by adding the following to your Page[x].xaml.cs file
protected override void OnAppearing()
{
//Your code here
}

Drag and drop the user control won't work in .net windows project

I created a user control and it shows up on the tool box as form components. Then when I try to drag and drop the user control on to a form , I get this visual studio error.
" The specified named connection is either not found in the configuration ,not intended to be used with the entity client provider or not valid."
Why am I getting this error?
But some other user controls I can drag and drop which are under the same project. I don't know what I missed in creating this user control.
Beware that code in the UserControl class runs at design time. The constructor, the OnLoad method and Load event. But also methods like OnPaint(). If this code does anything that depends on the environment being setup properly, that code is liable to throw an exception and cause the designer to change its mind about adding the control to the form. That certainly seems to be the case when you get a "not found in the configuration" error, there is no configuration file yet.
Use the DesignMode properly to skip such code. Like this:
protected override void OnLoad(EventArgs e) {
if (!this.DesignMode) {
// Do stuff...
}
base.OnLoad(e);
}
As Hans says, you might need to use the DesignMode property in the Constructor or OnLoad. Also, make sure any public properties that use the connection have this attribute:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Foo
{
get;
set;
}
That way, the designer won't attempt to set them when you add the control to a form. This is always a good habit to get into anyway for properties that you won't be setting at design time.
this error show if you put the code of loading data from database into the constructor of userControl.
"loading data or initialize entity framework"
so the solution is to move the code of loading data from constructor to a method. you can call it "loadData".
and call this method "loadData" in the constructor of the parent form

Overriding protected internal methods

I'm rewriting a web application to ASP.Net 4.0 and have included a Menu control (bound to a SiteMap file, naturally). While I'm liking the new RenderingMode property, I'm hating the fact that it automagically includes some javascript at the bottom of your page to animate the menu.
My preference would be for greater control using jQuery, but switching that off is proving very difficult. With some help from a very heavy hitter, I've been walked through to the point where I've discovered that the ASP.Net 4.0 Menu control has an internal OnPreRender method:
internal void OnPreRender(EventArgs e, bool registerScript);
How do I override this method so that I can call:
base.OnPreRender(e, false);
When trying at the moment, I'm getting an error from Visual Studio saying that "No overload for method 'OnPreRender' takes 2 arguments".
You should be able to refer to protected internal methods as they are protected or internal. If the method is just internal you might be out of luck.
Looking at ASP.NET 4 Menu control, it only has one OnPreRender override:
protected internal override void OnPreRender(EventArgs e);
So you should be able to override it. Not sure where OnPreRender(EventArgs e, bool registerScript) comes from (perhaps it's internal), but the fact that the base class doesn't it(or it's inaccessible) is your problem.
Not a direct answer, but its pretty trivial to dump the contents of any IHeirarchicalDataSource, such as a sitemap file, to a <ul> which any number of jquery menu products can chew and make purdy.

C# Override CheckedListBox

i need to override the base CheckedListBox behaviour.
it is possible to check and uncheck a CheckedListBox without any code attached to it.
i need to disable this behaviour so that i can implement custom code.
any ideas?
thanks.
.
for example:
if (ListenCheckedListBox.GetItemChecked(0))
{
ListenCheckedListBox.SetItemChecked(0, false);
}
if (!ListenCheckedListBox.GetItemChecked(0))
{
ListenCheckedListBox.SetItemChecked(0, true);
}
does not work because the controls default behaviour already does this anyway.
hopefully you can understand my issue now.
You can create your own CheckedListBox by inheriting from the built-in class and overriding the relevant methods.
As I understand your question, you don't want the items to be selected when the user clicks them, you want to control the selection entirely from your code.
To do this, you can override the OnItemCheck method, and control the new value that is being set:
public class CheckedListBoxEx : CheckedListBox
{
protected override void OnItemCheck(ItemCheckEventArgs ice)
{
ice.NewValue = ice.CurrentValue;
}
}
This can also be done by simply handling the ItemCheck event.
ok i figured it out. i was seeing it wrong. i just handle for the checked state instead of defining it twice. once by the control second by me.
yes i was being silly!

How to pass method name to custom server control in asp.net?

I am working on a Customer Server Control that extends another control. There is no problem with attaching to other controls on the form.
in vb.net: Parent.FindControl(TargetControlName)
I would like to pass a method to the control in the ASPX markup.
for example: <c:MyCustomerControl runat=server InitializeStuffCallback="InitializeStuff">
So, I tried using reflection to access the given method name from the Parent.
Something like (in VB)
Dim pageType As Type = Page.GetType
Dim CallbackMethodInfo As MethodInfo = pageType.GetMethod( "MethodName" )
'Also tried
sender.Parent.GetType.GetMethod("MethodName")
sender.Parent.Parent.GetType.GetMethod("MethodName")
The method isn't found, because it just isn't apart of the Page. Where should I be looking? I'm fairly sure this is possible because I've seen other controls do similar.
I forgot to mention, my work-around is to give the control events and attaching to them in the Code-behind.
If you want to be able to pass a method in the ASPX markup, you need to use the Browsable attribute in your code on the event.
VB.NET
<Browsable(True)> Public Event InitializeStuffCallback
C#
[Browsable(true)]
public event EventHandler InitializeStuffCallback;
Reference:
Design-Time Attributes for Components and BrowsableAttribute Class
All the events, properties, or whatever need to be in the code-behind of the control with the browsable attribute to make it so you can change it in the tag code.
Normally you wouldn't need to get the method via reflection. Inside your user control, define a public event (sorry I do not know the vb syntax so this will be in c#)
public event EventHandler EventName;
Now, inside your aspx page, or whatever container of the user control, define a protected method that matches the EventHandler:
protected void MyCustomerControl_MethodName(object sender, EventArgs e) { }
Now, inside your markup, you can use
<c:MyCustomerControl id="MyCustomerControl" runat=server OnEventName="MyCustomerControl_MethodName">
Your workaround is actually the better answer. If you have code that you must run at a certain part of your control's lifecycle, you should expose events to let the container extend the lifecycle with custom functionality.
buyutec and Jesse Dearing both have an acceptable answer.
[Browsable(true)]
lets you see the property in the Properties window. However, the event doesn't show up, which makes no difference to me.
The thing I overlooked earlier was the fact that when you reference a control's even from the tag, it prep-ends On.
Every ASP.NET page is class of its own inherited from Page as in:
class MyPage : Page
Therefore, to find that method via Reflection, you must get the correct type, which is the type of the page class that stores the page code.
I suppose you need to support multiple pages for this control to be instantiated in I believe you can find the child type of any instance of Page via Reflection, but I do not remember how, but you should be able to do it.
but... like everyone else has said, such case is what events are for.

Categories

Resources