How to access span with needed innerhtml? - c#

Say my WebBrowser1 downloaded a page that has a following line:
<span customattr="hamolulu">hamolulu</span>
It is inside of a td tag, inside of big table, inside of iFrame, inside of div etc.
How to I click this thing using c# ?
I need to do something following:
int i = 0;
for (i = 0; i <= 500; i++)
{
if (webBrowser1.Document.GetElementsByTagName("span")[i].GetAttribute("customattr") == "hamolulu")
{
webBrowser1.Document.GetElementsByTagName("span")[i].InvokeMember("click");
break;
}//end if
}// end for
but for some reason it does not work this way, so I'm thinking if it's possible to check the innerHTML of the span, or innerText?
I tried both:
webBrowser1.Document.GetElementsByTagName("span").InnerHTML == "hamolulu"
webBrowser1.Document.GetElementsByTagName("span").InnerText == "hamolulu"
And I failed both times.
Update:
I just noticed that the line is actually like this:
<span customattr="hamolulu"><a>hamolulu</a></span>
So I wrote a simple function:
int i = 0;
for (i = 0; i <= webBrowser1.Document.GetElementsByTagName("a").Count - 1; i++)
{
log(i.ToString()+ " : " +webBrowser1.Document.GetElementsByTagName("a")[i].InnerHtml);
} //log(string) is a custom function that saves all strings to a file log.txt
And what I've seen is that this link (and span) does not show up in my log.
In other words, getElementsByTagName("span") and getElementsByTagName("a") doesn't see the item. My guess is that it is because of iFrame. Do you have any thoughts about this?

another solution using no js (because you don't own the "page")
since it is inside an iframe then you should search within that iframe
HtmlElementCollection iframes = WebBrowser1.Document.GetElementsByTagName("iframe");
HtmlElement iframe = iframes(0 /* iframe index */); //
HtmlElementCollection spans = iframe.Document.GetElementsByTagName("span");
for (i = 0; i < spans.Count; i++) {
HtmlElement span = spans(i);
if (span.GetAttribute("customAttr") == "customAttrValue") {
string onclick = span.Children(0).GetAttribute("onclick"); //span.Children(0) should return the <a>
WebBrowser1.Document.InvokeScript(onclick);
}
}

Unless I am missing something...
<span id="hamolulu">hamolulu</span>
Then when you want to change it...
document.getElementById('hamolulu').innerHTML="<h1>Test!</h1>";

If you set up your span as an HTML server control:
<span runat="server" id="myspan" customattribute="customvalue">hello world</span>
Then you can register an event handler on page load:
protected void Page_Load(object sender, EventArgs e)
{
myspan.Attributes["onclick"] = "this.innerText='hamolulu'";
}
Another way to do it is using Page Methods which would call a page C# method using AJAX.

you can make it simpler by using JavaScript and invoking it from c# whenever you need to.
WebBrowser1 .Document .InvokeScript ("functionName")
javascript:
function functionName(){
var spans = document.getElementsByTagName('SPAN');
for (i = 0; i < spans.length; i++)
{
var span = spans[i];
if (span.getAttribute("customattr") == "hamolulu")
{
eval(span.getAttribute('onclick')); // .. be careful "span" has no "click()" method. you should use the onlick attribute if available
break;
}//end if
}// end for
}

Related

How to use Threading.Timer for a loop on a public non-static method on ASP.NET C#?

I have the following method:
//BOLO stands for Be On The Lookout
public void LoadBolos()
{
List<string> bolos;
//If not loaded before
if (ViewState["bolos"] == null)
{
List<string> bolos = GetBolos();
ViewState["bolos"] = bolos;
ViewState["index"] = 0;
}
else
{
bolos = (List<string>)ViewState["bolos"];
}
int index = ViewState["index"] != null ? (int)ViewState["index"] : 0;
if (bolos.Count > 0)
{
imgBolo.ImageUrl = bolos[index];
index++;
index = index >= bolos.Count ? 0 : index;
}
ViewState["index"] = index;
}
I need to loop this method every 10 seconds, so I can refresh an image on the aspx page. However, I've noticed that some Thread.Timer functions don't allow non-static functions to be used.
If I convert this method to static, I can no longer access private variables that load on page load, nor can I use the Viewstate nor access the image.
So I was wondering which would be the most accurate way to refresh an image on the page every 10 seconds.
You can refresh the page using the html content using
Untitled Page
<meta http-equiv="refresh" content="20" />

how can i get value of all controls created in runtime in C# asp.net

// this function create mutiple htmlcontrol with id fileid1,fileid2, fileid3 etc "content_data" is a html div id.
protected void enter_rec_btn_Click(object sender, EventArgs e)
{
String control_data="";
for (Int64 x = 1; x <= 4; x++)
{
control_data = control_data + "<input type='text' id='fileid" + x + "' runat='server' /> ";
}
content_data.InnerHtml = control_data;
}
//now in another btn click just access the controls
for(Int64 i=1;i<=4;i++)
{
HtmlInputText text1,
text1 = (System.Web.UI.HtmlControls.HtmlInputText)Page.FindControl("fileid" + i);
Response.write(text1.value);
}
but it can't find the control.i already include using System.Web.UI.HtmlControls.
Help me..
thanks in advance
You are rendering a string in the Div and not actually adding the control to the web-page so find control will not work you may have to read the html of the Div you are writing to to get the controls.
Alternatively add an actual control to the page to be able to use findcontrol.
i.e MyPannel.controls.add(new ctrl.....
Good Example found here :)
http://learning2code.net/Learn/2009/8/12/Adding-Controls-to-an-ASPNET-form-Dynamically.aspx

Add asp imagebutton inside javascript

I need to add asp image button inside a javascript function. Actually I need to create image buttons inside a html table dynamically. Here is my code. I need to add the image button where I have mentioned as "--- Here I need to add the image button ---"
function tableSeats() {
document.write("<table align=center>");
for (var i = 1; i <= 5; i++) {
document.write("<tr height=30px>");
for (var r = 1; r <= 10; r++) {
document.write("<td width=30px>");
document.write(--- Here I need to add the image button ---);
document.write("</td>");
}
document.write("</tr>");
}
document.write("</table>");
}
If this is not possible, please tell me a way to do it using c#.net
Thanks in advance...
I believe by using javaScript you are not able to create any kind of server controls like image button and all. But you can do some kind of things like <img /> And using jQuery you can invoke the client click and using ajax you can easily communicate with server.
function tableSeats() {
document.write("<table align=center>");
for (var i = 1; i <= 5; i++) {
document.write("<tr height=30px>");
for (var r = 1; r <= 10; r++) {
document.write("<td width=30px>");
document.write("<img src='Your picture path' alt='' />");
document.write("</td>");
}
document.write("</tr>");
}
document.write("</table>");
}
Updated the code
Check this for jQuery click
http://api.jquery.com/click/

programmatically adding buttons and OnClientClick but still having post back issue

I am trying to hide some divs using Javascript but i think the post back keeps reloading the page.
To make things more complicated my buttons are added programmatically by my code behind.
foreach (string line in thefilters)
{
Button newButton = new Button();
newButton.ID = Convert.ToString(line);
newButton.Text = Convert.ToString(line);
newButton.CssClass = "tblbutton";
//newButton.Attributes.Add("onclick", "hide_div("+newButton.ID+")");
newButton.OnClientClick = "return hide_div('" + newButton.ID + "')";
pnl_left.Controls.Add(newButton);
}
My javascript is located in the header as follows.
<script type="text/javascript">
function hide_div(filter) {
var pnl_right = document.getElementById("pnl_right");
var listofelements = pnl_right.getElementsById("div");
for (var i = 0; i < listofelements.length; i++) {
if (listofelements[i].id.indexOf(filter) == 0) {
document.getElementById(listofelements[i].id).style.display = 'inline';
}
else {
document.getElementById(listofelements[i].id).style.display = 'none';
}
}
return false;
}
I may have issues in the javascript for what i want to achieve but i am confident that if i can stop the postback then i can solve the javascript myself..
Thanks for any suggestions in advance.
You have not showed in which event you are adding controls. But I am assuming from your problem that you are doing this in Page_Load. If yes, try and move in OnInit event.
Second, in Page_Load you need to check
if(!IsPostBack)
{
//your code for adding controls
}
Hope that helps.

GetElementById without id and name HOW?

I need to click the button "Add" in the post new wordpress form, this button is to add tags to the post , the trouble is that button don't have the value and id propertie. Is just like that
the html for the button
input type="button" class="button tagadd" value="Add" tabindex="3"
my tries
webBrowser1.Document.GetElementById("button tagadd").InvokeMember("click");
webBrowser1.Document.GetElementById("Add").InvokeMember("click");
"GetElementById without id"
:-)
Unless you can change the markup for the button
What you need now is to traverse the entire DOM and look for a button in a known place. I'd suggest adding jquery if not already exist to be able for easier dom manipulation/search.
If you add jquery you could do something like $(".tagadd").click()
You could try doing
webBrowser1.document.getElementsByClassName("tagadd")
EDIT: Here is a script to create the getElementsByClassName function if it's not available http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
There is also this http://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument.getelementsbytagname.aspx but I've never used it.
Add an ID. Even if you're dynamically generating the buttons this should be trivial.
If you're using jQuery,
$('.tagadd')
will return a collection of everything with the tagadd class applied. You can further filter this by the other classes (button, etc)
Use this:
onload=function(){
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className)
{
var hasClassName = new RegExp("(?:^|\s)" + className + "(?:$|\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass
&& elementClass.indexOf(className) != -1
&& hasClassName.test(elementClass))
results.push(element);
}
return results;
}
}
}
and another
Some browsers provide the method getElementsByClassName() which lets you select by class without using jQuery (which is a bit heavy if this is all you need). I haven't tested this so I'm not sure how widely it's supported.
Did I mention that you should give everything an ID?
use TagName isteed for example
var elems = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement elem in elems)
{
if (elem.GetAttribute("class") == "button tagadd")
{
elem.InvokeMember("click");
}
}

Categories

Resources