Page EventValidation is True error? - c#

i did two listbox and value swap by using Input sumbit type(Html tag) onclick event of Jquery..values are easy to swap from 1 listbox to another but when i go for store selected value from listbox so selected value given me..
First i got error Page Eventvalidation=true so i searched on google and solve this problem by using
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(lsttolimo.UniqueID,this.ToString());
base.Render(writer);
}
yet solution is not found. than after i was used UpdatePanel and add Each of The Controls in Triggers but still Solution is not found

First let's understand what the EventValidation means in the world of .NET.
When you create some server controls, then should be rendered into HTML, so that they can be shown in the browser. Now, consider that you have 10 items in a DropDownList. Those 10 items would be rendered as <option> tags inside <select> tag (they are converted to HTML equivalent control).
Now, ASP.NET encrypts those 10 values, and adds them to the EventValidation hidden field too. This means that you now have sent those 10 values TWO TIMES to the browser. Now, whenever somebody posts back the form, ASP.NET gets the posted value (one of those 10 items is posted back). It then decrypts those 10 encrypted values (now it has 10 + 1 values). It checks to see if the posted value is one of the 10 items or not. If it is, then ASP.NET gets sure that everything is OK. But if it's not, then it understands that somebody has tried to fool it.
Now, consider for example that you have a list of countries, and you only accept users from 3 countries, say for example, US, England, and Australia. However, someone comes to your registration page, understands the HTML and creates another form with the same fields and the same list of countries. But this time he also adds the name of another country to your list (say Iran).
If you don't have EventValidation turned on, and you don't explicitly check for the country, then you're simply fooled into accepting Iran as a valid country. This is called form spoofing.
Now, since you have two lists, and you swap their values, of course, .NET thinks that it's getting fooled. I recommend that you turn off the EventValidation (this is the easier way, but more prone to security problems) and check it yourself.
To turn off event validation, simply add this to your web.config file:
<system.web>
<pages enableEventValidation="false"
</system.web>

Related

C# ASP.NET - Dynamic MenuItems in a user control are randomly being duplicated outside of control creation

EDIT: I needed to skip control creation during post back -- see my answer below.
I'm working on a very basic front end to a simple tool and I wanted to present some data in a much more sorted and useful way, instead of making one huge wall of text. I found this tutorial on building a simple tabbed interface using MultiView, but have run into a bizarre problem. I can't use Ajax tabs because of legal hissy fits over 3rd party software.
My webpage is a basic ASP.NET page with a user control plopped in the middle of it. In this control's ascx file, I defined the Menu (empty) and the MultiView (also empty) so that I can dynamically populate the tabs with content driven from an external file.
When the default page's OnInitComplete function is called, I call through to the user control to load the data file, then build out the tabs and the view content based on the loaded data. I tried doing this from PageLoad, PreInit, and CreateChildControls, but I kept getting an errors saying that I was setting the the MultiView's active view index at an invalid time (and also that there were 0 views despite the fact I just added a bunch of them):
ActiveViewIndex is being set to '0'. It must be smaller than the
current number of View controls '0'. For dynamically added views, make
sure they are added before or in Page_PreInit event.
But OnInitComplete appears to work just fine, so I went with that.
I iterate over the loaded data (multiple lists of strings), and for each list, I add a MenuItem with the list's title to the Menu and a View to the MultiView. The View is populated with a table->row->cell as in the above tutorial. In the cell, I add the list title and a CheckBoxList data bound to the list of strings.
So far so good, but when I click on a tab (or one of the checkboxes, etc) and there is a postback or something like that (the screen flashes as the site redraws itself), there is now a duplicate set of MenuItems immediately after the original. Each time I click on a tab or checkbox, another set of menu items are added.
I clear the MenuItem's Items list prior to building the controls and I verify that the controls hierarchy is structurally as expected after the control construction. Yet when one of my callbacks is called, my MenuItem list magically has some items added to it. None of my other controls appear affected at all. As a hack, I can remove the duplicates manually in my menu's OnMenuItemClick event, but I'd have to do the same in any of the callbacks I receive. Obviously I'd rather prevent this from happening. This has me stumped and I haven't been able to find anything online about it. Why would one set of controls have some content duplicated, yet every other control maintain its state correctly? My code is really simple so there isn't a way to add additional menu items without also adding the views. Anyway, there are a correct number of items prior to clicking on the tab/checkbox, an additional set immediately following in the callback.
This is my first time using ASP.NET, so I'm learning as I go. :) Thanks!
My problem was that I was not testing for postback before creating the controls. The code below is working for me.
In my user control's code behind:
protected void OnInitComplete( EventArgs e )
{
if( !Page.IsPostBack )
{
CreateMyControls();
}
}

Don't post back textbox

I have a fairly complex form (user control actually) with one textbox control on it that needs to NOT post back. Is there a way to remove a control from the post? Yes, this textbox is editable.
More info: This is for a credit card processing form, so the "final" submit will post to another site's page. However, prior to this there is plenty of server-side processing that goes on. I know that I can move the the credit card number text box to another page - but this requirement came very late and I'll trying to not have to re-work a lot of things.
The easiest way would be to use an html input as opposed to an ASP TextBox. These are not accessible from code if runat="server" is not set on them.
Or use the viewstate property (http://msdn.microsoft.com/en-us/library/system.web.ui.control.enableviewstate.aspx)
So the situation is that you have a form that is rendered in the user's browser with an action pointing to a different site and you need to make sure that one of the form fields will not be sent when the form is submitted.
Sounds to me like you cannot in that case make absolutely sure that the value is not posted. There are many different possible ways to solve this using javascript (disable input, clear value, etc before submit) but if scripting is turned off I think you're out of luck.
But since you can prepare for sending the form to the other server (change action on form or enable button with PostBackUrl), I guess you could also then set the Enabled property on the textbox to false. That would mean that it can no longer be edited on the final page beforr posting to the other server. Or you could hide the textbox a (so it's not renered at all) and show the field as a label or literal instead.
But even then you still have to somehow make sure the secret value is not included in the viewstate of the form. Which it will be in case you use a label or literal. And also for a textbox that was disabled or hidden on the last postback. Normally the viewstate is just a base64 encoded string so it would be trivial to find the credit card number from there. You could probably fix this by turning off viewstate for the control in question (or even for the whole page) in the last post back to your page before setting the form up for posting to the other server.
If you cannot tell for sure which will be the last postback to your server, then I think you're out of luck without more significant changes. Sorry to be a downer. Some seemingly trivial things are just hard with Asp.Net web forms.
Maybe you could add a separate page that you populate with just the data that you need to send to the other server and have that a sort of "Confirmation page". In that page you could turn off viewstate, show all the data summarized (using labels and literals etc) and the actual data to post could be included in the form as hidden fields. Then that form would post to the other server when the user "Confirms".

How do I know what is the type of control textbox/checkbox etc in controller?

I need to be dynamically build controls like textbox, checkbox, radiobutton etc by making a Ajax request and downloading the HTML. However once enough controls are on the screen and user submits the form, I need all the controls and it's posted values. Posted values are easy to get using Non sequential indices in Asp.Net MVC. But, how do I get which control's value it is? Put simply if the form has submitted the value "Hello World". I need to be able to know from where did the Hello World. Was the textbox that submitted this value or the textarea?
I don't need anything else like ID, name etc. Just need to know the type of control whether it was texbox, textarea, select or which one.
when you dynamically build these client elements, you give them a name so they will post to the server.
just follow a naming convention like:
textarea1,textarea2...
txt1,txt2,...
then at the server, to collect the values - take all the keys that start with textarea to collect the values of textAreas...
a nicer way will be to have a model with lists for each type, and when you generate the client elements, build their names so the values will be mapped to the correct list by the ModelBinder
the syntax for these names is a bit nasty so work with client templates
I used this post by haacked when i needed to build something like this
You need know more about http get or post.

Changing the value of a DropDownList in .Net client side

Assuming that I can't modify the code-behind file for a site (it's a compiled site), I've encountered a bug of mine that can only be fully fixed with a complete recompile and redeployment. Unfortunately, we are on a strict release schedule and we can't deploy for another 11 days.
The bug is that I'm doing a check on a drop down to make sure that the value that is selected isn't "-1". However, I didn't use drp.SelectedItem.Value, I used drp.Items[0].Value. Total bonehead move on my part. The bottom line is that drp.Items[0].Value is ALWAYS -1, so they page gives an error to the user stating that they need to choose an option for that drop down. Which they really have, but my bug is not letting them continue in this process.
Because I'm an idiot.
So, I'm trying to determine if I could, client-side, replace the value of the first drp item to the actually chosen value of that drop down.
I've gotten this all to work client-side, but when the form is posted back, the value is still the value that was populated from code, meaning "-1".
I'm sure this is because the drop down is loaded and all the values are held in ViewState.
Can anyone think of a .Net friendly solution to this? I'm really hoping there is one.
Unfortunately, when browser makes the postback, all controls are recreated with default values and then update their values from viewstate and post values. And DropDownList control doesn't update ListItems' values from another collection of values. If we change ListItem's value on client side by javascript, at server side our control will contain default values in its collection of ListItem, in our case it's -1.
Best regards,
Dima.

Ways to detect changed account/no account found in ASP.NET/C#

I have an ASP.NET page where at the top of the page is a search box. There are 2 text boxes - one is an autocomplete extender for the Name on a database, and one is just inputting the ID.
The page features DetailsViews and GridViews primarily, and even when no account has been searched for, these display blank data which is not ideal. I sort of fixed this by using if (IsPostBack), encasing the elements in a placeholder and setting it to visible only if the page ispostback. But this doesn't cover if the user types in an incorrect ID.
Also, some accounts have huge amounts of data inside the GridView's. I had an issue where because I have no way of detecting when a data source's rows has changed, I end up binding whenever the page loads (Page_Load method). I've come to realise this is simply very bad - there are lots of times when the user can click various things in the page and have the page postback, and it takes an eternity to load each time I click something for accounts with lots of data.
Anyway, my question is essentially two-fold but I have a feeling the solution will be similar:
1: How can I detect when there are no accounts returned when searching, and disable the Grids/Detailsviews and show an error message?
2: How can I figure out when the user searches for another account and only rebind the grids after that has happened?
Thanks
This method is very ugly but it'll get the work done.
1) To Check whether there are no records; after the AutoComplete Extenders Webservice is called if no record is returned put some value in Session like
Session["NoData"]=true;
if Records are found then;
Session["NoData"]=false;
after the webservice is called do ajax request to check that session & on the basis of value do what you want.
2) You can achieve this also by following the above option.

Categories

Resources