Enable/disable TextBox on FormView.ModeChanged - c#

I have DropDownList and a TextBox in the EditTemplate of my FormView. All I want is to enable/disable the TextBox based on whether the first entry of my DropDownList is selected:
When the FormView is switched to Edit mode by the user
When user changes the selected item of the DropDownList during Edit mode.
I have achieved the second one through JS and that's working fine, but the first one is proving too difficult. I've tried to do this in ModeChanged event of the FormView, but for some reason the following call returns null in the event:
MyFormView.FindControl("MyDropDownListID");
What am I missing here?
(I'm making sure that MyFormView.CurrentMode is FormViewMode.Edit before making the above call)

One of those times when you pull your hair for hours with a problem, then post it on SO and find the solution in the next few minutes. Anyone else trapped into this, the problem is that the controls of a databound FormView aren't created yet at the time of ModeChanged or Page_Load. You must call the above line in DataBound event and it will work fine.

Related

TextBox behavior on postback

I am returning to ASP.NET after a long hiatus, and am sure I'm doing something simple wrong, but I can't seem to sort it out. I have a page with a few controls (a few Literals and one TextBox), the values of which I am populating from a database query in Page_Load. When the value of the TextBox is changed, it correctly does a postback and fires the TextBox_TextChanged method, but it also seems to re-execute Page_Load, so the new value entered by the user is overwritten with the original value from the database. The only way I seem to be able to find to prevent this is to wrap the line that sets the control value in an if (!this.IsPostBack), which works fine, but I'm curious if there is a more elegant way to do this.
Thank you in advance,
Steve
What do you mean, "When the value of the TextBox is changed, it correctly does a postback and fires the TextBox_TextChanged method?" What causes the postback?
Beware that the TextChanged method doesn't work like JS (or similar) -- no check is done on each keystroke. It actually only fires on a full postback and the text is then checked for changes.
Otherwise, you've answered your own question by checking for a postback.

What is the best way to persist the usercontrol values without having to postback or to write server-side code?

Scenario: I am having a user control which has two drop down lists. The values of the second drop down is fetched and populated depending on the selection made in the first. So, the first drop down makes a postback and pulls the data for the second. The second dropdown, therefore, does not need any postbacks on any selection changes.
This user control is being used on a page in a gridview, and therefore it repeats several times depending on the number of records in the grid.
Problem: When I make a selection in any of the dropdowns, my previous selections in all the user control dropdowns are lost and reset to the first item in the dropdown list.
Solution that works for me: I am able to fix this behavior by setting the selections made in viewstate on SelectedIndexChanged event for the respective DropDownList. But I want to avoid the postback being made by the second dropdown as it does not do anything related to UI (changing or pulling additional data to update on screen) and from the user perspective, its a waste of time and thus irritating.
I have also tried using javascript to write the values I want to persist into hidden fields, but the hidden field values are empty when the page is refreshed.
So, What is the best way to persist the dropdown values without having to postback or to write server-side code?
Thanks in advance.
#Aniruddha , I guess you are filling the first dropdown at the page load of UserControl so write that code in !IsPostBack constraint, i.e.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
// Write your existing drop down bind or other code here.
}
}
Aniruddha you are probably binding you grid in the Page_Load and so in this case bind the grid under constraint of !IsPostBack as above
Hope this will help you !!

How to get the information from a dynamically created TableRow C#/Asp.Net?

Alright so in the project I am working on I am making a hard coded asp:panel on the page and then filling it with:
A Drop Down list
An asp:Table, with an asp:TableHeaderRow
Add and Cancel Buttons
When the selectedIndexChanged event happens with the DropDownList the tablerow gets created then filled with 6 or so tablecells which then get asp:Textboxes placed inside of them.
Eventually they all get added to the tablerow which then gets added to the tablerowcollection.
That all works just fine,but when I go to add the information from these dynamically created TableRows with a foreach loop they are no where to be found but the TableHeaderRow is.
Also when I click add and let it go through the process I get no null exceptions and the only thing left after it is the header, all my rows disappear.
Anyways I feel that I am just missing something really dumb. I've looked through a lot of forums, posts, MSDN and never found an answer to my problem. Any help would greatly be appreciated!
You need to add your dynamic rows to your Table in Page_Init not Page_Load. If you are creating it in Page_Load your new rows won't be added to ViewState and you will see this problem.
Unfortunately you are adding them in response to a server side event which can get tricky. You still need to add the rows in Page_Init but when the page posts back and you at in Page_Init then the selectedIndexChanged event hasn't yet fired. It's too early in the page lifecycle.
If you want to know if it has fired at Page_Init the only way I have found is by examining Request.Form("__EVENTTARGET") collection at that point. This contains the control ID of the control that has triggered the postback - in your case this will be the dropdown list. The control that fired the event will be there but the ID will be qualified i.e not MyControID but ctl_MasterPageContentHolderID_NamingContainer1_MyControlID` or the like - so you will have to take it into account when looking for it. Once you have identified that that event has fired then you can add the rows. Once they are added there then they won't disappear.
As I say it is tricky to get working but I have done this successfully in the past. Generally dynamic controls can be very hard to work with for just this reason. You may want to consider alternatives. Best of luck with it anyway though.

using autopostback on dropdownlist that also has submit button

Have a dropdown list with autopostback set to 'yes' have another dropdown list box that will be populated based on the selection of the first dropdown. It works fine until I put a submit botton on the form. When I do it appears to not do the auto postback until the submit button is pushed. Can you have a submit button on a form that has a dropdown with autopostback active. thanks...
Sure you can.
Are you handling the autopostback event of the first dropdown in the code-behind file to populate the second dropdown? If so, your postback event should be firing when you change your selection in the first dropdown. Put a breakpoint in your code to assure that your event code is being called.
Sure you can. Are you seeing it not load the second dropdown? If so, check where you're handling that, as it may be in the wrong spot (if you're doing it with code) or you might have it wrapped in a !postback without thinking about it. Post more code and explanation for more help.

dynamic construction of checkboxes in a asp:table problem with events

I'm currently dynamically building an asp:table which contains checkboxes
these checkboxes have a CheckChanged event as an auto postback.
v_articleCheckBox.CheckedChanged += new EventHandler(v_articleCheckBox_CheckedChanged);
I am storing the checkbox states in the viewstate and I am rebuilding the table every page load so that the viewstate may then be re applied to the check boxes.
For every checkbox but the first this works perfectly.
The odd case is that the first checkbox works on the check event but simply does not call the v_articleCheckBox_CheckedChanged method on the uncheck event
Does anyone have an idea?
I was thinking of adding a dummy checkbox as a first entry in the table and then simply hide it from the client. but this seems to be a bad workaround.
I'm curious: what was the issue? –
splattne (Nov 27 at 11:39)
I am curious too – JohnIdol (Nov 27 at
13:48)
I resolved the issue by modifying my logic so that some events were fired on a more generic basis and I simply avoided the issue.
I have no idea about the why or how this was occurring; I simply found a work around that suited my needs.

Categories

Resources