DropDownList Setting Default Value - asp.net

How should I get a value displayed in the DropDownList when redirected through a cancel buttton. The value in the page where cancel button is there in a TextBox should be caught in the DropDownList.
This DropDownList has SelectedIndexChanged event also fired for which on selection of a country in the list we get a ListView displayed in the same page. In that ListView we have an add button which will redirect us to another page called addcountry in which we have few controls. In those controls one TextBox I am getting value through QueryString in an enabled False state. Now again I need that value displayed in the DropDownList when I click cancel button.
How can I solve this problem?

Using $_POST and $_GET Variables you should be able to retreive almost any value from a form and put it in the right place on a new page, this right place can be one of your drop down list element...
To make absolutly sure your Cancel Button will send the good value to the right page put it in an independant form that has your new page (where you have yout dropdown list) as its action, and in this seperate form put an hidden field that holds the specific value you wanna see in this dd list ...
Hope I am clear but starting with your very "fuzzy" and blur question It is hard to help you.

Related

Dropdown Selected Item Not Properly set

I have a page where The Dropdownbox (selUsers) is populated from a database on pageload.
On LoadComplete I am setting the value based on a value from the maser page.
selUsers.SelectedValue = master.intUserID
This appears to work properly, as the correct text is displayed in the dropdownbox.
I have a button that calls a sub. In that sub, hen I call selUsers.SelectedItem.Value or selUsers.SelectedValue I get the value of the first item in the dropdownbox not the selected one.
I have been reading and I am seeing that the code to set the value has to be in ispostback, but I am not posting the page back, nor do I want to. Is it truly the case that even if you set the value of the dropdown, it isn't really set until you post the page back? If that isnt the case, why am I not getting the correct value.
Before you ask, I have scoured the code, and at no place else is the dropdown being rebound, or the value being reset.
the whole page life cycle can be a tad confusing.
I'd advise you to read this (article: ASP.NET Page Life Cycle Overview) https://msdn.microsoft.com/en-us/library/ms178472.aspx
But to answer your question:
If you click that button , the following takes place in this specific order (relevant to your problem):
1) Page load event gets raised: this is where you get your data and databind, thus 'resetting' the selected value on the dropdownlist as well.
2) The button click event gets raised (this is an item event): You read out the value you want to read out
BUT since you haven't reached LoadComplete yet (where you'll set the new selected value) you'll see the first value in the list.
3) Load complete event gets raised: here you set the correct selected value.
So yes, if you click on an asp web form's button (by default), it will cause a postback and go through the whole page life cycle to build the 'response' to your action. You'll have to take this into account while building your pages.
You can see this in action while debugging: place breakpoints on those 3 points in your code behind file and then click the button.
To solve your problem, you'll need to set the selected value after you databind your dropdownlist AND before you read out the selected value.
Hope this helps.

cross postback in asp.net

I chose one row to click select button in grid view and click to submit button to go next page. Then, I want to get selection grid view id from grid view page. How I do?
I think the easiest way is to pass the selection as a query argument to the second page.
You can use the GridView OnRowCommand event to fetch the datasource bounded to that particular row. From that you can take the for that particular row. Pass this to the second page either through querystring or session .
Thanks

How to disable listbox's click event

I have a listbox which acts as a list of items. If you click on some item, it's contents are shown in the panel on the right (few textboxes etc.).
I need to have a validation on these controls as all of them are required fields. And I do have it. The problem is that, even when the validators are not valid, user can click the listbox and change active index (that doesn't have impact on the panel on the right, as SelectedIndexChanged isn't fired).
The validators are standard RequiredFieldValidator with their Display property set to "Dynamic". So, what I want is to disallow the user clicking on the listbox and changing the index untill all validators are Valid.
What would be your solution for that? Is that even possible?
Did you try setting ListBox.Enabled = false when you actually do fire off the SelectedIndexChanged, and reenabling when your required fields meet the Page.IsValid requirement to proceed in code execution?

prevent DropDownList from going to the original value when refreshing the page

I have a DropDownList on an ASP.NET master page and I want to change some values and refresh the page when I select a different item from the list. I enabled the post back property in the DropDownList but it still gets back always to the first value whenever I select. Any advice?
Please post some more code to show where you are setting the value of the dropdown (both on load and anywhere else).
Usually, this is just a case of not understanding the event model. Try setting breakpoints on all of those points where you set the value and step through the code.
The most obvious case would be if you're setting the value in the page_load event handler and not wrapping it in a check for !Page.IsPostback

Is there a way to set a asp.net button's CommandArgument in javascript?

I have a GridView that lists a bunch of items and one of the columns has a link that displays a modal (AjaxToolkit ModalPopupExtender). Let's call that link "Show". In that modal, I have a asp:button for saving the data entered in that modal. Let's call that button "Save"
So when the user clicks on a "Show" link in a certain row, I'd like write some javascript that sets something in the "Save" button, so that in my code-behind, I can handle "Save".Command and use the CommandEventArgs parameter to get the value.
Is this possible, or do I just need to use a hidden input tag and set its value?
Not a direct answer to your question, but another possible way of solving the problem:
Place a HiddenField control on the page. In your code-behind, before displaying the modal popup, set the value of that control to the ID of the row that was clicked (or the row number, or some identifying value). Then in the code-behind of your Save button, you can just read the value of the HiddenField.
Well, after continuing the research, it looks like it cannot be done. The CommandArgument property might reside in the ViewState, but for this case, it is completely server side and cannot be changed using javascript.
If you are using Updatepanel, you need to place the Hiddenfield inside the Updatepanel. Otherwise you will not be able to get/set the value stored in hiddenfield.

Resources