I am trying to make an anchor tag cause both client and server validation. I have this code for now:
$(document).ready(function () {
$('div#imgEmailVerifyLoader').hide();
$('a#btn_SubmitContactMessage').click(function ()
{
if (Page_ClientValidate()) // this will trigger all validators on page
{
$('div#imgEmailVerifyLoader').show('slow');
window.Form_OnMasterPage.submit();
return true;
}
else
{
return false;
}
});
});
<a id="btn_SubmitContactMessage" href="Contact.aspx" onclick="Validate();" runat="server">SUBMIT</a>
This performs client validation properly and shows the error message. I have validation controls for each of the textboxes on the page. I also added a server click event handler in code behind for this:
btn_SubmitContactMessage.ServerClick +=new EventHandler(btn_SubmitContactMessage_ServerClick);
}
protected void btn_SubmitContactMessage_ServerClick(object sender, EventArgs e)
{
if (!Page.IsValid)
{
RequiredFieldValidator4.ErrorMessage = "show";
return;
}
}
But when I try to test it by turning off javascript the link(submit) does not postback. Why is that happening?
Now, how do I make sure that validation is being done on the server side to after postback.
I would imagine it's because of the 'onclick=validate()'. Instead of doing that you should register that event inside of '$(document).ready(function ()' like you've got your other JavaScript. That way if JavaScript is not available the form is submitted normally and your server side validation kicks in.
Related
I have a RadGrid control which is created dynamically on page_init and added to a placeholder which is inside an updatePanel on the page.
I'd need to add a new Button to the CommandItem section of the RadGrid. The button has to support full postback.
RadGrid has an event called RadGrid_ItemCreated() and that's where I've added my new button and it appears on my RadGrid:
protected virtual void RdGridItemCreated(object sender, GridItemEventArgs e)
{
var itemType = e.Item.ItemType;
switch (itemType)
{
// other cases...
case GridItemType.CommandItem:
{
var gridCommandItem = e.Item as GridCommandItem;
if (gridCommandItem == null) return;
if (this.EnablePdfExport)
{
var pdfButton = CreateExportToPdfButton();
PageUtil.RegisterPostBackControl(pdfButton);
// this is the cell which contains the export buttons.
((Table)gridCommandItem.Cells[0].Controls[0]).Rows[0].Cells[1].Controls.Add(pdfButton);
}
break;
}
}
}
The button has a Click event and a method has been added to it as an event handler:
private Button CreateExportToPdfButton()
{
var result = new Button();
result.ID = "btnExportToPdf";
result.Click += ExportToPdfButtonClick;
result.CssClass = "rgExpPDF";
result.CommandName = "ExportToPdf";
result.Attributes.Add("title", "Export to Pdf");
return result;
}
To register the postback event for this control I've used the RegisterPostBackControl() method of the ScriptManager.
public static void RegisterPostBackControl(Control control)
{
var currentPage = (Page) HttpContext.Current.CurrentHandler;
var currentScriptManager = ScriptManager.GetCurrent(currentPage);
if (currentScriptManager != null)
{
currentScriptManager.RegisterPostBackControl(control);
}
}
When I click the button on the RadGrid, it posts back to the server but the problem is that its Click event is never raised:
private void ExportToPdfButtonClick(object sender, EventArgs e)
{
// process
}
I don't understand why; any thoughts/help?
If I don't set ID for the button, then the click event is raised but a new problem arises in that case. When a partial postback happens on the page by an external drop downlist to update the radgrid, then my custom export button sends postbacks asynchronously whereas it should post back fully.
Many thanks,
I fixed it by adding the new control in the following event:
this.RadGrid.MasterTableView.Init += MasterTableViewInit;
void MasterTableViewInit(object sender, EventArgs e)
{
if (!this.EnablePdfExport) return;
var commandItem = this.RadGrid.MasterTableView.GetItems(GridItemType.CommandItem).SingleOrDefault();
if (commandItem == null) return;
AddPdfButton(commandItem as GridCommandItem);
}
I am having the same problem. I have tracked it down to Telerik switching the Visible property of the child controls of the RadGrid to false during Render. This only affects partial-page postbacks because Render gets called before the PageRequestManager writes the JavaScript for the postback controls, and it skips controls which are not Visible. For a full postback (or the initial page load), the PageRequestManager writes the JavaScript for the postback controls before the RadGrid is Rendered, and thus the controls are still Visible.
I'm not sure why Telerik is doing this as it causes a lot of problems to muck with the Visible property during the Render stage.
i am a beginner
function PopupUserRegist() {
result = $.ajax({ url: "Default.aspx?cmd=Setting",
async: false ,
complete: function () {
// unblock when remote call returns
}
}).responseText; ;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request["cmd"] == "Setting")
{
div.InnerText = "test"; //This does not change inner text
}
}
protected void Button2_Click(object sender, EventArgs e)
{
div.InnerText = "test";//This change inner text
}
when call PopupUserRegist(); does not change?
You've got confused about the flow of control with ASP.NET.
Page_Load() occurs on the server side at page generation time and can change the data that is about to be sent back in the HTML page to the browser.
When you call the ASPX again through ajax() from JavaScript, the codebehind can't directly touch the page content, because that's already sitting over on the browser. If you want to change the data in the page, you'll have to have the ASPX return a plain value to the JavaScript code in the responseText:
$('#myDiv').text(
$.ajax({
url: 'Default.aspx?cmd=Setting',
async: false
}).responseText
);
or, better, avoiding the unpleasant use of synchronous xmlhttprequest:
$.get('Default.aspx?cmd=Setting', function(result) {
$('#myDiv').text(result);
});
If you really want to blur the line between code that runs on the client and server sides, look at using runat="server", click events, postbacks and viewstates.
Check if the condition is met.
i am a beginner
function PopupUserRegist() {
result = $.ajax({ url: "Default.aspx?cmd=Setting",
async: false ,
complete: function () {
// unblock when remote call returns
}
}).responseText; ;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Request["cmd"] == "Setting")
{
div.InnerText = "test"; //This does not change inner text
}
}
protected void Button2_Click(object sender, EventArgs e)
{
div.InnerText = "test";//This change inner text
}
when call PopupUserRegist(); does not change?
You've got confused about the flow of control with ASP.NET.
Page_Load() occurs on the server side at page generation time and can change the data that is about to be sent back in the HTML page to the browser.
When you call the ASPX again through ajax() from JavaScript, the codebehind can't directly touch the page content, because that's already sitting over on the browser. If you want to change the data in the page, you'll have to have the ASPX return a plain value to the JavaScript code in the responseText:
$('#myDiv').text(
$.ajax({
url: 'Default.aspx?cmd=Setting',
async: false
}).responseText
);
or, better, avoiding the unpleasant use of synchronous xmlhttprequest:
$.get('Default.aspx?cmd=Setting', function(result) {
$('#myDiv').text(result);
});
If you really want to blur the line between code that runs on the client and server sides, look at using runat="server", click events, postbacks and viewstates.
Check if the condition is met.
I need to logout on close page or on close browser ...
as usual ...
but by default ASP Membership wont do it ...
How to make logout when I just leave my site
(FormsAuthentication.SignOut();
HttpContext.Current.Session.Abandon();)
btw got problem on logout button.
here is a code on page render to check if user authorization=true I setup "authorezated panel with logout button"
protected void Page_PreRender()
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
Label1.Text = System.Web.HttpContext.Current.User.Identity.Name;
MultiView1.ActiveViewIndex = 0;
}
else
{
MultiView1.ActiveViewIndex = 1;
}
}
but when I click logout
protected void Button2_Click(object sender, System.EventArgs e) //logout
{
if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
System.Web.Security.FormsAuthentication.SignOut();
System.Web.HttpContext.Current.Session.Abandon();
MultiView1.ActiveViewIndex = 1;
}
}
Page rendering before logout and I can't see ActiveViewIndex = 1 :(
So I need to click twice on logout button . weird.
There is no request sent to the server when you close a page. There is only a javascript event that fires. So you would have to do a ajax request to do what you need. But I wouldn't depend on that. But if you really need to sign out your user when he closes the page, then that is what you have to do. You can do something like this (if you use jquery):
window.onunload = logOut;
function logOut() {
$.get("[[url to a resource that logs you out]]");
}
About the second problem, I would suggest put the code in your Page_PreRender() in Page_Load() instead.
I have an extender control that raises a textbox's OnTextChanged event 500ms after the user has finished typing. The problem with this is that OnTextChanged gets raised when the textbox loses focus, which causes problems (because of the postback).
What I'd like to do is give the extender control its own server-side event (say, OnDelayedSubmit) so I can handle it separately. The event will originate in the extender control's behavior script (after the 500ms delay), so putting a __doPostBack in onchanged is not an option.
Can anyone shed light on how to go about this?
After plenty of reading up on extender controls and JavaScript, I've cobbled together a solution that seems to be working so far.
The main trick was getting the necessary postback code from server-side to the client-side behavior script. I did this by using an ExtenderControlProperty (which is set in the control's OnPreRender function), and then eval'd in the behavior script. The rest was basic event-handling stuff.
So now my extender control's .cs file looks something like this:
public class DelayedSubmitExtender : ExtenderControlBase, IPostBackEventHandler
{
// This is where we'll give the behavior script the necessary code for the
// postback event
protected override void OnPreRender(EventArgs e)
{
string postback = Page.ClientScript.GetPostBackEventReference(this, "DelayedSubmit") + ";";
PostBackEvent = postback;
}
// This property matches up with a pair of get & set functions in the behavior script
[ExtenderControlProperty]
public string PostBackEvent
{
get
{
return GetPropertyValue<string>("PostBackEvent", "");
}
set
{
SetPropertyValue<string>("PostBackEvent", value);
}
}
// The event handling stuff
public event EventHandler Submit; // Our event
protected void OnSubmit(EventArgs e) // Called to raise the event
{
if (Submit != null)
{
Submit(this, e);
}
}
public void RaisePostBackEvent(string eventArgument) // From IPostBackEventHandler
{
if (eventArgument == "DelayedSubmit")
{
OnSubmit(new EventArgs());
}
}
}
And my behavior script looks something like this:
DelayedSubmitBehavior = function(element) {
DelayedSubmitBehavior.initializeBase(this, [element]);
this._postBackEvent = null; // Stores the script required for the postback
}
DelayedSubmitBehavior.prototype = {
// Delayed submit code removed for brevity, but normally this would be where
// initialize, dispose, and client-side event handlers would go
// This is the client-side part of the PostBackEvent property
get_PostBackEvent: function() {
return this._postBackEvent;
},
set_PostBackEvent: function(value) {
this._postBackEvent = value;
}
// This is the client-side event handler where the postback is initiated from
_onTimerTick: function(sender, eventArgs) {
// The following line evaluates the string var as javascript,
// which will cause the desired postback
eval(this._postBackEvent);
}
}
Now the server-side event can be handled the same way you'd handle an event on any other control.