I have this line in my .aspx code:
<div id="view" class="g2" runat="server">
</div>
but when I want to find this element in C#(aspx.cs) and send data for it, C# cant find it this way:
view.Controls.Add(item);
*item is a html element that I made through this code:
for(int i=0; i<foods.Count; i++)
{
HtmlGenericControl item = new HtmlGenericControl("div");
item.Attributes["class"] = "items";
item.Style["border_bottom"] = "3px solid aqua";
HtmlImage img = new HtmlImage();
img.Src = foods[i].picGet();
HtmlGenericControl mask = new HtmlGenericControl("div");
item.Attributes["class"] = "mask";
HtmlGenericControl h2 = new HtmlGenericControl("h2");
item.InnerHtml = foods[i].fnameGet();
HtmlGenericControl price = new HtmlGenericControl("span");
item.Attributes["class"] = "price";
item.InnerHtml = foods[i].priceGet().ToString() + "ریال";
HtmlGenericControl desc = new HtmlGenericControl("p");
item.InnerHtml = foods[i].describeGet();
mask.Controls.Add(h2);
mask.Controls.Add(price);
mask.Controls.Add(desc);
item.Controls.Add(img);
item.Controls.Add(mask);
view.Control.Add(item);
}
I have written the namespace System.Web.UI.HtmlControls
I saw a course that did the same thing and was successful
the error is that even my visual studio cant find the view that i used in the last line of the for loop.
You can use Control.FindControl to search control with the specified id parameter as follow:
Control view = FindControl("view");
//Then add your item to div
view.Controls.Add(item);
Related
<div id="targetSummaryCount" class="large-text" data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$target_key_30.1.1.0">
<span data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$target_key_30.1.1.0.0">0</span>
<span data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$target_key_30.1.1.0.1">/</span>
<span data-reactid=".0.0.0.3.0.0.1.0.0.0.0.$target_key_30.1.1.0.2">20</span>
</div>
I want to get the '0' and '20' values from the above html, using the div id 'targetSummaryCount'. Is there any way to do this in Selenium C# without using xpath?
You can use a CSS selector:
driver.FindElements(By.CssSelector("#targetSummaryCount > span"))
This would match all span elements directly under the element with id="targetSummaryCount".
I have posted the solution in Java. Please Convert it to C#
WebElement divSummary = driver.findElementById("targetSummaryCount");
List<WebElement> targetKeys = divSummary.findElements(By.tagName("span"));
// For getting first and last element
targetKeys.get(0);
targetKeys.get(targetKeys.size()-1);
// For maintaining queue of elements with text 0 or 20
HashMap <Integer, List<WebElement>> elementmap = new HashMap <Integer, List<WebElement>>();
List<WebElement> webElements1 = new ArrayList<WebElement>();
List<WebElement> webElements2 = new ArrayList<WebElement>();
for (WebElement elem : targetKeys){
if(elem.getText().trim().equals("0")){
webElements1.add(elem);
}
else if (elem.getText().trim().equals("20")){
webElements2.add(elem);
}
}
elementmap.put(0,webElements1);
elementmap.put(20,webElements1);
// elementmap.get(0) contains all span tag under target Summary Count div tag
// elementmap.get(20) contains all span tag under target Summary Count div tag
To get the "0" and "20" with an XPath:
string value_00 = driver.FindElementByXPath("//div[#id='targetSummaryCount']/span[1]").Text;
string value_20 = driver.FindElementByXPath("//div[#id='targetSummaryCount']/span[3]").Text;
// or
var elements = driver.FindElementsByXPath("//div[#id='targetSummaryCount']/span");
string value_00 = elements[0].Text;
string value_01 = elements[2].Text;
And with a Css Selector:
string value_00 = driver.FindElementByCssSelector("#targetSummaryCount > span:nth-child(1)").Text;
string value_20 = driver.FindElementByCssSelector("#targetSummaryCount > span:nth-child(3)").Text;
// or
var elements = driver.FindElementsByCssSelector("#targetSummaryCount > span:nth-child(odd)");
string value_00 = elements[0].Text;
string value_01 = elements[1].Text;
You can try this.
var spanCount = driver.FindElements(By.XPath("//*[#id='targetSummaryCount']/span")).Count;
var myResult = "";
for (int spanIndex = 0; spanIndex < spanCount; spanIndex++)
{
var spanText = driver.FindElement(By.XPath("//*[#id='targetSummaryCount']/span[" + spanIndex + "]")).Text;
if (spanText != "/")
{
myResult += "span:" + spanIndex + " spanText:" + myResult;
}
}
You can try this. Fetching the span which is the child of the div having id targetSummaryCount and text as 0 or 20.
var span_00 = driver.FindElementByXPath("//div[#id='targetSummaryCount']/span[text()='0']");
var span_20 = driver.FindElementByXPath("//div[#id='targetSummaryCount']/span[text()='20']");
I've got the following code:
HtmlGenericControl li = new HtmlGenericControl("li");
ULRouting.Controls.Add(li);
HtmlGenericControl anchor = new HtmlGenericControl("a");
li.Attributes.Add("myCustomIDAtribute", "11");
anchor.InnerText = "TabX";
li.Controls.Add(anchor);
I'm trying to find out how you add a dropdown box to the li control when you dynamically create the li please ie:
<asp:DropDownList ID="cmbRoutingStage_1" runat="server" DataSourceID="sqlDBStages" DataTextField="stages" DataValueField="StageRefID"></asp:DropDownList>
Try this:-
DropDownList ddl = new DropDownList();
ddl.ID = "cmbRoutingStage_1";
ddl.DataSourceID = "sqlDBStages";
ddl.DataTextField = "stages";
ddl.DataValueField = "StageRefID";
li.Controls.Add(ddl);
As a side note you can use HtmlAnchor class for generating the anchor tag:-
HtmlAnchor anchor = new HtmlAnchor();
I'm using a javascript menu from dynamicdrive
I'd tried to get the menu items from a database.
my aspx file contain the menu and it works fine when it's a static menu.
my table or my query actually produces:
IdPage int, PageTitle varchar(20), PageFileUrl varchar(30), ParentIdPage int
My methods to get data
DataRow[] dataRowParent = _dataTable.Select("[ParentIdPage]=" + 0);
foreach (DataRow dr in dataRowParent)
{
HtmlGenericControl li = new HtmlGenericControl("li");
// add <a>
HtmlGenericControl hlink = new HtmlGenericControl("a");
if (dr["PageFileUrl"].ToString() == "") // this item has a submenu.
{
li.Attributes.Add("rel", "ddsubmenu" + dr["IdPage"].ToString());
hlink.Attributes.Add("href", "#");// link should be # when no direct link
hlink.InnerText = dr["PageTitle"].ToString();
li.Controls.Add(hlink);
ulTopMenu.Controls.Add(li);
AddNewUl((int)dr["IdPage"]);
AddSubmenuItems(_dataTable, (int)dr["IdPage"]);
}
else // Direct link ,no submenu
{
hlink.Attributes.Add("href", dr["PageFileUrl"].ToString());
hlink.InnerText = dr["PageTitle"].ToString();
li.Controls.Add(hlink);
ulTopMenu.Controls.Add(li);
}
}
}
private void AddSubmenuItems(DataTable dataTable, int menuId)
{
// create related sub menu
DataView dataView = new DataView(dataTable);
dataView.RowFilter = "ParentIdPage=" + menuId;
foreach (DataRowView subMenuItem in dataView)
{
// find related <ul>
HtmlControl ulControl = (HtmlControl)FindControl("ddsubmenu" + menuId);
// Add new <li><a href="PageFileUrl.aspx" >page title</a> </li>
HtmlGenericControl li = new HtmlGenericControl("li");
HtmlGenericControl hlink = new HtmlGenericControl("a");
hlink.Attributes.Add("href", subMenuItem["PageFileUrl"].ToString());
hlink.InnerText = subMenuItem["PageTitle"].ToString();
li.Controls.Add(hlink);
li.InnerText = subMenuItem["PageTitle"].ToString();
li.Attributes.Add("href", subMenuItem["PageFileUrl"].ToString());
ulControl.Controls.Add(li);
}
}
private void AddNewUl(int menuId)
{
// Add new <ul id="ddsubmenu00" class= "ddsubmenustyle">
HtmlGenericControl newUl = new HtmlGenericControl("ul");
// Set the attributes of the new ul.
newUl.ID = "ddsubmenu" + menuId;
newUl.Attributes.Add("class", "ddsubmenustyle");
placeHolder1.Controls.Add(newUl);
}
My problem is that submenu doesn't appear!, what is wrong?
any help appreciated.
Thanks for all, finally I figured it out:
Replace this line
li.Attributes.Add("rel",""+ "ddsubmenu" + dr["IdPage"].ToString()+"");
with this one:
hlink.Attributes.Add("rel",""+ "ddsubmenu" + dr["IdPage"].ToString()+"");
Now it works properly :-)
I have a HtmlGenericController and to this I want to add RadioButtons. My radiobuttons come from a RadioButtonList an hence the objects are Listitems. How do I get my genericcontroller to show the radiobuttons?
This is my code
private HtmlGenericControl generateCells(String domainName)
{
HtmlGenericControl container = new HtmlGenericControl("div");
HtmlGenericControl dName = new HtmlGenericControl("span");
dName.InnerHtml = domainName;
RadioButtonList radioList = new RadioButtonList();
radioList.ID = "radio_" + domainName;
radioList.RepeatDirection = RepeatDirection.Horizontal;
ListItem sunriseA = new ListItem();
sunriseA.Value = Price_Types.SUNRISE_ONE.ToString();
//sunriseA.Text = Price_Types.SUNRISE_ONE.ToString();
sunriseA.Text = "";
radioList.Items.Add(sunriseA);
ListItem sunriseB = new ListItem();
sunriseB.Value = Price_Types.SUNRISE_TWO.ToString();
//sunriseB.Text = Price_Types.SUNRISE_TWO.ToString();
sunriseB.Text = "";
radioList.Items.Add(sunriseB);
ListItem landrush = new ListItem();
landrush.Value = Price_Types.LANDRUSH.ToString();
//landrush.Text = Price_Types.LANDRUSH.ToString();
landrush.Text = "";
radioList.Items.Add(landrush);
ListItem general = new ListItem();
general.Value = Price_Types.GENERAL.ToString();
//general.Text = Price_Types.GENERAL.ToString();
general.Text = "";
radioList.Items.Add(general);
container.Controls.Add(dName);
foreach (ListItem item in radioList.Items)
{
HtmlGenericControl span = new HtmlGenericControl("span");
span.InnerHtml = item;//what to put here??
container.Controls.Add(span);
}
return container;
}
var radioButton = new HtmlGenericControl("input");
radioButton.Attributes["type"] = "radio";
radioButton.Attributes["name"] = "groupName";
radioButton.Attributes["value"] = "buttonValue";
That will only render the round radiobutton itself, though. To add a label, you'll have to render for example a span besides it. Or, IIRC, render a label field with the for attribute set to the ID of the radiobutton, so clicking the label will automatically click the button too.
I got some code I need to change. it is build by others, and not very neat...
There is a javascript tabcontrol, containing 4 tabs, which contains gridviews.
All the 4 gridviews are build during the load of the page, but I want them to load, when you activate the tabs (as it is possible to watch the side, while you don't need the specific gridviews to see)
So, my question is: how to call an event (that loads the gridview) from an javascript tab?
how the tabs are generated: (generated code, I know, horrible)
var obj = 0;
var oid = 0;
var otb = 0;
var myTabs = new Array();
var myTabitems = new Array();
var myTabitem = new Array();
var myTabContent = new Array();
var myLists = new Array();
function showTabContent(tab)
{
tb = tab.obj;
id = tab.nr;
if (myTabs[tb].oid != -1)
{
myTabs[tb].myTabContent[myTabs[tb].oid].style.display = 'none';
myTabs[tb].myTabitem[myTabs[tb].oid].className -= " active";
}
myTabs[tb].myTabContent[id].style.display = 'block';
myTabs[tb].myTabitem[id].className += " active";
myTabs[tb].oid = id;
}
function boTabs()
{
var myBlocks = new Array();
myBlocks = document.getElementsByTagName("div");
var stopit = myBlocks.length;
for (var g = 0; g < stopit; g++)
{
if (myBlocks[g].className == "tabs")
{
myTabs.push(myBlocks[g]);
}
}
var stopit2 = myTabs.length;
for (var i = 0; i < stopit2; i++)
{
myTabs[i].myLists = myTabs[i].getElementsByTagName("ul");
if (myTabs[i].myLists[0].className == "tabs")
{
myTabs[i].myTabitems = myTabs[i].myLists[0].getElementsByTagName("li");
}
var stopit3 = myTabs[i].myTabitems.length;
myTabs[i].obj = i;
myTabs[i].myTabitem = new Array();
myTabs[i].myTabContent = new Array();
for (var j = 0; j < stopit3; j++)
{
myTabs[i].myTabitem.push(myTabs[i].myTabitems[j]);
myTabs[i].myTabitem[j].nr = j; myTabs[i].myTabitem[j].obj = i;
myTabs[i].myTabitem[j].onclick = function() { showTabContent(this); };
}
var myTabDivs = myTabs[i].getElementsByTagName("div");
for (var j = 0; j < myTabDivs.length; j++)
{
if (myTabDivs[j].className == "tabcontent")
{
myTabs[i].myTabContent.push(myTabDivs[j]);
}
}
myTabs[i].myTabitem[0].className += " active";
myTabs[i].myTabContent[0].style.display = 'block';
myTabs[i].oid = 0;
myTabDivs = null;
}
myBlocks = null;
}
onload = boTabs;
I would change it entirely to use JQuery Tabs and JQuery AJAX to load the grids
If you want a JS solution, you have to build the gridview table yourself using JS code... If you want the server to do the work, you need an UpdatePanel, and make use of the client _doPostBack method. If you like this approach, the Ajax Control Toolkit tab container can be configured to postback to the server whenever the tab changes, which you can wrap this control with an update panel, and it looks like everything is being done with JS code. Alternatively, you can also use a web service that binds a gridview and returns the HTML too... not tried that yet, but seen it done.
HTH, if you let me know what option you prefer, I could update accordingly.
I now got the tabcontrol made by JQuery and CSS.
at this moment all 4 gridviews are bound at Page_Load, but, as it takes time to run te sproc's behind the gridview, it takes a few seconds. Therefore I want to load them on tabclick... The UpdatePanel seems the best way...
JQuery:
$(document).ready(function()
{
//When page loads...
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function()
{
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
$(activeTab).fadeIn(); //Fade in the active ID content
return false;
});
});