Im working with asp.net framework 4.0 and I have this code:
form id="form1" runat="server" method="get" action="Profile.aspx"
// some code
asp:Button runat="server" ID="SubmitButton" Text="Submit"
Each time i click the submit button i get this error:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
any idea how to fix it ???
This is caused by cross-page POST (i.e. you are submitting the ViewState of the first page to the second). You can add PostBackUrl to the button like this:
<asp:Button runat="server" ID="SubmitButton" Text="Submit" PostBackUrl="~/WebForm2.aspx" />
Alternatively you can handle the click event of the button in the first page, move some of the logic in this handler and do a Response.Redirect (i.e. GET request) to the second page. The right solution depends on your particular case.
If you want to send post back to your current page, remove in form tag method="get" action="Profile.aspx" attributes. And handle in codebehind post data from your page.
If your want to send data you another page like Profile.aspx, use PostBackUrl attribute of the button control, like Stilgar wrote for you. And then in Profile.aspx codebehind to get access to control from your current page use somethink like this:
If(Page.PreviousPage != null)
{
var textBox = Page.PreviousPage.FindControl("ControlID") as TextBox;
if(textBox != null)
{
//Use your logic here
}
}
Hope it will be helpful for you!
Best regards, Dima.
Related
I have a form with a bunch of fields that I am using RequiredFieldValidator, RegularExpressionValidator, and CustomValidator.
I want my form to perform client side checks when tabbing between fields (it currently does this), but I want to force a server side submit when the asp:button is clicked. Right now, if a form field is determined invalid on the client side, the submit button doesn't do anything. I want it to submit the page and perform the server side check.
The reason I want this to happen is because I want the page to go back to the top and display all possible issues and make it obvious to the user that there was a problem. Currently if they didn't see the client side error message, they may just click submit, see nothing happen, and end up confused.
Example field on aspx Page:
<asp:TextBox MaxLength="30" ID="LNom" runat="server" /><asp:RequiredFieldValidator ID="reqLNom" runat="server" ErrorMessage="Last Name Required" ControlToValidate="LNom" /><asp:CustomValidator ID="valLNom" runat="server" ErrorMessage="Please enter a valid last name (less than 30 characters in length)." ControlToValidate="LNom" OnServerValidate="ValidationLastName" />
<asp:Button ID="Submit" Text="Submit" runat="server" onclick="Submit_Click" />
Submit button Code Behind:
protected void Submit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
// Do stuff
}
}
Obviously there is a bit more to this, but you get the idea.
Try asp.net ValidationSummary. See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary(v=vs.110).aspx
It does exactly how you want it. It can be a pop up or inline notification that tells the user what s/he needs to fix.
If you add 'CausesValidation="False"' to your asp:button declaration. This will cause the postback to happen regardless of the outcome of client side validation
You can do this client side, no need for a post back. Look into the ValidationSummary control:
http://www.w3schools.com/aspnet/control_validationsummary.asp
I want to reset a form but I'm using a few "Required Field Validator" every time I click the reset button the error message shows requiring fields... What I tried:
ReuiqredFieldValidator.Enabled = false;
TextBox.Text="";
Response.Redirect("Samepage.aspx");
Response.Redirect("Request.RawUrl");
<input type=reset>
Try this:
<asp:Button
runat="server"
ID="reBt"
Text="Reset"
OnClientClick="this.form.reset();return false;"
CausesValidation="false"
/>
from here
You can apply ValidationGroup property to group of controls and the relevant asp button and do not provide ValidationGroup to reset button.
Or same can be achieved vice-versa
This is applicable as you using asp.net controls
Refer this link
you can try it
<input type="reset" causesvalidation="False">
It should work.
It works for input submit so I think it should work on input reset also.
Details Input =submit
Edit 1
Or if you are using jQuery you can use like this
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
References
Clear form fields with jQuery
How to reset a form using jQuery with .reset() method
jQuery reference
:reset Selector
So that's a long title, here's what I'm doing: To avoid having report parameters show up in the URL I'm doing this in a button click handler in the code-behind to show the report:
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("<html><head></head>");
System.Web.HttpContext.Current.Response.Write("<body onload=\"document.mainform.submit(); \">");
System.Web.HttpContext.Current.Response.Write(string.Format(CultureInfo.InvariantCulture, "<form name=\"mainform\" method=\"post\" action=\"{0}\">", ReportURL));
foreach (string key in Params.Keys)
{
System.Web.HttpContext.Current.Response.Write(string.Format(CultureInfo.InvariantCulture, "<input name=\"{0}\" type=\"hidden\" value=\"{1}\">", key, Params[key]));
}
System.Web.HttpContext.Current.Response.Write("</form>");
System.Web.HttpContext.Current.Response.Write("</body></html>");
This works great, but when I go back from the report I'm taken to the page I just generated which immediately submits the form due to the onload event. I can hit back twice really quickly to go back past it but this isn't ideal.
I've tried opening a new window with JavaScript using ClientScript.RegisterStartupScript before writing the html but I don't know how (or if it's even possible) to get the HttpContext for the new window.
I wasn't able to recreate the problem using your code, so more context may help. Depending on how the rest of the page is structured, you may be able to use PostbackURL instead of a click handler:
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="Key1" Value="Value1"/>
<asp:HiddenField runat="server" ID="Key2" Value="Value2"/>
<asp:Button runat="server" ID="button"
Text="ok" PostBackUrl="ReportURL.aspx"/>
</form>
This way you don't have to worry about an extra/replaced page in the browser history.
I ended up opening a new window. I added two small javascript functions
function openNewWin() {
$('form').attr('target', '_blank');
setTimeout('resetFormTarget()', 500);
}
function resetFormTarget() {
$('form').attr('target', '');
}
Then in the linkbutton I set
OnClientClick="openNewWin();"
The OnClick still executes the code above server-side but the response is written to the new window. The setTimeout in the openNewWin function resets the form's target property so the app will still function normally after coming back to it.
On our ASP.NET/C# web application, we are handling error/notifications with a div in our Master page that is set to Visible="false". What we are doing is, from our aspx pages when we want to create a notification message using the following code:
public static void DisplayNotificationMessage(MasterPage Master, string message)
{
if (Master.FindControl("divmsgpanel") != null)
{
Master.FindControl("divmsgpanel").Visible = true;
}
TextBox thetxtbox = (TextBox)Master.FindControl("txtboxmsgcontents");
if (thetxtbox != null)
{
thetxtbox.Text = message;
}
}
And the HTML in the Master page:
<%--start msgpanel--%>
<div id="divmsgpanel" runat="server" visible="false">
<div id="msgpanelheader">
</div>
<div id="divmsgpanelmainstage">
<asp:TextBox ID="txtboxmsgcontents" runat="server" TextMode="MultiLine"
Width="249px" SkinId="messageboxcontents"></asp:TextBox>
<asp:Label ID="lblmsgcontents" runat="server" Text="Label"></asp:Label>
</div>
<div id="msgpanelfooter">
<asp:Button ID="btnmsgcloser" runat="server" Text="Close"
onclick="btnmsgcloser_Click" />
</div>
</div><%--end msgpanel--%>
This is working quite well so far --- it offers us a centralized way of handling error messages for all pages, while allowing us to easily style the message box to the style of the overall site. We can just call this DisplayNotifcationMessage method from any page in one line. The div contains a "Close" button as well, which again makes it Visible="false".
The only problem we are having (or at least, the only problem we are aware of, anyway!) is with forms submission. On our "Add Employees" page, for example, after you have successfully added an employee, you get a nice notification message using the above method, which says "Employee successfully added." The problem is, is that after you hit the message box's "Close" button, you can press the browsers back button to go Back to the page's previous state (which had the message box's Visible attribute set to true).
I found some guides that detail "tricks" to "disable" the back button (such as A Thorough Examination of "Disabling The Back Button". Ultimately though, these "tricks" are not real solutions to the problem, but rather workarounds that aren't guaranteed.
Bottom line questions:
1) Is this method of having a centralized message box hidden in the Master page a viable method of handling error/notifications messages? Are there any other potential pitfalls that we just haven't noticed yet?
2) How can we handle the "Back" button dilemma?
I have some text fields and a button on my aspx. The text fields are validated using required field validators. On button i have set OnClientClick() event to do certain JS operations.
The problem here is that, the JS function is called before validations are completed. That is i need to validate first and then call js on client click event of button.
Can anybody help me?
Thanks for sharing your time.
You could do the client side validation manually:
<asp:Button ID="Button1" runat="server"
OnClientClick="if (Page_ClientValidate()) return YourFunction(); else return false;"
OnClick="Button1_Click" />
Remember to do whatever checking server side as well since you javascript could be manipulated and/or bypassed.