Server Control Render inside Server Control - c#

I have 2 server control
One create Items
the other one create a List of items.
So i have a public Item with a viewstate in the first
in the page when i add the server control name (Server control name 1) to a panel it render (with a createChildControls) and add to a public List<Server Name 1> that is a view state in the server control 2.
so i make
foreach (ServerControl_1 a in ServerControl_2)
output += a;
the result is the namespace of the item not the text.
So i must have to render it first and then add to the output...
But i just dont know how...
Someone help me?

use something like
protected String displayName(Object item)
{
String name = "";
if (item != null && item.hasOwnProperty("name")) {
name = item["name"];
}
return name;
}
call this in your for loop.
output += displayName(a)

Related

Read assembly MethodBody, parse its Name in a ListBox and its IL in a TextBox?

I have a WPF MainWindow Form with a ListBox and a TextBox that looks like this:
Figure A. WPF MainWindow with Sample Text.
Now, the Load Assembly... OnClick button event allows me to select a .NET Assembly and load it up using DnLib
Then, if I want to display the Methods bodies I would do it like so:
Assembly asm = Assembly.LoadFile(filename);
foreach (Module mod in asm.GetModules())
{
foreach (Type types in mod.GetTypes())
{
foreach (MethodInfo mdInfo in types.GetMethods())
{
listBox.Items.Add(mdInfo.Name);
}
}
}
This adds each found Method name to the ListBox on the left, resulting like so:
Figure B. Showing the ListBox Filled with Methods Names
Now the trick part, I would like to for whichever method I select from the ListBox to display its respective MethodBody IL on the TextBox
How can I achieve such thing?
«Phew!» Finally Solved it!
Here's the solution for whoever tries to do the same thing in the future.
Make an instance of 'List' and then iterate through the methods and assign the names to such list, then whenever your SelectedItem index value changes, I can simply call GetMethodBodyByName and then I can surely solve this issue
Here's how to implement the function GetMethodBodyByName:
public string GetMethodBodyByName(string methodName)
{
ModuleDefMD md = ModuleDefMD.Load(filename);
foreach (TypeDef type in md.Types)
{
foreach (MethodDef method in type.Methods)
{
for (int i = 0; i < type.Methods.Count; i++)
{
if (method.HasBody)
{
if (method.Name == methodName)
{
var instr = method.Body.Instructions;
return String.Join("\r\n", instr);
}
}
}
}
}
return "";
}
The idea is that 'GetMethodBodyByName' will receive the method name as a parameter, then it will iterate through methods and see if a method matches the given name, then if found, the function will just simply iterate through that method and output the method's body.
Here's how my ListBox_SelectedItemChanged event looks like:
private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
textBox.Text = "";
textBox.Text = GetMethodBodyByName(method[listBox.SelectedIndex].Name);
}
That's All Folks!
Note: Be careful when doing this approach as if when you request names, different methods can have the same names. But that's a cake for another day, I'm done for now! take care bye-bye!
Working our way up for the Ultimate Solution!
The WPF MainWindow Forms carry with themselves two little useful properties, they are: Tag and Content, the idea is the following one:
With the Tag and Content Property we can assign any values to it that later it can be retrieved On-The-Fly without having to depend on Methods names specifically for this task.
So you would instead of looping each method and get its name respectively you can just do the way I did:
Iterate through the Method, and assign its body to the Tag property, and its name to the Content property, as this last property is the one that handles the actual Title property, so disregarding anything you do with the method in the future and even if it had the same name of another one, it will work no matter what.
How Can We Implement It?
Simply:
<...>
// Inside Method Body iteration routine...
<...>
var instr = mdInfo.Body.Instructions;
// Allocate in a new `ListBoxItem` each method and add it to the current listbox with their
// ... respective Tag and Content information... // Many Thanks Kao :D
newItem = new ListBoxItem();
newItem.Content = mdInfo.Name;
newItem.Tag = string.Join("\r\n", instr);
method.Add(mdInfo);
listBox.Items.Add(newItem);
Then on your SelectedItem Index-Value-Changed Event put this:
MSILTextBox.Clear();
// Retrieve them given the selected index...
// ... the returned value will be the Tag content of the ...
// ... previously saved item.
string getTag= ((ListBoxItem)listBox.SelectedItem).Tag.ToString();
MSILTextBox.Text = getTag;

How to put string as panel name in c#

I have panel that visibility is false. I want when click on treeview list, that list will retrive the panel name that i stored in database. i know how to retrieve that panel name from database in string. But how to change that 'string' into panel to make able to write like this:
panelNamethatLoadFromDB.visible = true;
My Code:
DataTable dtPanelToView = MyLibrary.getResults("SELECT LEVEL1_ID, LEVEL1_PATH FROM LEVEL1_TREEVIEW WHERE LEVEL1_DESC='" + clickLink + "'");
if (dtPanelToView.Rows.Count > 0)
{
string panelToDisplay = dtPanelToView.Rows[0]["LEVEL1_PATH"].ToString();
}
So, currently this "panelToDisplay" is string that contains panel name. i want to change this panel visibilty. Example : panelToDisplay.visible = true;
WinForms stores the controls diplayed on a form in the Form.Controls collection.
You could loop over this collection to find your panel
foreach(var pan in yourForm.Controls.OfType<Panel>())
{
if(pan.Name == panelToDisplay)
{
pan.Visible = true;
}
}
Using the IEnumerable extensions you could also avoid the explicit loop with
var pan = yourForm.Controls.OfType<Panel>().FirstOrDefault(x => x.Name == panelToDisplay);
if(pan != null)
pan.Visible = true;
Keep in mind that the Controls collection of the form stores only the first level of controls. If a control contained in the Form's Controls collection is a control container then it has its Controls collection where other controls could be stored.
EDIT
As from the comment below from TaW you could also use the Controls collection with a string indexer
if(this.Controls.ContainsKey(panelToDisplay))
this.Controls[panelToDisplay].Visible = false;

How can I access all the properties in one tab in an umbraco node?

Is there a way that I can access tabs in umbraco using C#? I am trying to loop through each property in a particular tab so that I can show/hide that section of the website depending on whether that tab has content in it or not.
I have tried ContentType.Tab.GetTab(); but that takes an id and I can't find a tab id anywhere.
Thanks.
you can use getVirtualTabs method then loop foreach property inside that tab
Node current = Node.GetCurrent();
DocumentType dt = DocumentType.GetByAlias(current.NodeTypeAlias);
if (dt != null) {
foreach(var tab in dt.getVirtualTabs) { //get all tabs
foreach(var propertyType in tab.PropertyTypes) { //loop through each property inside the Tab
// propertyType.Name
//....write here your code
}
}
}

Unique persistent control identifier

What we have
We have some complex winforms control. To store its state we use some custom serialized class. Lets say we've serialized it to xml. Now we could save this xml as a file in User directory or to include it in some another file....
But...
The question is,
if user creates several such controls across his winform application (at design time), what unique identifier is better to use in order to know which of the saved configs belongs to which of these controls?
So this identifier should:
Stay the same across application launches
Automatic given (or already given, like we can assume that Control.Name is always there)
Unique across application
I think one could imagine several ways of doing it and I believe there are might be some default ways of doing it.
What is better to use? Why?
This small extension method does the work:
public static class FormGetUniqueNameExtention
{
public static string GetFullName(this Control control)
{
if(control.Parent == null) return control.Name;
return control.Parent.GetFullName() + "." + control.Name;
}
}
It returns something like 'Form1._flowLayoutPanel.label1'
Usage:
Control aaa;
Dictionary<string, ControlConfigs> configs;
...
configs[aaa.GetFullName()] = uniqueAaaConfig;
I've been using a compound indentifier made of a full tree of control hierarchy. Assuming that your form name is Form1, then you have a groupbox Groupbox1 and a textbox TextBox1, the compound identifier would be Form1/Groupbox1/TextBox1.
If you'd like to follow this, here are the details:
http://netpl.blogspot.com/2007/07/context-help-made-easy-revisited.html
This is the method I've ended up creating to define a unique name that includes the full name of the form (with it's namespace) then each parent control above the control in question. So it could end up being something like:
MyCompany.Inventory.SomeForm1.SomeUserControl1.SomeGroupBox1.someTextBox1
static string GetUniqueName(Control c)
{
StringBuilder UniqueName = new StringBuilder();
UniqueName.Append(c.Name);
Form OwnerForm = c.FindForm();
//Start with the controls immediate parent;
Control Parent = c.Parent;
while (Parent != null)
{
if (Parent != OwnerForm)
{
//Insert the parent control name to the beginning of the unique name
UniqueName.Insert(0, Parent.Name + ".");
}
else
{
//Insert the form name along with it's namespace to the beginning of the unique name
UniqueName.Insert(0, OwnerForm.GetType() + ".");
}
//Advance to the next parent level.
Parent = Parent.Parent;
}
return UniqueName.ToString();
}

Actual Element That Lost Focus

I am working on an application that has a GridView item on an ASP.net page which is dynamically generated and does a partial post-back as items are updated within the grid-view. This partial post-back is causing the tab indices to be lost or at the very least ignored as the tab order appears to restart. The grid view itself already has the pre-render that is being caught to calculate the new values from the modified items in the grid-view. Is there a way to get what element had the focus of the page prior to the pre-render call? The sender object is the grid-view itself.
You can try using this function, which will return the control that caused the postback. With this, you should be able to reselect it, or find the next tab index.
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;
}
Here's an instance where I had dynamically generated inputs that updated totals via AJAX on change. I used this code to determine the next tab index, based on the tab index of the control that caused the postback. Obviously, this code is tailored to my usage, but with some adjustments I think it could work for you as well.
int currentTabIndex = 1;
WebControl postBackCtrl = (WebControl)GetControlThatCausedPostBack(Page);
foreach (PlaceHolder plcHolderCtrl in pnlWorkOrderActuals.Controls.OfType<PlaceHolder>())
{
foreach (GuardActualHours entryCtrl in plcHolderCtrl.Controls.OfType<GuardActualHours>())
{
foreach (Control childCtrl in entryCtrl.Controls.OfType<Panel>())
{
if (childCtrl.Visible)
{
foreach (RadDateInput dateInput in childCtrl.Controls.OfType<RadDateInput>())
{
dateInput.TabIndex = (short)currentTabIndex;
if (postBackCtrl != null)
{
if (dateInput.TabIndex == postBackCtrl.TabIndex + 1)
dateInput.Focus();
}
currentTabIndex++;
}
}
}
}
}

Categories