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>");
}
Related
What I'm trying to do is, when I click on the button it should show an alert box.
Here is the code.
protected void btnAdd_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\">alert('abc 1');
</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1",
script);
Response.Redirect("~/Index.aspx");
}
It is working perfectly but when I put this same code in another page it does not work.
what are the things I have to look into? any suggestion please?
move your js code to client side
<asp:Button ID="btnAdd" OnClientClick="alert('alert');" OnClick="btnAdd_Click" runat="server"/>
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Index.aspx");
}
May be you can try this - syntax may be wrong -
protected void btnAdd_Click(object sender, EventArgs e)
{
Response.Redirect("~/Index.aspx?Issuccess=1");
}
index.aspx page -
protected void Page_Load()
{
if(Request.QueryString["Issuccess"] != null && Convert.ToInt32(Request.QueryString["Issuccess"]) == 1)
{
string script = "<script type=\"text/javascript\">alert('abc 1');
</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert 1",
script);
}
}
Pass the last Parameter either True or False in ClientScript.RegisterScript and better you pass the alert message in this way.
Like This
protected void btnAdd_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('abc 1');",true);
Response.Redirect("~/Index.aspx");
}
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...
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
}
}
in HTML:
<script type="text/javascript">
function removeIndustry(item)
{
confirm(item);
}
</script>
in C#:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "<a href='javascript:removeIndustry('IndustryNameHere')'><i class='icon-remove'></i></a>Click";
}
The code is not working if I put any strings like 'IndustryNameHere' in that function. Without any values, it is working fine. Anyone please help me fix this error. Thanks a lot.
Try to escape quotes with \:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "<i class='icon-remove'></i>Click";
}
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>