Getting dynamically created behaviorID with find() in javascript - c#

I am using a ajaxToolkit:CollapsiblePanelExtender inside a repeater in a aspx page.
Since I need to have a unique behaviorID, I am setting it in this way:
((CollapsiblePanelExtender)(e.Item.FindControl("ControlPanelExtender"))).BehaviorID =
"bhvControlExtenderPanel" + e.Item.ItemIndex.ToString();
Now in Javascript I have to collapse/expand all the created panels according to a certain logic, but I am struggling in performing a partial match using the find() function.
$find("bhvControlExtenderPanel0").collapsePanel();
works, but I have to do it for every Panel, and I don't know the exact number.
Basically I need the equivalent of:
$("[id$='bhvControlExtenderPanel']")
Any idea on how to do that?
Thanks in advance

Try to use Attribute Starts With selector:
$("[id^='bhvControlExtenderPanel']")

Related

Selenium Automation unable to enter username in Way2Automation website

I am trying to do registration for this site
Registration page is inside a popup page.
HTML Code:
<fieldset>
<label>Username:</label>
<input name="username" required="" type="text"/>
</fieldset>
When I try to find the element using below tried code, element is not getting find.
driver.FindElement(By.XPath(".//*[#id='load_form']/fieldset[1]/input")).SendKeys("Kal");
I have also tried this with using CssSelector, but facing the same issue.
driver.FindElement(By.CssSelector("div#load_box form#load_form input[name=username]")).SendKeys("kal");
When I execute above code, I have got an error like element not visible
Can anyone help me on this issue?
Try this below code using xpath locator
driver.FindElement(By.XPath("//input[#name='name']")).SendKeys("Kal");
Explanation of xpath:- Use name attribute of <input> tag.
Suggesstion:- Instead of using absolute xpath, use relative xpath.
Note:- Before reach to this web-element provide some few seconds of wait, so your driver may able to find the web-element. So, you will not get an error like element not visible
Use below xpath:
//*[#id='load_form']/fieldset[6]/input[#name='username']
that site has 2 forms with the id load_form so you're getting the first one which isn't visible since it's the login form. You want the second one which is the register form.
you can use a selector to grab one of the fields that exists on the registration page and then move up to it's parent form and get all descendants that are fieldsets to fill out.
Here is the xpath you can use to pass the text "Dev" into the field labelled with "Name".
driver.findElement(By.xpath("//div[#class='fancybox-overlay fancybox-overlay-fixed']//form[#id='load_form']/fieldset/input[#name='name']")).sendKeys("Dev");
Let me know if this answers your question.
The problem is that there are two username INPUT fields. The way I typically handle this is to find a parent of the element that I want that has an ID or something unique that will distinguish the two elements. In this case, you can use a simple CSS selector,
#load_box input[name='username']
Note the load_box ID that distinguishes the two INPUTs.
Ajax popup on way2automation site is a tricky one because if you look for the username field by name By.name("username"), you will end up with 2 elements - one for username from signup popup, one from singin popup. To avoid this you have to explicity mention the correct element. This can be done via the following code:
webDriver.get("http://way2automation.com/way2auto_jquery/index.php");
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("a[href='#login'"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".ajaxlogin input[name='username']"))).sendKeys("my_username");
As you can see in the code I am using class of the login popup - .ajaxlogin. I have used Java, but the concept is the same - you have to refer to the username element via css selector with popup class included: By.cssSelector(".ajaxlogin input[name='username']")

calling a javascript function in C#

Hi i wanted to show or hide duplicate records according to query. So, I need to know how to call the javascript function from C# codebehind.
<a onclick="Grid1.insertRecord(); return false;" id="a2" href="javascript:">Save</a>
When I click save i need to show a popup which i have written in javascript.
if (!exist)//exists is the query
{
System.Web.UI.Control my = FindControl("a2");
a2.Attributes.Add("onclick", "retrun HideDuplicate()");
This line returns an error saying "a2 doesnot exist in current context."
Why not use an asp.net LinkButton? It has a server side Click event and is accessible from c# code-behind.
The basic <a> tag is not turned into a control by asp.net unless you add a runat="server" to it. Its then turned into a HtmlGenericControl.
<a onclick="Grid1.insertRecord(); return false;" id="a2" href="#" runat="server">Save</a>
This might work for you - its not clear if you have more than one of these links on the page (such as in a row of a gridview?) or if its just on there once.
Also the way you have used javascript is not following best practices but thats a discussion for another day :)
MSDN documentation for programatic creation of client side callbacks without postback with an example where the code behind is in C# might give a good overview of how it is supposed to work in general.
In your case, the corresponding code-behind should implement the interface 'ICallbackEventHandler' and the two methods it describes. In addition, you would need two more client side Javascript functions to prepare and handle the callback, besides the executor/invoker (in your case, a 'save' method). However one of the additional two Javascript functions could be registered in the codebehind, as the example shows.

While using silverlight, is there a way to use a button to change the webpage?

I have an ASP.MVC application which has a silverlight app inside. I want to change the page when I click one of my buttons. Is there a way to make it?
I think the HtmlPage.Navigate method is probably what you're looking for.
You'll need the following using statement:
using System.Windows.Browser;
HtmlPage.Window.Navigate(new Uri("http://www.mypage.com/newPage.html"));
Just stick this guy in the click event of your button and it should do what you want.
Also, if you wish to navigate within your website, you can use a Relative URI, like so:
HtmlPage.Window.Navigate(new Uri("newPage.html", UriKind.Relative));
What should be done is to use the using System.Windows.Browser; statement, and the, in the click method, add the following code:
HtmlPage.Window.Navigate(new Uri("http://yourpagegoeshere.com"));
Thanks overhed!

Why has Request.QueryString["key"] stopped working?

I have a simple web form called default.aspx in the folder structure webroot/folder/
When I navigate to http://myapp/folder/?key=value the page returns fine and when I call
<%= Request.QueryString[0] %>
I get http://myapp/folder/?key=value rendered on the page. However if I call
<%= Request.QueryString["key"] %>
I get nothing, and when I call
<%= Request.QueryString[1] %>
I get Index was out of range. Must be non-negative and less than the size of the collection.
This seems like a very trivial problem but I can't figure out what's going on?!
So it turns out that behind the scenes Sitecore turns the querystring into
?page=the-requested-page.aspx?key=value
But the url in the browser looks as requested. Obviously sticking a second ? in the actual url makes everything after the second ? disappear
If you apply a breakpoint, and then hover over the QueryString in Visual Studio then you should be able to view all of the keys. Alternatively you can foreach over the collection and print out the key names to see if it's slightly different to what you're expecting.
If you're doing something that isn't really supposed to be under Sitecore control (although I have never found it to make working with the QueryString collection impossible), try adding your /folder/ to the ignoreUrl setting. Sitecore will get out of your hair, then ;-)
your query string should be in /default.aspx?key =value then you will be able to access using ["key"] param
you can use
Request.QueryString.Count();
and get the count of querystring modifiy code accordingly

Failing to add controls to a page dynamically

I'm adding a User Control for each record pulled up in a data reader, here's the basic loop:
while (dr.Read())
{
ImageSelect imgSel = new ImageSelect(dr["Name"].ToString());
myPanel.Controls.Add(imgSel);
}
The problem is that there are no controls added to the page, I check the html output and there is my panel, with nothing in it.
I even stepped through the code in the debugger, and verified that myPanel.Controls gets a control added on each loop, with the count being 6, no errors, but then they dont show up on the page.
I've run the above code in the Page_Init and Page_Load events, both with the same result.
EDIT:
Ok so I've switched to using LoadControl("....ascx") to get my control instance, which is now working. But originally I was also passing in data via the controls constructor.. Is this still possible or do I just need to set them via get/sets?
EDIT 2:
Thanks to Freddy for pointing out that the LoadControl has an overload where you CAN pass in constructor params, see accepted answer.
EDIT 3:
After trying this method both with and without the constructor. I have found its better to just use setters for any properties I want the control to have versus trying to use the passed in object array for my constructor.
Update: As Steve pointed out, the overload of LoadControl that uses the type won't take into account the controls in the ascx. This is also mentioned in this answer: Dynamically Loading a UserControl with LoadControl Method (Type, object[]).
As I mentioned before, the get/set are more in line with the asp.net model, so I recommend using that with the LoadControl variation that receives the user control path. That said, the Steve's version is an interesting alternative: http://www.grumpydev.com/2009/01/05/passing-parameters-using-loadcontrol/.
My take is the LoadControl with type is meant to be used with web custom controls instead.
If it is an user control you should use LoadControl(usercontrolpath) to get the instance of the user control.
You can use a constructor by doing:
var name = dr["Name"].ToString();
var imgSel = LoadControl(typeof(ImageSelect), new object[]{ name });
myPanel.Controls.Add(imgSel);
Notice that depending on the project model you are using, you need to add a Reference to the aspx to use it with the typeof variation:
<%# Reference Control="~/somepath/myusercontrol.ascx" %>
Ps. I usually use the set/get for controls as I find them more in line with the asp.net model
To add UserControls you must call the LoadControl method passing in the path to the .ascx file. You can not create them by just instantiating the object the .ascx file inherits from.
A UserControl consists of both the markup and the class in the code behind. The markup contains a link to the class behind, but the class behind does not know where the markup lives and therefore can not be created on it's own.

Categories