Get Javascript Variable Value in Server Side Without Page Load - javascript

I am trying to get value of Javascript variable
on server side, but it always shows values on
second button click and I don't want to reload this page again
protected void Button1_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageBox", "<script language='javascript'>setValues();</script>");
Thread.Sleep(5000);
Response.Write(HiddenField1.Value);
}
Javascript Code . . . ..
<script type="text/javascript">
function setValues()
{
var abc = "thi is first";
document.getElementById('<%=HiddenField1.ClientID%>').value = abc;
}
</script>

Related

How can I call a javascript function from server side

I try to call a javascript function from an other c# function but I have an error in my console
Uncaught ReferenceError: updateState is not defined
.ascx file
<script>
function updateState(){
console.log("test")
}
</script>
<button runat="server" ID="Btn_Modify_state" onserverclick="Btn_Modify_state_Click">
<i class="fas fa-edit"></i>
</button>
.ascx.cs file
protected void Btn_Modify_state_Click(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "updateState();", true);
}
I don't know how to resolve that issue someone have any idea ?
You may have forgotten to register the script. Try this different approach instead.
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Define the name and type of the client script on the page.
String csName = "updateState";
Type csType = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(csType, csName))
{
// If not, redefine your script
var csText = $"
<script type=\"text/javascript\">
function updateState(){
console.log("test")
}
</script>";
cs.RegisterClientScriptBlock(csType, csName, csText.ToString());
}
}
</script>
Source : https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.clientscriptmanager.registerclientscriptblock?view=netframework-4.8
The solution was to do that :
.ascx.cs
protected void Btn_Modify_state_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "<script type='text/javascript'>updateState();</script>");
}

window.returnValue rework for chrome

I have a java date picker and it is not passing back the value in chrome. I think its is due to the js using window.returnvalue. How would i fix this?
// Allow the browser to save/pass the date selected and close the window.
protected void calDate_SelectionChanged(object sender, System.EventArgs e)
{
string closeWindow = #"
<script language='javascript'>
window.returnValue='###date###';
window.close();
</script>";
closeWindow = closeWindow.Replace("###date###", calDate.SelectedDate.ToShortDateString());
ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", closeWindow);
}

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...

Receive function parameters in javascript?

Is possible receive the parameters that were sent from the
IFrame code behind (id and pTmp) the javascript popup then send them to the page parent
Code IFrame
protected void btnConfirm_Click(object sender, EventArgs e)
{
EDPBLL = new EDPLogic();
int id = EDPBLL.Add(Convert.ToInt32(Request.QueryString["EDP"]), Convert.ToInt32(Session["userId"]), DateTime.Now, Convert.ToInt32(ddlPrensa.SelectedValue));
string numeroEDP = EDPBLL.generarNumeroEDP(id);
EDPBLL.UpdateEdpCode(id, numeroEDP);
Session["pEDPId"] = id;
Session["numeroEDP"] = numeroEDP;
int pTmp = 4;
ScriptManager.RegisterStartupScript(Page, GetType(), "Popup", "<script>Popup('"+id+"','"+pTmp+"')</script>", false);
}
The script simulates a click event of parent window
Code JavaScript .aspx
<script type="text/javascript">
function Popup() {
$("#MainContent_btnPrueba", window.parent.document).trigger("click");
}
</script>
Use this
<script type="text/javascript">
function Popup(var1, var2) {
$("#MainContent_btnPrueba", window.parent.document).trigger("click");
}
</script>

javascript alert not firing

I have a new page with the following:
The Response.Redirect works, but I don't get a popup before hand...
Any ideas???
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["timeout"] != null && Request.QueryString["timeout"].ToString().Equals("yes"))
{
Response.Write("<script>alert('Your Session has Timedout due to Inactivity');</script>");
Response.Redirect("Default.aspx");
}
}
The Response.Redirect call never returns the code to the user. It immediately redirects the user to the next page. From the MSDN on Response.Redirect: "Any response body content such as displayed HTML text or Response.Write text in the page indicated by the original URL is ignored."
The Response.Redirect redirects the browser and your JavaScript does not get executed.
Try redirecting in JavaScript instead:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["timeout"] != null && Request.QueryString["timeout"].ToString().Equals("yes"))
{
Response.Write("<script>" +
"alert('Your Session has Timedout due to Inactivity');" +
"location.href='Default.aspx';" +
"</script>");
}
}

Categories