WP7 Webbrowser InvokeScript errors - c#

WebBrowser control's invoke script is always giving me errors. This html script is validated from http://validator.w3.org. I wrote the code such that on clicking "button1" webBrowser1 invokes the function "setCredentials". I am not sure why this is giving an error like
"An unknown error has occurred. Error: 80020006."
public TestInvokeScript()
{
InitializeComponent();
LoadHtml();
webBrowser1.IsScriptEnabled = true;
webBrowser1.NavigateToString(_html);
button1.Content = "Set Credentials";
}
private void LoadHtml()
{
_html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">" +
"<head>" + "<meta name=\"generator\" content=\"HTML Tidy for Linux (vers 6 November 2007), see www.w3.org\" />" +
"<script type=\"text/javascript\">" +
"//<![CDATA[" +
" function setCredentials()" +
" {" +
" document.getElementById(\"email\").value = \"test#gmail.com\";" +
" }" +
"//]]>" +
"</script>" +
"<title></title>" +
"</head>" +
"<body>" +
"<form action=\"https://cloudmagic.com/k/login/send\" method=\"post\">" +
"<input id=\"email\" type=\"text\" value=\"\" /> " +
"<input id=\"password\" type=\"password\" />" +
" <button type=\"submit\" id=\"login_send\">Login</button>" +
" </form>" +
"</body>" +
"</html>";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var obj = webBrowser1.InvokeScript("setCredentials");
}
What is the mistake I am doing.?

A couple of possibilities:
1. Make sure you call it after PageLoaded or NavigateComplete fires.
2. Try this one:
Dispatcher.BeginInvoke(() =>
{
var result = webBrowser.InvokeScript("javascrpitMethod", param1, param2);
});

I was passing the html in string form. Unknowingly, I used a double slash(//)
and that commented the rest of the string since there are no newLine characters. It took me almost a day to figure this out. Remove the double slashes and CData tag.

Related

Append text to Top of Textbox

While populating a Textbox using a List. The Display method is as follows.
private void Display()
{
StringBuilder sb = new StringBuilder();
foreach (Player dude in _FootballRoster)
{
if (btnUSA.Checked == true)
{
sb.AppendLine("\r\nName: " + dude.getName() + " \r\n Team: " + dude.getTeam() + "\r\n Birthday: " + dude.getBirthday() + "\r\n Height(in):" + dude.getHeight() + "\r\n Weight(lbs): " + dude.getWeight() + "\r\n Salary(USD): " + dude.getSalary());
}
if (btnUSA.Checked == false)
{
sb.AppendLine("\r\nName: " + dude.getName() + " \r\n Team: " + dude.getTeam() + "\r\n Birthday: " + dude.getBirthday() + "\r\n Height(meters):" + (dude.getHeight()) / 39.3701 + "\r\n Weight(kg): " + (dude.getWeight()) / 2.20462 + "\r\n Salary(CD): " + (dude.getSalary()) / 1.31);
}
}
txtRosterLog.Text = sb.ToString();
}
While trying to implement a Sort method when you click btnName, I want "SORT BY: NAME" to appear at the top of the textbox but my current code puts it at the bottom of all the players.
Current Name Sort Code:
private void btnName_Click(object sender, EventArgs e)
{
_FootballRoster = _FootballRoster.OrderBy(dude => dude.Name).ToList();
Display();
txtRosterLog.AppendText("SORT BY: NAME ");
}
Any ideas? I've tried using txtRosterLog.Text.Insert(0, "SORT BY NAME)" but that didn't work either.
txtRosterLog.Text = "SORT BY: NAME \r\n" + txtRosterLog.Text;
txtRosterLog.Text.Insert(0, "SORT BY NAME)" would also work if you assign it back:
txtRosterLog.Text = txtRosterLog.Text.Insert(0, "SORT BY NAME");
I would go with String.Format as it is quite flexible and easly readable if you would like to make your string more fancy in future.
String s = String.Format("SORT BY: NAME \r\n {0}", txtRosterLog.Text);

How to know click (Ok or Cancel) event from print pop-up of web browser

In my application I am printing some data through a pop-up window. On loading the pop-up, the user will be prompted for to either print or cancel. I want to know which button is clicked (print or cancel) so that I can take some action based on the button clicked. The code and screenshot are given below.
public void Print(int Id)
{
StringBuilder sb = BadgeHelper.preparePrintableString(Id);
string printWindowFunc = "function printWindow() {\n" +
"var z = window.print();\n alert('Return: ' + z);\n" +
"checkDocumentState();\n" +
"}";
string checkDocumentStateFunc = "function checkDocumentState() {\n" +
"if (document.readyState == 'complete') {\n" +
"window.close();\n" +
"}\n" +
"else {\n alert('Else');\n" +
"setTimeout('checkDocumentState()', 2000);\n" +
"}\n"+
"}\n";
string startingHTMLCode = "<html>\n" +
"<head>\n" +
"<script type='text/javascript'>\n " +
checkDocumentStateFunc +
"\n" +
printWindowFunc +
"</script>\n " +
"</head>\n" +
"<body onload='printWindow();'>\n";
sb.Insert(0, startingHTMLCode);
sb.Append("\n</body>\n</html>");
Response.Write(sb.ToString());
}

Getting values of all controls

i am trying to get the values of my controls like this:
function ConfirmWithUser()
{
var nodeText = '';
$('.mytreeview input[#type=checkbox]:checked').each(function() {
nodeText += $(this).next('a').text() + '\r';
});
var confirmationMessage;
confirmationMessage = "Please review the data before submitting:" + "\r"
+ "Sample Received Date: " + document.getElementById(received_dateTextbox).Value + "\r"
+ "Site of Ocurrence: " + document.getElementById(site_of_occurrenceTextBox).Value + "\r"
+ "Occurrence Date: " + document.getElementById(occurrence_dateTextBox).Value + "\r"
+ "Report Date: " + document.getElementById(report_byTextBox).Value + "\r"
+ "Specimen ID: " + document.getElementById(spec_idTextBox).Value + "\r"
+ "Batch ID: " + document.getElementById(batch_idTextBox).Value + "\r\n"
+ "Report Initiated By: " + document.getElementById(report_byTextBox).Value + "\r\n"
+ "Problem Identified By: " + $("input[#name=RadioButtonList1]:checked").val() + "\r\n"
+ "Problem List: " + nodeText;
HiddenFieldConfirmation.Value = confirmationMessage;
if (confirm(document.getElementById('HiddenFieldConfirmation').value) == true)
{ return true; }
else
{ return false; }
}
and the CONFIRM box is not firing at all! i do not get any pop up.
i tried to debug using firefox, and as soon as it go to this line:
confirmationMessage = "Please review the data before submitting:" + "\r"
+ "Sample Received Date: " + document.getElementById(re.......
it escapes out of the function
what am i doing wrong? how can i get the values of all the controls?
You need to use a lowercase "v" for value and quote your element ids. Eg:
document.getElementById("received_dateTextbox").value
Since it appears you are already using jQuery, you can make your code a little more concise. So document.getElementById("received_dateTextbox").value becomes:
$("#received_dateTextbox").val()
There is no variable named spec_idTextBox.
You probably want to pass a string literal.
Once you fix that, you need to use .value in lowercase
If you are using dynamic client ids, you have to render the ids inline or pass them to your function:
confirmationMessage = "Please review the data before submitting:" + "\r"
+ "Sample Received Date: " + document.getElementById('<% = received_dateTextbox.ClientID %>').value + "\r"
+ "Site of Ocurrence: " + document.getElementById('<% = site_of_occurrenceTextBox.ClientID %>').value + "\r"
+ "Occurrence Date: " + document.getElementById('<% = occurrence_dateTextBox.ClientID %>').value + "\r"
+ "Report Date: " + document.getElementById('<% = report_byTextBox.ClientID %>').value + "\r"
+ "Specimen ID: " + document.getElementById('<% = spec_idTextBox.ClientID %>').value + "\r"
+ "Batch ID: " + document.getElementById('<% = batch_idTextBox.ClientID %>').value + "\r\n"
+ "Report Initiated By: " + document.getElementById('<% = report_byTextBox.ClientID %>').value + "\r\n"
+ "Problem Identified By: " + $("input[#name=RadioButtonList1]:checked").val() + "\r\n"
+ "Problem List: " + nodeText;

window.focus() of JavaScript not working on IE9

This is my code
protected void LoginButton_Click(object sender, EventArgs e)
{
if (DAL.DAOKullanici.Login(KullaniciTextBox.Text,SifreTextBox.Text))
{
VeriyazPROTicari.Sessionlar.Variables.loginkontrol = true;
Session["kullaniciAdi"] = KullaniciTextBox.Text;
Session["kullaniciId"] = DAL.DAOKullanici.GetEntity(DAL.DAOKullanici.KullaniciAdiIleKullaniciIdCek(KullaniciTextBox.Text)).ID;
bool main_window_open = false;
if (!main_window_open)
{
Page.RegisterClientScriptBlock("Main_Window", "<script>" +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} "
+ "</script>");
main_window_open = true;
}
HataLabel.Text = "";
}
else
{
HataLabel.Text="Hatalı Giriş";
}
}
I have no problem with it except the JavaScript part.
What I am trying to is after LoginButton is clicked opening dashboard.aspx and setting focus on it.And this code opens dashboard.aspx and sets focus in Google Chrome and Mozilla Firefox 4.However,when I try it on IE9 dashboard.aspx is opened but focus does not work and dashboard.aspx remains under the login page.
How can I set focus on a new window on IE9?
I have had a similar problem to this and it seemed to happen because in IE9 (and any IE) that the focus() method is run before the window is rendered.
To get round this there are two ways that I can see that will fix this:
Set a timer to load the focus after a small amount of time.
Defer the JavaScript from being read until the window is fully rendered.
The timer method to me is not my preferred choice as in my personal opinion it is messy and if the page takes longer to load than the timer you are stuck with the same problem. To implement the timer you could use something like:
Page.RegisterClientScriptBlock("Main_Window", "<script>" +
"setTimeout(function() { " +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} " +
"}, 5);" +
"</script>");
I have set a delay of 5 seconds, which may be overkill.
The defer method is my preferred choice as I feel it is cleaner and simpler, but it may or may not work:
Page.RegisterClientScriptBlock("Main_Window", "<script type="text/javascript" defer="defer">" +
"var newwindow; " +
"newwindow = window.open('" + "/dashboard/dashboard.aspx" + "', 'main_app_window', ' toolbar=0,location=0,directories=0,status=1,menubar=0,left=1,top=1,scrollbars=" + "1" + ",resizable=1,width=" + "1280" + ",height=" + "800" + "'); " +
"if (window.focus) " +
"{newwindow.focus();} "
+ "</script>");
After what seemed like an eternity, my colleague figured out a way to make it work for us.
There are other references to the issue itself, http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/a250c431-9f09-441c-9b78-af067233ed78 http://support.microsoft.com/kb/979954
That said, we simply put the window.focus() in the body tag of the new (pop-up) page.
<body onload="FocusWindow()">
Defined FocusWindow() in a separate .js file as follows,
function FocusWindow() {
window.focus();
}

Event Triggering in asp.net using javascript

I've created two buttons using javascript; yes and no. I need to get the event triggered when the button is clicked.
i.e... onclick=getvalue().
public void SetYesButton(string msg_button_class) {
this.msgbox_Yes_button = "<input type=\"button\" value=\"Yes\" class=\"" + msg_button_class + "\" onClick=\"getvalue()\";>";
}
public void SetNoButton(string msg_button_class) {
this.msgbox_No_button = "<input type=\"button\" value=\"No\" class=\"" + msg_button_class + "\" onClick=\"document.getElementById('pagedimmer').style.visibility = 'hidden'; document.getElementById('msgbox').style.visibility = 'hidden';\">";
}
Didn't understand your question, but a suggestion.
Remove the penultimate semi-colon:
"<input type=\"button\" value=\"Yes\" class=\"" + msg_button_class + "\" onClick=\"getvalue()\">";

Categories

Resources