Event Triggering in asp.net using javascript - c#

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()\">";

Related

C# ASP.NET Wrap dynamically added CheckBox Control with a dynamically added Label Control

I want the checkbox wrapped in a label so that if they click the Label it will check the CheckBox. I also need the descArea to diplay once the hover over the Label. These must also be dynamic and created in the code behind. I have 2 ways so far, both with their own issue...
This option doesn't allow the JS functions to be called for the descArea to slide down
caPanel.Controls.Add(new LiteralControl(
"<div class='col-md-6'>"+
"<div id='AButton" + t.Rows[i]["ID"].ToString() + "' class='caLabel' onmouseenter='slideDescriptionDown(this.id);' onmouseleave='slideDescriptionDown(this.id);' >" +
"<label>"));
CheckBox aCheckBox = new CheckBox();
aCheckBox.ID = "checkbox" + t.Rows[i]["ID"].ToString();
caPanel.Controls.Add(aCheckBox);
ca.Controls.Add(new LiteralControl(" " + t.Rows[i]["NAME"].ToString() +
"</label>"+
"</div>"+
"<div class='alert alert-info aDescription' id='descArea" + t.Rows[i]["ID"].ToString() + "'>" +
"<span id='desc" + t.Rows[i]["CA_ID"].ToString() + "'>" + t.Rows[i]["Description"].ToString() + "</span>" +
"</div>"+
"</div>"));
This one doesn't allow the CheckBox to be checked when clicking the Label. I inspected the code and the Label is showing up as a span
caPanel.Controls.Add(new LiteralControl("<div class='row caSectionRow'>" +
"<div class='col-md-6'>"));
Label aName = new Label();
aName.ID = "AButton" + t.Rows[i]["ID"].ToString();
aName.Attributes.Add("class", "caLabel");
aName.Attributes.Add("onmouseenter", "slideDescriptionDown(this.id);");
aName.Attributes.Add("onmouseleave", "slideDescriptionDown(this.id);");
aName.Text = " " + t.Rows[i]["NAME"].ToString();
CheckBox aCheckBox = new CheckBox();
aCheckBox.ID = "checkbox" + t.Rows[i]["ID"].ToString();
caPanel.Controls.Add(aCheckBox);
caPanel.Controls.Add(aName);
caPanel.Controls.Add(new LiteralControl(
"<div class='alert alert-info aDescription' id='descArea" + t.Rows[i]["ID"].ToString() + "'>" +
"<span id='desc" + t.Rows[i]["ID"].ToString() + "'>" + t.Rows[i]["Description"].ToString() + "</span>" +
"</div>" +
"</div>"));
I found the error lied within the JS function itself when trying to display the descArea for the first option I proposed. I didn't have the ID for the descArea correct. It was prepended with the MainContent_aContent_ to fulfill UniqueID property all controls must have in ASP.NET.
When is the ctl00 prefix added?
https://msdn.microsoft.com/en-us/library/1d04y8ss(v=vs.100).ASPX

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());
}

WP7 Webbrowser InvokeScript errors

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.

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();
}

Categories

Resources