在C#后面的代码中调用javascript函数不起作用
代码如下
提交按钮代码如下
Code as follows
Submit button code as follows
string slip_No = string.Empty;
foreach (GridViewRow row in grdRpt.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
if (cb.Checked == true)
{
int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
slip_No = row.Cells[2].Text;
}
}
else
{
slip_No = row.Cells[2].Text;
slipnoList.Add(slip_No);
}
if (slipnoList.Contains(dist_requests.FirstOrDefault().SLIP_NO))
{
string confirmValue = Request.Form["confirm_value"];
int len = confirmValue.Split(',').Length - 1;
confirmValue = confirmValue.Split(',')[len];
if (confirmValue == "Yes")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "Confirm()", true);
}
if (confirmValue == "No")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "Confirm()", true);
return;
}
Javascript code as follows
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("There are some more items with same slip number is there. Do you want to proceeds?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
i我在上面的代码中调用上面的java脚本函数,如下所示
ScriptManager.RegisterStartupScript(this,this.GetType(),alert,Confirm(),true);
在gridview中如下
Slipno Slipdate Zone区位置
2365 28.05.2018 Fereke Oung 1001
2365 28.05.2018 Ferekes Sordi 1002
i已经在提交按钮中写了上面的代码,如果在gridview中没有多次存在意味着我想向用户显示消息。
用于向用户显示消息我在c#后面的代码中使用javascript函数。
但是当我点击提交时,java脚本函数警报信息没有显示。
我的下面代码中的错误是什么?
ScriptManager.RegisterStartupScript( this,This.GetType(),alert,Confirm(),true);
我必须在上面的代码中做出哪些更改。
我尝试了什么:
参考上文..
i am calling the above java script function in above code, below line as follows
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "Confirm()", true);
In gridview as follows
Slipno Slipdate Zone Section Location
2365 28.05.2018 Fereke Oung 1001
2365 28.05.2018 Ferekes Sordi 1002
i have written above code in submit button , if in the gridview slip no exists more than once means i want to show the message to the user.
for showing message to the user i am using javascript function in code behind c#.
But when i click the submit, the java script function alert message is not showing.
what is the mistake in my below code
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "Confirm()", true);
what changes i have to made in my above code.
What I have tried:
refer to above..
这没有意义。看起来好像你说上面的C#代码应该在浏览器端调用javascript方法。这永远不会奏效。这不是网络服务器的工作方式。
你知道RegisterStartUpScript的作用吗?提示,它就在名字中!
这不会在客户端上调用javascript方法。它将您指定的代码写入发送给客户端的页面的HTML中。没有更多。
检查重复项的代码不应该在服务器端用C#编写。它应该在客户端用javascript编写。
This doesn't make sense. It appears as though you're saying that the above C# code should call a javascript method on the browser side. That is never going to work. That's not how web servers work.
Do you have any idea what RegisterStartUpScript does? Hint, it's in the name!
This does NOT call a javascript method on the client. It writes the code you specify into the HTML of the page you send to the client. NOTHING MORE.
Your code that checks for duplicates should not be written in C# on the server side. It should be written in javascript on the client-side.