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.
Related
I have a web-application in .NET framework 4.5 with code behind C#.
I am using a RadGrid of telerik Asp.Net Ajax control. I am creating it dynamically from code behind in PageInit event and adding it to a div that is statically defined in aspx file.
Now, for each row I am having a button that is used to expand and collapse the row using JQuery script.
I also have a RadCalendar on the page and I am fetching records according to selected date range.
Now, the problem is, if I select a date range from 1st to 16 days, it works fine. If I select 1st to 30 days then also there is no problem. But now if I select 1st to 8, it gives me the error show below :
Server Error in '/' Application.
An error has occurred because a control with id 'ctl00$ContentPlaceHolder1$gvGridViewDemo$ctl00$ctl09$Detail20$ctl06$Detail10$ctl04$ctl10' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.
I am NOT using any ajax in this page, so on date selection it gets full postback. Also I had set viewstate to false, ViewstateMode to disable and ClientId = autoId for both - Button and Gridview. But I am not able to get rid of this.
Can anyone help me to get rid of this ridicules error?
P.S. I have checked the link An error has occurred because a control with id {0} could not be located or a different control is assigned to the same ID after postback and according to the comment I am having string.format() in my code. But I have commented it and still I am facing the same issue.
Resolved it by myself. I do not consider it to be the great solution I have discovered but I am answering this question that it might help someone who wants to get rid of this kind of issue and tired of finding the exact solution.
I have solved it by debugging skills with Trial and Error methodology (it requires so much patience in oneself).
What I did is, I started commenting some of the code that places dynamic controls on the page and checked for result whether the same error is generated or not? If yes, then you are commenting wrong code. So just remove the comments, make that portion as it was and move on.
At last I found that there were some dynamically generated tool-tips; I was used to display some additional details on every cell of the grid view (RadGrid), were creating this error. On PageInit I was clearing all of the controls of the division and adding a grid view by code behind. So each time the request gets initiated, it clears all of the controls and adds them newly.
However, the objects of tool-tip (I have used RadTooltip to display the tool-tip on cell) were not getting cleared from the page across the postbacks.
So I just applied a new ID to each tool-tip using its cell's (cell of grid) client id + DateTime.Now.Ticks.ToString(), and I kicked off myself from this weird error.
I see you solved it, yet this may be useful in the future if you have problems with RadTooltip, I had a couple of issues resolved with this: http://www.telerik.com/help/aspnet-ajax/tooltip-troubleshooting-common-issues.html.
I was getting this problem too. What fixed it was explicitly adding ID values to all the Control objects I was dynamically loading onto my screen.
It happens for me when I have multiple command field in my grid view. I converted them to template fields and everything worked fine!
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>
I have to set dropdownlost to read only. I can set enabled =false. But I cannot set font color to regular dropdown text. It will show like blur. The samething I did textbox.readonly=false. There we can see font same as regular text, no change.
The same way I want read only for dropdownlist. Is there any way we can do with CSS or javascript or jQuery ?.
Thanks in advance
There is varying browser support for this. Typically, Internet Explorer provides fewer opportunities to override the default greyed out look for disabled controls, than the other main browsers. I have struggled to do this in the past across all browsers. I would be interested to see some answers here that do this rather than my advice not to do it...
I would question why you are doing this. Your users will be used to the default look of disabled controls. It is a visual cue to them that the control is disabled. I would urge you not to disrupt this subtle visual que that people are very very used to.
Unless you can give a compelling reason to do this of course.
One way to hack this would be to not disable the dropdown list at all (taking care of the styling). Then, add an event handler to the change event that sets the dropdown back to the original value. You would have to store the original value, probably in an input of type hidden, and when you handle the change event, first check whether the value is the same as the stored value. If not, set it back. If you don't make this check you will get an infinite loop, because setting it back to the original value will call the change event again.
I'm trying to persist the contents of a textbox through a postback, and I've exhausted all of my effort but can't get it working right.
What should happen is:
User selects a radiobutton
Depending which button was
selection, a usercontrol is loaded
to specify some data and a viewstate
to say which enum type it's
equivalent to.
When they click
save, if the UserControl is just a
textbox input - the simplest), the
contents are read and saved, then
saved to the database with the
format(the radiobutton choice) so
they can be deserialized again
later.
The page posts back, and
the value and format are read from
the database, then the right control
is loaded.
The problem is - the first time the page posts back, it works. Every other postback it resets to the default value of the textbox.
I have a very similar setup elsewhere, so I'm thinking it might be a minor thing I'd never think of. There's a lot of code, so it might be easier to talk about what to do (load the dynamic control, populate the values etc) rather than how to do it.
There was actually a bug in my original code which meant it would never have in the way I was using the modified version. Apparently state is restored in Page_Load, so any controls need to be initialized by to have their values restored.
Creating the control in Page_PreLoad, then populating it after Page_Load solved the problem.
How can use a dropdown list without the autopostback=true.
The value on the server is not being changed according to the one selected from the client side. As I already stated I do not wish that for each dropdown I have the autopostback will trigger a post back.
Any time I have lost the value of the drop-down it is because I messed up and repopulated the drop down before handling the value change. For me, it has been drop-downs that I need to do something special with like add item attributes for Javascript, etc. This is data that needs to be added on every page load (aka data that is not persisted in the drop down like the names and values of each item). In these cases I have done this work on load, then I try to retrieve the value later in the page lifecycle and DOH!
Here is the page lifecycle:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Dollars to donuts that is what is happening. You are probably just reloading the items before you get to handling whatever postback event you are using to grab the value. If you are doing this and cannot get around this work flow, just save the selected index at the beginning of the logic that populates the drop-down, then set the selected index of the drop down with that value when done.
it'll be saved in the viewstate, so the value will be correct when you do eventually post back, and if you're really desperate to get the current value without a postback, javascript would be the way to do this.
Worst case you can grab the value right off the request object:
string selectedID = Request[DropdownControl.UniqueID];
You should make sure you are only filling the select box with options during the initial page load, and not again during the postback
if (!this.Page.IsPostBack) {
//fill select box here
}