Adding CompareValidator to dynamically added text box - c#

I have a form that needs dynamic input boxes that have to be integers. I used a slightly modified version of the code found here to do that: http://www.learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx
I have a the following code to add to the placeholder:
CompareValidator cmpVal = new CompareValidator();
cmpVal.ID = "cv" + textboxID;
cmpVal.ControlToValidate = textboxID;
DynamicTextBoxIntegerValidation.Controls.Add(cmpVal);
Obviously this is missing two very important pieces; the Type and Operator fields. The problem is I can't figure out how to add them. Any help would be appreciated.

Type and Operator are simply properties of CompareValidator. You can add them as follows:
CompareValidator cmpVal = new CompareValidator();
cmpVal.ID = "cv" + textboxID;
cmpVal.ControlToValidate = textboxID;
cmpVal.Type = ValidationDataType.Integer; //Set your type and operator here.
cmpVal.Operator = ValidationCompareOperator.Equal;
DynamicTextBoxIntegerValidation.Controls.Add(cmpVal);

Related

How to get the slidetype (type-attribute) of a slidelayout-slide?

Application is a C# based VSTO-AddIn for PowerPoint.
Each SlideLayout in powerpoint has a type. Is there way to get the type in an VSTO-AddIn? Up to now I just know how to get it from the SlideLayout.xml
Yes, the PowerPoint object model exposes an enumeration PpSlideLayout so something like
PowerPoint.PpSlideLayout theLayout = theSlide.Layout;
Here's a code snippet that creates a new slide (which requires assigning a CustomLayout), shows the string values of the custom layout and the PpSlideLayout, then assigns a different PpSlideLayout
PowerPoint.Presentation p = this.Application.ActivePresentation;
PowerPoint.PpSlideLayout layoutBlank = PowerPoint.PpSlideLayout.ppLayoutBlank;
PowerPoint.CustomLayout custLayout = p.SlideMaster.CustomLayouts[1];
PowerPoint.Slide s = p.Slides.AddSlide(2, custLayout);
System.Windows.Forms.MessageBox.Show(s.CustomLayout.Name + ", " + s.Layout.ToString());
s.Layout = layoutBlank;

How to add Attribute Tagged Value using C#

I am trying to create an EA Attribute and after that I am adding Tagged Value to that attribute.
The problem is when I create the tagged value for the attribute without any value then its creating fine with proper Type but when I fill some value into the tagged then the Type of the tagged value is changing.
EA.Attribute headerName = eleName.Attributes.AddNew("Header", "char");
headerName.Update();
EA.AttributeTag decAtt = headerName.TaggedValues.AddNew("Description", "<memo>");
decAtt.Update();
decAtt.Value = "Description needs to entered";
How to add the Tagged values for the attribute without changing the properties ?
How to add the contents to Tagged values note through Adddin ?
Thanks in advance.
Try this:
EA.Attribute headerName = eleName.Attributes.AddNew("Header", "char");
headerName.Update();
EA.AttributeTag decAtt = headerName.TaggedValues.AddNew("Description","");
decAtt.Value = "<memo>";
decAtt.Notes = "Description needs to be entered";
decAtt.Update();
PS. I'm surprised the Attributes.AddNew("Header", "char") works for you. I would never trust the AddNew operation to define the type of my attributes.

How do I populate a 'asp:ListBox' with an array of strings?

I have an asp:Listbox that I need to switch out the items on depending on user selection. Here is what I have tried:
string[] my2012Departments = new string[5];
my2012Departments[0] = "Administration";
my2012Departments[1] = "Imaging Services";
my2012Departments[2] = "IT";
my2012Departments[3] = "Lab";
my2012Departments[4] = "Support Services";
lstDSYDepartment.Items.AddRange(my2012Departments.ToArray());
//The AddRange will also not work without .ToArray()
This however causes the following errors:
1. Cannot Convert from 'string[]' to 'System.Web.UI.WebControls.ListItem[]'
2.The best overloaded method match for ....AddRange.... has some invalid arguments
According to the documentation this should work as long as I put the code in the form load.
You can create a List<string> and assign that as the datasource for your listbox like below
List<string> my2012Departments = new List<string>();
my2012Departments.Add("Administration");
my2012Departments.Add("Imaging Services");
my2012Departments.Add("IT");
my2012Departments.Add("Lab");
my2012Departments.Add("Support Services");
lstDSYDepartment.DataSource = my2012Departments;
lstDSYDepartment.DataBind();
(OR) Instead of assigning a string array; create a array of ListItem[] like below
ListItem[] my2012Departments = new ListItem[5];
my2012Departments[0] = "Administration";
my2012Departments[1] = "Imaging Services";
my2012Departments[2] = "IT";
my2012Departments[3] = "Lab";
my2012Departments[4] = "Support Services";
this.lstDSYDepartment.Items.AddRange(my2012Departments);
The documentation you refer to is for Windows Forms, not for ASP.
Try
lstDSYDepartment.Items.Add("Administration");
lstDSYDepartment.Items.Add("Imaging Services");
...

reflection with stacktrace / stackframe to get both methods name and parameters

while writing the question subject I came across
some other allmost related post ...leading to MSDN
http://msdn.microsoft.com/en-us/library/system.reflection.parameterinfo.aspx
but i couldn't manage to extract the bit of code i needed
i just learnd how to get method name with a helper method based on st and sf as following :
public void setLogView(View ViewMode)
{
AAdToAppLog();
Lview_AH_AutomationLog.Sorting = SortOrder.Ascending;
ColumnHeader ColHeadRowNo = new ColumnHeader();
ColumnHeader ColHeadFunction = new ColumnHeader();
ColumnHeader ColHeadContent = new ColumnHeader();
ColumnHeader ColHeadTime = new ColumnHeader();
Lview_AH_AutomationLog.View = ViewMode;
Lview_AH_AutomationLog.Columns.Add(ColHeadRowNo);
Lview_AH_AutomationLog.Columns.Add(ColHeadFunction);
Lview_AH_AutomationLog.Columns.Add(ColHeadContent);
Lview_AH_AutomationLog.Columns.Add(ColHeadTime);
ColHeadRowNo.Text = "#";
ColHeadFunction.Text = "Function Name";
ColHeadContent.Text = "Content";
ColHeadTime.Text = "Time";
ColHeadRowNo.Width = 45;
ColHeadFunction.Width = 150;
ColHeadContent.Width = 150;
ColHeadTime.Width = 100;
}
public void AAdToAppLog(string FunctionOutPut = "N/A")
{
string t = DateTime.Now.ToString("mm:ss.ff");
string h = DateTime.Now.ToString("HH");
ListViewItem FirstCell= new ListViewItem();
FirstCell.Text =h+":" +pre0Tosingle_9(LogCounter.ToString());//Another helper puts 0 infront <=9 digits
Lview_AH_AutomationLog.Items.Insert(0, FirstCell);
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
string FunctionName = sf.GetMethod().ToString().Replace("Void", "").Replace("System.Windows.Forms.", "");
FirstCell.SubItems.Add(FunctionName);
FirstCell.SubItems.Add(FunctionOutPut);
FirstCell.SubItems.Add(t);
LogCounter++;
}
so in every method i want i just put the
AddToAppLog()
that method contains my call to AddToAppLog() and then reports(Via ListView) the name of method and i just added the time of the call.
there's two things i would like to address in this post , about my implementation of that helper metod :
the "FunctionName" i recive from sf.GetMethod is nice that it throws the type of the Parameter of a given Method i liked the idea , only that it is containing parameter.type's Father + Grandfather + Great-Grandfather, but i would like only the bottom Type :
System.Windows.Forms.View
this is one of the shortest (: and i tried to get to View by it self via playing with .Replace()
so is there anothere way to strip it down or actually another method in same family that extract it the way i mentiond above or i could use a list containing every possible most-used types and do a foreach with stringReplace?
and more importently , how do i get the Method(parameterName) as well ?
thanks alot in advance !!
can someone Show An easy to comprehend , Simple syntax Example
of getting parameters name ?
The crux of this is the line that involves sf.GetMethod(); if you instead store that:
var method = sf.GetMethod();
string name = method.Name;
var parameters = method.GetParameters();
you have access to the full signature, and note that the .Name is just the simple name - not the full declaring type-name, etc. You can of course access method.DeclaringType.Name if you want more context. Note that you can only get the declaration of the parameters; you can't get their values via any normal mechanism.
However!!! I will also observe that all this has a performance cost associated with reflection and stack-walking. If you have the C# 5 compiler, you may prefer:
public void AAdToAppLog([CallerMemberName] string callerName = "")
{ ... }
which means that the compiler will supply the name of the caller voluntarily (as a constant geneated in the IL) - no need to either supply it, or go walking the stack to figure it out. You cannot, however, get the parameters like this.

Adding attributes to validator programmatically

I'm building a function to allow me to add validators into dynamically created tables. They work, in that they show up on the page and function properly. But I'm trying to add the "Display" attribute via the codebehind, and any combination of parameters fails...
RequiredFieldValidator newRQValid = new RequiredFieldValidator();
newRQValid.SetFocusOnError = true;
newRQValid.ControlToValidate = txtID;
newRQValid.Display = "dynamic"; <<---
strID = "cv" + cellID;
newRQValid.ID = strID;
newRQValid.ErrorMessage = txtErrorMessage;
newRQValid.InitialValue = initval;
tCell.Controls.Add(newRQValid);
I've tried with and without quotes, but "Dynamic" doesn't even appear in the autocomplete, so I'm assuming I'm just plain mistaken.
I have similar issues adding attributes to a compare validator as well:
CompareValidator newCMValid = new CompareValidator();
newCMValid.SetFocusOnError = true;
newCMValid.ControlToValidate = cellID;
newCMValid.ControlToCompare = "txt_clm_dob";
newCMValid.Type = ValidationDataType(DateTime); <<==
newRGValid.Display = Dynamic; <<==
strID = "cv" + cellID;
newCMValid.ID = strID;
newCMValid.ErrorMessage = txtErrorMessage;
newCMValid.Operator = LessThanEqual; <<==
tCell.Controls.Add(newCMValid);
With several attempts on each of those as well.
So what's the right syntax there, or is adding those attributes somehow not allowed here?
newRQValid.Display = ValidatorDisplay.Dynamic;
newCMValid.Type = ValidationDataType.Date;
newCMValid.Operator = ValidationCompareOperator.LessThanEqual;

Categories

Resources