Calling javascript from Codebehind
I have read many different resources but am still not sure if this is possible without using AJAX.
I have a javascript function that loads a div into a modal and says "Loading Please Wait" I have named this funciton loadingModal()
function loadingModal(url)
{
loadModal(...)
}
What I need to do is only trigger this after I have verified that the password and username are correct on the server side so:
btnSubmit_OnClick(object sender EventArgs e)
{
string usr;
string password;
if (verify(usr, password))
{
///// TRIGGER JAVASCRIPT HERE
LOAD TONS OF SESSION VARIABL开发者_如何学PythonES
.
.
.
}
else
Show Error and Definitely Don't ever mention still loading
}
I know I could just attach an onclientclick call to the javascript however it would load the loading modal even if it was an invalid username and password
Can I trigger javascript mid execution from the server side?
Can I trigger JavaScript mid execution from the server side?
No. JavaScript code is evaluated on the client-side (in the browser), long after the server-side has finished processing the request. Client-side and server-side scripts are run at different places and at different times. There cannot be that kind of direct interaction.
You could use AJAX, if you do not want to trigger a full page refresh. When you use AJAX, your client (the browser) will issue a fresh new request to the server. The server processes this request (checks username and password, for example) and returns a response (access denied or access granted) back to the client. It is then up to the client to handle the response appropriately.
If you mean add
javascript code to the page, see ScriptManager.RegisterStartupScript
Hope its not too late...
Your best bet is to add 2 asp:Panel: One with a Ajax loading gif that is normally visible until the page is ready and one with style="display:none" and an asp:Label within that panel.
In code-behind, when validation is false, you just change the style to display:inline and set the text of the label to whatever you need. And of course, exit sub without displaying your modal thingy!
Here's an example:
<asp:Panel ID="pnlError" runat="server" style="display:none; position:absolute; left:300; top:300; width:300;">
<asp:Label ID="lblErr" runat="server" style=" color:Red;"></asp:Label>
</asp:Panel>
If validation = false Then
lblErr.Text = "Wrong user name or password or whatever!..."
pnlLoading.Style.Item("display") = "none"
pnlError.Style.Item("display") = "inline"
Exit Sub
End if
This way your user will see the loading gif, knowing "something" is in poreparation, and if wrong, he is warned.
精彩评论