__doPostBack not being included as part of control - c#

I have been scratching my head on this one for a few hours now and the issue seems to be interesting. I seem to be missing the __doPostBack event that should come with my control but it does not. Some have suggested adding:
ClientScript.GetPostBackEventReference(this, string.Empty);
this however, while it does create a function definition I am looking for, no matter what I do the page reloads on the postback event. As some have mentioned in:
__doPostBack is not defined
this is because the control ID does not match/not found.
I am obviously doing something wrong considering that 1) The postback function does not render without being brute forced in 2) Even when brute forced in, the postback cannot find the appropriate setup. Here are the snippets of code:
Class definition
[ParseChildren(true)]
[PersistChildren(true)]
[ToolboxData("<{0}:Accordion runat=server></{0}:Accordion>")]
public class Accordion : DataBoundControl, IPostBackEventHandler
Events
public event JQUI_Event ItemClick;
Click handler, RaisePostBackEvent, and the OnLoad which appends the __doPostBack
public void RaisePostBackEvent(string eventArgument)
{
ItemClick(null, null);
}
protected override void OnLoad(EventArgs e)
{
Page.ClientScript.GetPostBackEventReference(this, string.Empty);
base.OnLoad(e);
}
protected virtual void OnClick(EventArgs e)
{
if (ItemClick != null)
{
ItemClick(null, null);
}
}
Finally the code which generates the the postback:
if (ItemClick != null)
{
outPut += "<script> $(function() { $('#" + ClientID + "').find('div').on('click', function() { __doPostBack('" + UniqueID + "', '') }); }); </script>";
}
writer.Write(outPut);
Let me know if you need more code. I didn't want to just throw a long page of code and just tell people to figure it out.
EDIT:
Ok, got the RaisePostBackEvent(string eventArgument) to get called. However, the issue I am having is that the page still decides to reload. I have tested to make sure that it calls the correct function in the correct control (not just because it's a postback). Is there a way to limit this to a partial postback rather than a full postback?

See the GetPostBackEventReference documentation, which shows it returns a string, which contains the __doPostBack method call. It's not a registration method for the control. You then render this string with your control. The __doPostBack is not an automatic output when you are using custom controls.

Related

A Test on Page Life Cycle for loading ViewState

I'm trying to understand the phases of an asp.net page lifecycle by building a simple site web composed of a web form with a code behind in c#.
In the aspx page there is only a button to make a postback so I won't show the code; instead, I'll report the browser output because I use the Response.Write method to write all the page phases that I'm handling.
In the aspx.cs file I started the class with a property:
public string prop
{
get
{
object _propViewstate = ViewState["propview"];
if (_propViewstate != null)
{
return (string)_propViewstate;
}
else
{
return "Prop ViewState still not avaiable <BR> ";
}
}
set
{
ViewState["propview"] = value;
}
}
How you can see it's used to store in the ViewState a string value that I'll check in each phase. The setting of the property becomes in the method that handles the PreInit event of Page only if it's not a postback (I take advantage of AutoEventWireUp):
protected void Page_PreInit(object sender, EventArgs e)
{
Response.Write("PreInit_Method <BR>");
if (!Page.IsPostBack)
{
prop = "PreInit";
}
Response.Write(prop + "<BR> <BR>");
}
Then I'll override these methods of the Page to check this property in the ViewState: OnInit, OnInitComplete, OnPreLoad, OnLoad, OnLoadComplete. The code is very similar so I'll show only the OnInit override:
protected void Page_Init(object sender, EventArgs e)
{
Response.Write("Init_Method <BR>");
if (!Page.IsPostBack)
{
prop += " - Init";
}
Response.Write(prop + "<BR> <BR>");
}
I update the value of the property in the ViewState for each method if it's not a postback. I override also the LoadViewState method:
protected override void LoadViewState(object savedState)
{
Response.Write("LoadViewState_Method <BR>");
Response.Write("Before calling base method: ");
Response.Write(prop);
base.LoadViewState(savedState);
Response.Write("After calling base method: ");
Response.Write(prop + "<BR> <BR>");
}
Now I show the output before and after the postback and then finally my questions will come:
Without PostBack
After PostBack
My questions:
0) First of all, I'm using those methods correctly to test the loading of the ViewState? Which would be a better way to test it?
1) Before to PostBack, the property is available in all the methods. This is because I'm accessing to this viewstate's property that I just created? (If I do Response.Write(ViewState["propview"] + "<BR> <BR>"); I got the same effect, obviously)
2) Based on my test, I could say that the ViewState is loaded in the LoadViewState method because before to call the parent's method inside it I can't see my property set but just after that call it's available.
On the net, I read that the ViewState is instead loaded on LoadPageStateFromPerstistenceMedium. In the image from the official ASP.NET Page Life Cycle, I see that LoadPageStateFromPerstistenceMedium is called before LoadViewState and with my test inside this method I checked that my property is not available before the call to base.LoadViewState so I would say that the ViewState is loaded in the Page's LoadViewState method, correct?

Window.Load Not working on asp.net PostBack

I'm trying to get the height/width of an image using JQuery.
It won't work until the image is fully loaded by using $(window).load.
When doing an asp.net UpdatePanel, the events are unbind from the elements. I've tried using ScriptManager.RegisterStartupScript to call a Javascript function to bind all of the events again, but it won't run the $(window).load. Please help.
External JS Code
$(window).load(function () {
WindowLoad();
});
function WindowLoad() {
alert($("#test").width());
}
C# Code
protected void Page_PreRender(object sender, EventArgs e)
{
if (IsPostBack)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "WindowLoad" + DateTime.Now.Ticks, "WindowLoad();", true);
}
}
protected void Previous_Click(Object sender, ImageMapEventArgs e)
{
String value = e.PostBackValue;
if (value == "test")
{
imgTest.ImageUrl = IMAGEPATH;
}
}
When the page first loads. The $(window).load gets the height/width of the image just fine because it waits for the image to finish loading onto the page before it displays the width/height.
When you click on the imagepath, it'll do a partial postback. When that occurs, the JS are unbind from the elements. Previous_Click will run first and then the Page_PreRender to register the JS on the elements. This is where I need to make sure I get the height/width of the image again. So that JS can do some recalculations. On the partial postback, the results ends up being 0. I do notice that the UpdatePanel takes a while to update the image path, so they may have some cause as to why the results become 0, but I would assume the UpdatePanel will finish loading everything first before trying to run the WindowLoad function.

Disable RegisterOnSubmitStatement for specific buttons

I have a RegisterOnSubmitStatement that takes a method name plus validation group. The problem that I am having is that no matter what button I push on the page, the client side validation is getting checked. I only want it to run if the button pushed is part of that validation group.
So in my c# code, I have this:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterOnSubmitStatement(Page, Page.GetType(), "loadingHandler",
"AlwaysFireBeforeFormSubmit('loginControl')");
}
And that calls the following javascript function:
function AlwaysFireBeforeFormSubmit(validationGroup) {
if (typeof (Page_ClientValidate) == 'function') {
var isPageValid = Page_ClientValidate(validationGroup);
if (isPageValid) {
modals.showPleaseWait();
}
} else {
modals.showPleaseWait();
}
}
Now, no matter what button with a postback is called, this will get fired for the validation group specified.
Ideally, this would only be fired if the button is within the validation group specified. I'd even be happy if I could find out which button fired the event so that I can check the href to see if it contains, "WebForm_DoPostBackWithOptions" in the href. If it does, run validation, otherwise skip it and shop the please wait modal.
Thoughts? Questions? Am I crazy?
-C

Button click event error

Having just added a new button in my web application, I get an error when clicking on it, and I'm wondering if this is related to misplaced code. I will describe what/where I did, briefly. Thanks very much.
In ascx file:
<asp:Button ID="btn_rezerv" runat="server" Text="Reserve film" OnClick="btn_rezerv_Click"/>
In the ascx.cs file:
namespace CinProj.UserControls
{
public partial class FilmsList : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
PopulateControls();
}
private void PopulateControls()
{
string categId = Request.QueryString["CategID"];
string filmId = Request.QueryString["FilmID"];
....
if (categId != null)
{
.....
}
if (filmId != null)
{
......
Button btn_rezerv = (Button)item.FindControl("btn_rezerv");
}
}
protected void btn_rezerv_Click(object sender, EventArgs e)
{
string fid = Request.QueryString["FilmID"];
ShoppingCartAccess.AddItem(fid);
}
}
}
"Server Error in '/' Application.
Invalid postback or callback argument. Event validation is enabled using in configuration or <%# Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation. "
Another problem could be because your PopulateControls method should probably only be called when during the Page Load when it's not a PostBack. I can't tell from above, but to me it looks like it only needs done on Load. Try wrapping that call with this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateControls();
}
}
It's likely the result of making some sort of client change that the server doesn't know about. Many times this is the result of changing values in a dropdown in JavaScript, for example.
To fix, you could:
Do away with using JavaScript for said modification
Use an UpdatePanel and add your control to it. If the client needs to make a change, trigger the UpdatePanel's update in order for the control's viewstate to update.

How can i have Response.Redirect() work from MasterPage?

I have a problem: when i call a Response.Redirect() from the MasterPage it doesn't work.
Well, debugging i can see that until the Pre_Render() method the target page is loaded, but then is rendered the previous page.
Here's some code to better explain:
(from MasterPageMain.master.cs)
protected void Page_Init(object sender, EventArgs e)
{
string m_QueryStringValue = Request.QueryString.Get("action");
if ((!string.IsNullOrEmpty(m_QueryStringValue)) && (m_QueryStringValue.ToLower() == "send"))
{
if (Session["to"] != null && Session["to"] is List<string>) this.SendPageByMail();
else
{
Session.Add("AddressToSend", Request.RawUrl);
Response.Redirect("~/chooseRecipients.aspx");
}
}
}
I have a javascript that adds the querystring adding "action=send" when i click on the Send button.
If i am on page "~/somethingInterestingToSend()" -for example- i want to get on the recipient selection page, but when i click the Send button i see always the same page.
What coul be the mistake?
If you want to redirect not logged in users to a login-page you should check the Request.RawUrl() like this:
string strURL=Request.RawUrl.ToUpper();
if (!strURL.Contains("LOGIN.ASPX") && !strURL.Contains("LOGOUT.ASPX")
&& !strURL.Contains("ERROR.ASPX") && !strURL.Contains("UNDERCONSTRUCTION.ASPX"))
{
Response.Redirect("~/Login.aspx", false);
}
All other sites will be redirected.
I am not sure I fully understand your description of the problem, but here are a few things to consider:
You mention a send button. If this is an , clicking it fires a javascript postback to the server. This postback is to the original URL. I'm not sure what you are modifying with Javascript, but I don't think it would change the postback URL (and querystring).
If you need to perform logic to redirect you might want to execute in the button click event on the server.
If you don't need to execute any logic on the server, you could to the redirect with javascript:
window.location = "chooseRecipients.aspx";
Can't test this theory (running from memory at the moment), but give this a shot:
(sorry, cleaned up the code a bit as well)
protected void Page_Init(object sender, EventArgs e)
{
string m_QueryStringValue = Request.QueryString.Get("action") ?? "";
if (m_QueryStringValue.ToLower() == "send")
{
if ( (Session["to"] as List<string>) != null)
{
this.SendPageByMail();
}
else
{
Session.Add("AddressToSend", Request.RawUrl);
Response.Redirect("~/chooseRecipients.aspx", false);
HttpContext.Current.ApplicationInstance.CompleteRequest()
}
}
}
I don't know if its the root of your problem but I'd change 2 things. I'd change your code to:
Response.Redirect("~/chooseRecipients.aspx", false);
and move the logic to PageLoad

Categories