New Windows focus issue JavaScript and C# - javascript

I have below code which I am using to open a new window up on buttonclick. I am executing below code in button click event : New windows opens up but it does not focus, it just goes behind the main page.... please how can I fix this issue....
protected void btnView_Click(object sender, EventArgs e)
{
int activeTab = pgSearchFrm.ActiveTabIndex;
string sel = this.ddlFields.SelectedValue;
string url = "\\ATPrintView.aspx?SearchID=" + Convert.ToString(activeTab) + "&Col=" + sel + "&Val=" + txtFields.Text;
ScriptManager.RegisterStartupScript(this, this.GetType(), "Open", "window.open('" + url + "','_blank','height=500,width=800,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no').focus();", true);
}

Related

window.onload = function() is not showing complete msg

windows.onload() popup is not showing entire message in chrome. In explorer popup is showing full message. How to resolve this issue in Chrome?
tried in explorer popup is showing full msg . how to resole this issue in chrome
string script = "window.onload = function() { alert('" + message + "'); };";
ClientScript.RegisterStartupScript(this.GetType(), "alert", script, true);
protected void Page_load(object sender,EventArgs e)
{
string message="hii";
Response.Write("<script type='text/javascript'>alert('"+message+"');</script>");
}
Try this one...

Trying to interact with a Website "synchronously" using javascript in Android Webview

I'm trying to interact with a Website "synchronously" (javascript events when submit buttons are clicked) in order to get a textField value. This textField doesn't exist in the first place. It is generated when the user clicks on a submit button from a HTML form (2 buttons actually : first one : Id = "ColumnLeft_ADD_BTN_ID", second one : Id = "ColumnLeft_GET_BTN_ID").
When I simulate a click with Javascript, the textField doesn't appear. I get this error "Uncaught TypeError: Cannot read property 'value' of null" because the textField doesn't exist.
I've tried HTMLUnit but I couldn't make it work with Android. I turned to Android WebView.
Here's my code :
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(url);
final String js = "(function() { " +
"document.getElementById('ColumnLeft_RES_TB_ID').value='" + group + "';" +
"document.getElementById('ColumnLeft_ADD_BTN_ID').click();" +
"document.getElementById('ColumnLeft_GET_BTN_ID').click();" +
"return document.getElementById('ColumnLeft_FEED_URL_TB_ID').value; " +
"})();";
mWebView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
if(Build.VERSION.SDK_INT >= 19){
view.evaluateJavascript(js, new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
JsonReader reader = new JsonReader(new StringReader(s));
// Must set lenient to parse single values
reader.setLenient(true);
try {
if(reader.peek() != JsonToken.NULL) {
if(reader.peek() == JsonToken.STRING) {
String msg = reader.nextString();
Log.d("DEBUG", "msg : " + msg);
}
}
} catch (IOException e) {
Log.e("TAG", "MainActivity: IOException", e);
} finally {
try {
reader.close();
} catch (IOException e) {
// NOOP
}
}
}
});
}
}
});

Call Javascript function multiple times from code behind

I have the following button event handler in C#. I would like to call the same javascript function with different paramater.
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
string script = String.Format("test({0},{1})", i.ToString(), i.ToString() + 1);
this.Page.ClientScript.RegisterStartupScript(this.GetType(),
"testFunction", script, true);
}
}
define your js method like:
<script type="text/javascript">
function UpdateTime(time) {
document.getElementById("<%=lblTime.ClientID %>").innerHTML = time;
}
and then in your code behind, use:
protected void UpdateTime(object sender, EventArgs e)
{
string time = DateTime.Now.ToString("hh:mm:ss tt");
string script = "window.onload = function() { UpdateTime('" + time + "'); };";
ClientScript.RegisterStartupScript(this.GetType(), "UpdateTime", script, true);
}
Source and demo here

Javascript confirm dialog coming up twice; Jumping back after clicking Element

Beginner here, so please be gentle.
I'm trying to ask the User for confirmation. When he accepts, my javascript invokes a Button to run some code behind. The code behind will run a JavaScript function which alerts, just so I can see if it all works. What happens is, that after my first confirm() dialog, a second dialog appears. After confirming again, the code runs and my second JavaScript function confirms by alerting. But after I press okay on that, the whole thing jumps back to my first function and I get into a loop!
ASPX:
<script type = "text/javascript">
function ConfirmComputerOverwrite(ComputerName) {
if (confirm("Overwrite present computer " + ComputerName + " ?"))
{
}
else
{
}
document.getElementById("Test").click()
}
function allworkedout()
{
alert("asdf");
}
Code behind:
public string ComputerName = "";
protected void Page_Load(object sender, EventArgs e)
{
string ComputerName = "Computer1";
this.ComputerName = ComputerName;
StringBuilder sb = new StringBuilder();
sb.Append("ConfirmComputerOverwrite('" + ComputerName + "');");
ScriptManager.RegisterStartupScript(this, GetType(), "ConfirmComputerOverwrite", sb.ToString(), true);
}
protected void Test_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("allworkedout();");
ScriptManager.RegisterStartupScript(this, GetType(), "allworkedoutID", sb.ToString(), true);
}
Thanks in advance !
Unless you are using ajax, the whole page is reloaded after click, so your javascript is executed again, prompting for confirmation, etc...

server side alert message that shows unwanted when get back and reload the page

I have a submit button and below is the code in the onClick event :
protected void btnSubmit_Click(object sender, EventArgs e)
{
...
ScriptManager.RegisterClientScriptBlock(this, GetType(), "alertMessage", "alert('Submitted')", true);
}
This code works.
But the problem is when user go to next page by this:
Response.Redirect("page2.aspx");
and when click backspace to get back to page1 and,
before the reload,
the following message box appears!!
this problem happened again when we refresh(F5) the page1 after submiting
how will I solve this?
I tried:
1. if(isPostback)// before the alert
2. string message = "Submitted";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
In such case, you can implement a special code block to detect browser refresh as
private bool refreshState;
private bool isRefresh;
protected override void LoadViewState(object savedState)
{
object[] AllStates = (object[])savedState;
base.LoadViewState(AllStates[0]);
refreshState = bool.Parse(AllStates[1].ToString());
if (Session["ISREFRESH"] != null && Session["ISREFRESH"] != "")
isRefresh = (refreshState == (bool)Session["ISREFRESH"]);
}
protected override object SaveViewState()
{
Session["ISREFRESH"] = refreshState;
object[] AllStates = new object[3];
AllStates[0] = base.SaveViewState();
AllStates[1] = !(refreshState);
return AllStates;
}
In the button submit you can do it as
protected void btn3_Click(object sender, EventArgs e)
{
if (isRefresh == false)
{
Insert Code here
}
}

Categories