How do I check the display styling of an element with Watin? - c#

I'd like to do the following with watin. Maybe you guys know the best ways to go about doing this. I've been unable to find any thorough documentation on watin's power, I've even checked the homesite with little luck.
I'd like to...
Check the visibility/display value of a div.
Check the class name of an element.
eg:
Span this_span = Span(Find.ByText("Loading..."));
if (this_span.Visibility=="True")
{
}
and
if (this_span.Class.Value=="classname")
{
}
I appreciate any help!

if (this_span.Style.Display != "none")
{
}

Check the class name of an element.
if (this_span.ClassName=="classname")
{
}
Check the class name of an element.
if (this_span.Style.GetAttributeValue("display")!="none")
{
}

Related

How to check if EPIserver.Core.ContentReference is true or contains something?

We have a block in our EPIserver website called KeyVisualBlock. This is basically the header section of our pages consisting of things like a hero image, page title.
Our users would like to be able to insert a form into this area of the page.
So I have modified our Model KeyVisualBlock.cs to include the ability to select a form by adding:
[CultureSpecific]
[Display(Order = 90,
GroupName = SystemTabNames.Content)]
[AllowedTypes(typeof(FormContainerBlock))]
public virtual ContentReference ContactForm { get; set; }
Now I would like to check if this field contains a form and then display the form in our view. So in our View I am trying to do something like this:
#{
if (Model.CurrentKeyVisualBlock.ContactForm) {
// do something.
}
}
But Visual Studio informs me that:
Cannot implicitly convert type 'EPIserver.Core.ContentReference' to
type 'bool'
What is the preferred way to check this?
You can check whether the content reference is set like this:
if (!ContentReference.IsNullOrEmpty(Model.CurrentKeyVisualBlock.ContactForm))
{
// Do stuff
}
But keep in mind that even though the content reference isn't null the content it refers to might not exist. To be really sure you need to actually load the content, preferably like this:
// Constructor injected IContentRespository into field contentRepository.
if (this.contentRepository.TryGet<FormContainerBlock>(Model.CurrentKeyVisualBlock.ContactForm, out var formContainerBlock))
{
}

How radio button click if included onclick function?

<asp: label input name="id" value="p" onclick="form.submit();
return false;" type="radio">personal / romance
/label>
I am trying below both code but not getting any solution. Please if anyone have any good solution then it's really good help for me.
webBrowser1.Document.GetElementById("id").SetAttribute("value", "P");
foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("label"))
{
if (el.InnerText == ("personal romance"))
{
el.InvokeMember("Click");
}
}
Your code looks okay, but the InnerText of the label is not personal romance
Try it like this:
string searchString = #"personal / romance";
if(el.InnerText.Equals(searchString)
{
el.InvokeMember("click");
}
Check when expect this element if the correct tag name is label. I can tell you for sure that this is not asp:label. You can check with Inspect Elements in Chrome, if the tag name is different use correct one when you are taking webBrowser1.Document.GetElementsByTagName(correct tag)

Exist control in current form?

I need to find out if component with some name exist in current form.
I have name of the component in string variable and if it doesnt exist, i need create it.
I use this code
Control c = Controls.Find(New, true)[0]; //najiti komponenty
if (c == null) {}
But it gives me error that the index was outside the bounds of the array.
I know this code is bad, but i dont know to write it good and google dont help me.
Find method return an array of controls, i.e. Control[]. You are trying to access the first element of the empty array, thus resulting in IndexOutOfRangeException
You should try:
Control[] controls = Controls.Find(New, true);
if (controls.Length > 0)
{
//logic goes here
}
else
{
//no components where found
}
Try using the Control.ContainsKey() Method, (pass a string variable containg the control name instead of the quoted text in my example):
if (!this.Controls.ContainsKey("MyControlName"))
{
// Do Something
}

How to use LowprofileImageLoader and Image Place holder together in wp7?

How can we use LowprofileImageLoader and Image Place holder together in wp7 app?
I have gone through this and this link but unable to use together.
But people have said in comments that they are able to use both by changing the following code in PlaceImage.cs:
if (null != _frontImage)
{
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
Delay.LowProfileImageLoader.SetUriSource(this, ((BitmapImage)this.Source).UriSource);
}
}
But when I do so, I got invalid arguments error. Can somebody suggest me how to solve this problem?
It should be:
if (null != _frontImage)
{
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
Delay.LowProfileImageLoader.SetUriSource(_frontImage, ((BitmapImage) this.Source).UriSource);
}
}
It's the _frontImage's UriSource that you want to set.

Problem looping through web controls

I've got a web page where I am dynamically creating controls during Page_Load event (this is done so because I do not know how many controls I will need until session is active and certain variables are accessible)
I need to be able to loop through these controls to find Checkbox when a button click is processed. Looping through the Form.Controls does not appear to be sufficient. I would think that Request.Form might work but it does not appear to be accessible in my C# block?
What should code for Request.Form look like? OR
Has anyone done this before with dynamically created controls?
Any insight is appreciated.
Simplified Example from MSDN:
var myControl = FindControl("NameOfControl");
if(myControl != null)
{
//do something
}
else
{
//control not found
}
Hope this helps! ;)
Your controls will be accessible trough the Controls collection of their immediate parent. Unless you add them like Page.Form.Controls.Add (myControl);, you won't find it in Page.Form.Conttrols. If you add them to a place holder, you must find them in thePlaceHolder.Controls.
LinkButton myDynamicLinkButton = new myDynamicLinkButton ();
myDynamicLinkButton.ID = "lnkButton";
myPlaceHolder.Controls.Add (myDynamicLinkButton);
//........
LinkButton otherReferenceToMyLinkButton = myPlaceHolder.FindControl ("lnkButton");
As #David said in his comment, you should probably think about using a Repeater instead. It would probably simplify your case a lot.
Since the controls might be nested in other controls, you need to search recursively. You can use this method to find the control:
public Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
And you can implement it this way:
CheckBox check = FindControlRecursive(Page.Form, "CheckBox1");
You should have access to Request["xyz"] anywhere in your aspx.cs code. You can either find control as described above and read it's value or do so directly from Request using the Control.UniqueID property. For example if it's a checkbox that's within the repeater then the UniqueID would look like dtgData$ctl02$txtAmount
Thanks for the insight guys. I kind of took the discussion and ran with it and found my solution that worked best for me.
foreach(String chk in Request.Form)
{
if (chk.Contains("chkRemove"))
{
int idxFormat = chk.LastIndexOf("chkRemove");
objectname = chk.Substring(idxFormat);
}
}
Turned out really all I needed was the name. The string contained a number at the end which was needed to determine a position of datatable items. Thanks for the advice!

Categories