I am trying to do a simple form submit, which will take my application to another url in another domain. I have seen it working on my SYS environment but when I am doing that on Prod, it does not work.
For ex, my website is something like this https://dosomething.com/Index
The value from document.getElementById("hidUrl").value is like https://anotherdomain.com/id=1
Ideally when the form submits, I should be redirected to the page https://www.anotherdomain.com/id=1 (which is what I have seen on my test server) but on production, it is taking me to https://dosomething.com/Index/www.anotherdomain.com/id=1 and I am getting 404 error
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
I am not sure why is this happening. Has anyone have got any idea?
Much appreciated.
Thanks
Here is the HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function submitAuthenticationForm() {
document.getElementById("userName").value = document.getElementById("hidUserName").value;
document.getElementById("token").value = document.getElementById("hidToken").value;
document.getElementById("hiddenForm").action = document.getElementById("hidUrl").value;
document.getElementById("hiddenForm").submit();
}
</script>
</head>
<body onload="javascript:submitAuthenticationForm();">
<form method="post" id="hiddenForm" runat="server">
<div>
<asp:HiddenField ID="hidUserName" runat="server"/>
<asp:HiddenField ID="hidToken" runat="server"/>
<asp:HiddenField ID="hidUrl" runat="server"/>
</div>
</form>
</body>
</html>
Here is what the code behind cs file doing:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
hidUserName.Value = Session["authUserName"].ToString();
hidToken.Value = Session["authToken"].ToString();
hidUrl.Value = Session["authUrl"].ToString();
Session["authUserName"] = null;
Session["authToken"] = null;
Session["authUrl"] = null;
}
}
Related
function alertbox() {
var mode = document.getElementById('<%= hdnMode.ClientID %>').value;
if (mode == "EDIT")
return false;
if (confirm("the same data is present against that ID ?") == true) {
document.getElementById('<%= hdnYesNo.ClientID %>').value = "COPY";
}
else {
document.getElementById('<%= hdnYesNo.ClientID %>').value = "CANCEL";
}
}
the above confirm message should appear after the retrieve data from sql and
Page.ClientScript.RegisterStartupScript(this.GetType(),"CallMyFunction",
"MyFunction()",true);
how to use it from codebehind and if so then how to get the return value based on the value
copy and cancel
I will try to provide an example that I just created as a testing application.
Firstly, I used the ScriptManager to apply all the javascript files that I like to be present for the web page as follows:
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/JS/tester.js" />
</Scripts>
</asp:ScriptManager>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Call database" />
</div>
</form>
</body>
In the <Scripts> tag add more of your JS files. This will ensure that your javascript file is added when you load the webpage.
The code that is there in my tester.js is:
function alertbox(data) {
alert("Completed the database operation with following data = " + data);
}
Now coming to your code behind scenario, what I have in my sample data is, I created a button on the webpage that would do some database operations and once completed it will alert the user about the sql update.
The button event handler is as follows:
protected void Button1_Click(object sender, EventArgs e)
{
Thread.Sleep(2000);
int sqlReturnValue = ExecuteTheQuery();
ScriptManager.RegisterStartupScript(this, typeof(string), "Database Completed", "alertbox(" + sqlReturnValue + ");", true);
}
Now this will call the Javascript function alertbox.
(Note: this is just a small example of how you can achieve the thing that you expect)
Update:
The same can be achieved with ClientScript as well.
What I did is, add a script tag:
<head runat="server">
<title>Test Page</title>
<script src="JS/tester.js" type="text/javascript"></script>
</head>
In the code behind of the button click:
protected void Button1_Click(object sender, EventArgs e)
{
Thread.Sleep(2000);
ClientScript.RegisterStartupScript(this.GetType(), "Database Completed", "alertbox(23);", true);
}
For understanding ClientScript and ScriptManager, check this question.
I want to get HTML DIV content via asp.net C# code behind event.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Report.aspx.cs" Inherits="WebApplication1.Report.Report" %>
<!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">
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$('#_Hidden_CrystalReportContent').hide();
$('#_Hidden_CrystalReportContent').html("<b>I want to get Current value. 1<sup>st</sup></b>");
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="_Hidden_CrystalReportContent">I want to get Current value.</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
My code behind file as below.
public partial class Report : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void Button1_Click(object sender, EventArgs e)
{
string s = Request["_Hidden_CrystalReportContent"].ToString();
}
}
But I still cannot get div content value.
Please let me get your suggestion.
Make the div runat="server" to access on server.
Html
<div id="_Hidden_CrystalReportContent" runat="server">
Code behind
string htmlOfDiv = _Hidden_CrystalReportContent.innerHTML;
Javascript
$(document).ready(function () {
$('#<% _Hidden_CrystalReportContent.ClientID %>').hide();
$('#<%= _Hidden_CrystalReportContent.ClientID %>').html("<b>I want to get Current value. 1<sup>st</sup></b>");
});
Making a div server accessible by puttin runat="server" attribute cause the changed client id if CLientIDMode is not static. You will need to use ClientID attribute to get client id of div in javascript.
Edit: based on comments. You are trying to get the updated html, if so you then you wont get it as on post back only html form elements are posted. Put the changes in some hidden field and assess it on server.
In html
<input type="hidden" id="hdnDivContents" runat="server">
In javascript
$('#<% hdnDivContents.ClientID %>').val("<b>I want to get Current value. 1<sup>st</sup></b>");
In code behind
_Hidden_CrystalReportContent.innerHTML = hdnDivContents.Value;
I am trying to modify a simply chat application for learning purposes. The only change i made was to change a button to a serverside control. The problem i have is that the first time i broadcast a message, it works, and the Clients.addNotification(msg) is called. Yet the second time, although the javascript works, at its final step, the javascript undos all the changes and the Clients.addNotification(..) is never called :/ It only works for the first time! I have to rebuild my project to see it working again (a page refresh won't work)
public class Chat : Hub
{
public void Send(string message)
{
// Call the addMessage method on all clients
Clients.addNotification(message);
}
}
My aspx page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Client1.aspx.cs" Inherits="WebApplication1.WorkingWithHubs.Client1" %>
<!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">
<head runat="server">
<title></title>
<script src="../Scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script src="../Scripts/jquery.signalR.min.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
// Proxy created on the fly
var chat = $.connection.chat;
// Declare a function on the chat hub so the server can invoke it
chat.addNotification = function (message) {
$('#messages').append('<li>' + message + '</li>');
var labelValue = $('#total').val();
$('#total').val(parseInt(labelValue, 10) + 1);
};
$("#btn").click(function () {
// Call the chat method on the server
chat.send($('#msg').val());
});
// Start the connection
$.connection.hub.start();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" />
<div>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<input type="text" id="msg" runat="server" />
<asp:Button ID="btn" runat="server" Text="BroadCast" OnClick="btn_click" ClientIDMode="static" />
<input type="text" id="total" value="0" />
<ul id="messages">
</ul>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
My code behind:
public partial class Client1 : System.Web.UI.Page
{
public List<String> list = new List<String>();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_click(object sender, EventArgs e)
{
list.Add(msg.Value);
}
}
Once this example work I will shift the code to another application I am working on so that notifications can be immediately pushed to all clients.
I am a very beginner and I would appreciate all your help! Thanks a lot guys!
EDIT:
when checking out the network tab (on my chrome), for the first click I get a 'send' (signalR) and Client1.aspx and all works fine, for the second click onwards, I only get Client1.aspx, and no send whatsoever :/
for a quick and dirty solution, just place the update panel ONLY around the button, and remove
$("#btn").click(function () {
// Call the chat method on the server
chat.send($('#msg').val());
});
and instead, insert the following in the beginning
$('#form1').delegate('#btn', 'click', function () {
chat.send($('#msg').val());
});
Thanks to my friends shifty and red_square :)
i"m using asp.net FileUpload , after user input file he click on save button
in the c# i have this function
protected void btnUploadImages_Click(object sender, EventArgs e)
{
SaveImages(FileUpload1, "", returnAlbumId, out returnPhotoId);
}
this function save the image from the FileUpload1 so far all work as it"s should
but after the postback when i push the refresh button on the page i"m go to this function again , SaveImages function save the same image again .
the FileUpload1 didn't clear after the postback
thanks
Even i got the Same Problem I have resolved like Below.
After uploading the File If you Redirect to same page or some other page in your project. After Redirection Response will not be there once you redirected.
In My ASPX
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebApplication.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
</html>
In My Code Behind
public partial class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string path = Server.MapPath("~");
path = path + FileUpload1.FileName;
FileUpload1.SaveAs(path);
Response.Redirect("WebForm.aspx"); // Responce will be cleared. This Redirection will do the Trick
//Put the debugger and check it will work
}
}
Here, to show the success and error messages try to use sessions.
I have an aspx page defined as follows:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" language='javascript'>
function changecolor() {
var lbl = document.getElementById('lblDisplayDate');
lbl.style.color = 'red';
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDisplayDate" runat="server"
Text="Label"></asp:Label><br />
<asp:Button ID="btnPostBack2" runat="server"
Text="Register Client Block Script"
onclick="btnPostBack2_Click" />
</div>
</form>
Here is the btnPostBack2 Click event:
protected void btnPostBack2_Click(object sender, EventArgs e)
{
if (!ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
{
ClientScript.RegisterClientScriptBlock(this.GetType(),
"JSScriptBlock",
"changecolor();",
true);
}
}
Even though, I put the script in a function to change the color, it is still not doing it and why do I need to add the script tags to true if the function is already enclosed in script tags?
lblDisplayDate is in the Page Load where it is set to the current time on every page reload.
Change this line:
var lbl = document.getElementById('lblDisplayDate');
to:
var lbl = document.getElementById('<%= lblDisplayDate.ClientID %>');
Also, this probably isn't doing what you want it to do anyway. Try adding:
OnClientClick="changecolor()"
to your <asp:Button /> tag.
Edit At this point I am pretty confused as to what exactly you are trying to accomplish...
If you just want to change the color of the label without posting back, change your <asp:Button /> to a normal <input type="button" /> with an onclick handler and call your script, like this:
<input type="button" ... onclick="changeColor();" />
If you want to change the color of the label while posting back, then just change the label's background in the code-behind like this:
protected void btnPostBack2_Click(object sender, EventArgs e)
{
/* ... other stuff goes here ... */
lblDisplayDate.BackColor = Color.Red;
/* ... other stuff goes here ... */
}
like what Sean already stated: it is best to use OnClientClick in this case. OnClientClick would prevent a postback occuring - it is done dynamically using javascript. All u need to do is preference it to the javascript function.
I do not understand y u want to do a "RegisterStartupScript and RegisterClientScriptBlock" on colour change. These should be used when u want to add javascript from the code behind, but seeing that u have it written out already in the aspx page, its use is pointless.
Check this out. I have modified your code and its working fine now without postback.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language='javascript'>
function changecolor() {
var lbl = document.getElementById('lblDisplayDate');
lbl.style.color = 'red';
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDisplayDate" runat="server"
Text="Label"></asp:Label><br />
<asp:Button ID="btnPostBack2" runat="server"
Text="Register Client Block Script"
OnClientClick="changecolor(); return false" />
</div>
</form>
</body>
</html>
hope this helps