i have done this:
code behind:
var uri = new Uri(Request.Url.ToString());
if ("newsFeed" == Request.Url.Segments[2])
{
L1.Attributes.Add("class", "active");
}
the url of a the page is:
http://localhost:52040/ClientSide/newsFeed/allEr.aspx
so it's suppose to work and enter the if, but it doesnt
what is the problem?
Well Segment[2] would be newsFeed/ not newsFeed. So you can do:
if ("newsFeed" == Request.Url.Segments[2].Trim('/'))
Or use string.TrimEnd
An easier way to debug these problems in future is to use a debug point and watch window. There you can see the value of Request.Url.Segments[2]
See: How to: Use Debugger Variable Windows
By the way Request.Url is already of type Uri, you don't have to create a new instance of Uri with ToString
Try This:
if(Request.Url.Segments[2].Contains("newsFeed"))
Related
I am trying to load a local HTML file into an instance of C# WebBrowser (WinForms).
This is what I am doing:
string url = #"file:///C:MyHtml/hello.html";
myWebbrowser.Url = new Uri(url, UriKind.Absolute);
object test = myWebbrowser.Url; // breakpoint here
The path above is correct; if I copy it and paste into an external browser, the file is immediately opened. But the instance of WebBrowser does not want to react. I set a breakpoint in the last line of the snippet, and what I get there is that myWebbrowser.Url is null (the test variable). The control remains correspondingly empty.
myWebbrowser.AllowNavigation is explicitly set to true. I have also tried all possible versions of slashes and backslashes; the result is always the same. The version of the webbrowser seems to be 11 (myWebbrowser.Version = "{11.0.18362.1139}"). I am working in Windows 10, VS 2019.
What can be wrong in this setup?
The path above is correct; if I copy it and paste into an external browser, the file is immediately opened. But the instance of WebBrowser does not want to react. I set a breakpoint in the last line of the snippet, and what I get there is that myWebbrowser.Url is null.
I was able to replicate this exact issue, it's because the property of the Url doesn't get actually set until the document has actually finished loading.
To resolve this issue, you must handle the DocumentCompleted event. You can do so for example:
string url = #"file:///C:MyHtml/hello.html";
myWebbrowser.DocumentCompleted += MyWebbrowser_DocumentCompleted;
myWebbrowser.Url = new Uri(url, UriKind.Absolute);
Create a new routine to handle the DocumentCompleted event:
private void MyWebbrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string test = myWebbrowser.Url.ToString();
}
You can also get the Url from the WebBrowserDocumentCompletedEventArgs:
string testUrl = e.Url.ToString();
I am not sure exactly why when setting the Url and then checking it, it is null, I haven't found anything to explain why. My only guess is that it may be an invalid Url and or path, if navigation succeeds then that property is set.
Edit: upon looking at the source for WebBrowserDocumentCompleted, it does seem the Url property is only set in the DocumentCompleted, you can see more there.
Please note: you must register the DocumentCompleted event first before setting the Url property as when you do, it will navigate first and you will not receive the DocumentCompleted event.
I was able to find out why it did not want to function, at least I hope so. The "hello.html" file contained calls to jquery and THREE.js, while WebBroser seems not to support the latter. Therefore I did not see any content in the control. After I threw out THREE.js and inserted most simple HTML code, it worked just OK! Now I am busy trying to bring WebBrowser to support THREE.js (there exists a skeptical opinion about this, though).
If I would use default Visualstudio webBrowser control (IE) i would just write something like this:
textBox3.Text = webBrowser1.Document.GetElementById("mydivid").InnerHtml;
But I'm stubborn to use GeckoFX (Mozilla) and ofcourse it doesn't work. What I found is information that I need to adress "HTMLDocument" not "Document" to pull my desired value. But no example that fit my needs.
How to get this innerHTML of this particular element?
--- Response to #Timothy Groote ---
I've read the other topic - there's no example of code with GetElementById, it's like wide definition, but I need specific one - I cannot read this codeand get clear information what method or property shoueld I use in my app.
I can add that my element is and will be always HTML element, so i don't need to verify that every time with "if".
When I use "the other code" in my app result (instead of innerHTML) was only:
<head></head><body></body>
In "the other code" has also a mistake:
var geckoDomElement = WebBrowser1.Document.DocumentElement;
There's no webBrowser1 control! Only geckoWebBrowser1, neither works!
--- Edit2 ---
I was also thinking about something like that:
textBox3.text = (GeckoHtmlElement)geckoWebBrowser1.Document.GetElementById("mydivid")....
but there's also no "innerHtml"
in
(GeckoHtmlElement)geckoWebBrowser1.Document.DocumentElement
there's no "getElementById".
If you using Geckofx 45 then its very simple , just try like this....
GeckoHtmlElement testelement = null;
testelement = (GeckoHtmlElement)webBrowser1.Document.GetElementById("test");
string text=testelement .InnerHtml;
if you don't know how to use Geckofx 45 then try this simple tutorial ..
How to use or embed Geckofx 45 Webbrowser control into Visual Studio into WinForms Applications
ANSWER
Of course defining variable:
GeckoHtmlElement machcode;
Finding element:
machcode = geckoWebBrowser1.Document.GetHtmlElementById("machinecode");
And finally reading the content:
textBox3.Text = machcode.InnerHtml;
And one important thing was to place code in DocumentComplete event!
Every solutions provided in web were wrong - some point into nonexisting webBrowser1 control, some forget about definition, some throw null exception, some use wrong GetElement instead of GetHtmlElement.
I meet a strange behavior. I dont understand my mistake.
I want to read a value from an HTML page. I can obtain it with :
JavaScript
function getSize() {
return (size.toString());
}
getSize();
Output (in Chrome console)
120
C#
// wBrowser is not null and the page is loaded
String o = wBrowser.InvokeScript("eval", "getSize();").ToString();
Debug.WriteLine(o);
Here o is empty.
I read the msdn doc and try some research without success. What am I missing?
Thank you.
Well ...
Just found my error.
My solution is working. Lukasz's solutions works too.
I just forgot to swith the run mode from Release to Debug ...
So working solutions are :
String o1 = wBrowser.InvokeScript("eval", "getSize();").ToString();
// Lukasz's 1st solution
String o2 = wBrowser.InvokeScript("getSize").ToString();
// Lukasz's 2nd solution
String o3 = wBrowser.InvokeScript("eval", "size.toString();").ToString();
Both print the right size.
Maybe it could be helpful for someone else ...
In the code behind, I have the following code:
if (!Page.ClientScript.IsClientScriptBlockRegistered("Script1"))
Page.RegisterClientScriptBlock("Script1", "<script type=\"text/javascript\">alert('test');</script>");
This code seems to work fine, but when I try to debug it Page.ClientScript.IsClientScriptBlockRegistered("Script1") always returns false. I even opened the quick watch window and was able to find the script under _clientScriptBlock.
Any idea why this is?
Use the GetType() method parameter to ensure you're isolating the correct script block, like this:
if (!Page.ClientScript.IsClientScriptBlockRegistered(GetType(), "Script1"))
{
Page.RegisterClientScriptBlock(GetType(), "Script1",
"<script type=\"text/javascript\">alert('test');</script>");
}
I have the following line of code to open a web page modal dialog in C# (Silverlight):
var so = (ScriptObject)HtmlPage.Window.Invoke(
"showModalDialog",
modalWindowUrl,
dialogArgs,
"dialogWidth:600px;dialogHeight:600px;");
Now, code similar to the following is being called on the page I am displaying, and I need to make sure it gets the values I'm trying to pass in (this is a MSCRM web page I don't have control over):
dialogArgs.items <-- will be an array I pass in
dialogArgs.items[i].getAttribute("oid") <-- will return something
dialogArgs.items[i].getAttribute("otype") <-- will return something
dialogArgs.items[i].values <-- will return something
What I have tried to send in (from my C# code) is this:
dialogArgs = #"{items:[{oid:" + id + ",otype:" + type + "}]}";
which will result in a JSON string... but I'm guessing this just ends up as a string within the JavaScript and not a JSON object.
Any ideas how I get this to work?
A few side notes:
I can't get IE to debug the modal dialog that results from this call. I can get the debugging tools displaying, but it won't attach to the page because it cannot refresh it.
I don't have control over this modal dialog. It's a page that is displayed using MS Dynamics CRM. For that reason I cannot mess with the JavaScript or anything to test stuff.
Looks like I won the tumbleweed award for this one! Can't believe how uncommon this scenario seems to be. The solution ended up being quite simple, but not very documented so took me a while to track down. Thought I would share here.
Firstly, a quick search across the internet reveals that we can set this up using the following:
var dialogArgs = HtmlPage.Window.CreateInstance("Object");
Which gives you a ScriptObject back. For properties:
dialogArgs.SetProperty("items", items);
Some code for setting up an array and an item should look something like this (I have just created a new GUID for the purpose of this example):
var item = HtmlPage.Window.CreateInstance("Object");
item.SetProperty("oid", Guid.NewGuid());
item.SetProperty("otype", "account");
var items = HtmlPage.Window.CreateInstance("Object");
items.SetProperty(0, item);
And finally, just pass that object straight into your dialog window like this:
var so = (ScriptObject)HtmlPage.Window.Invoke("showModalDialog", lookUpWindow, dialogArgs, "dialogWidth:600px;dialogHeight:600px;");