I have 2 pages, both with 3 similar DropDownLists. The first DropDownList needs to be populated before selecting the second, and the second before the third (which then populates a gridview).
Usually, a user will view data on one page, click a button, and then view data on the next page (usually with the same DropDownList selections). Is there any way to have the dropdownlists pre-populated with their old selections?
You can pass values from one page to the other using the PreviousPage parameter that asp.net provides you. A small example:
You set the second page on the submit button as:
PostBackUrl="SecondPage.aspx"
On SecondPage.aspx you declare where you can get informations
<%# PreviousPageType VirtualPath="~/FirstPage.aspx" %>
and you get them by...
if (Page.PreviousPage != null)
{
if(Page.PreviousPage.IsCrossPagePostBack == true)
{
// and get the controls of the previous page as
var SomeVariable = PreviousPage.DropDownlListId.SelectedValue;
}
}
Some reference.
Cross-Page Posting in ASP.NET Web Pages
ASP.NET how to access public properties?
Cross-page postbacks and back again retaining data from source page
Cross-page posting. Is it a good pratice to use PreviousPage in Asp.net?
If you have the form where the button that the users click post to the second page, the second page can look for the values of the DDL and set them on page load.
There is a way:
pass selections to the second page either via Session or Query String
populate first dropdown
select appropriate item
repeat 2-3 for the rest of dropdowns
If you're looking for a magic solution that just "does that", I don't think this is possible.
p.s. You might rethink your design (if this is an option) and have the grid on the first page; bind the grid when user selects a value from the last dropdown (make sure you have AutoPostback set to True on your last dropdown in order to trigger the final postback)
Related
I have a page for searching when I write my name for example in textbox it will search and display the results in a GridView. I'm using an EntityDataSource with a QueryExtender so I didn't write any code in C#.
The problem is: in the GridView I have a hyperlink when I click it, it will go to another page, ok fine then when I return to the previous page the GridView does not show the results of the search when I was left because of the postback (update panel) it show the whole data from EntityDataSource.
I think I have to use session but I don't know how I can do it, I mean how I can save the GridView in session and how I can retrieve it in page_load.
i want that after clicking a button saves the index i have selected in order to storage that number to my database. My drop-down-list is in a comment below.
The drop-down-list is filled by using a query. And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index.
I have done some research, and so far i cannot solve it, hope someone can help me out.
Code for Dropdown list:-
<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList>
here's how i fill the drop-down-list
ClsUtil clsutil = new ClsUtil();
DataSet ds = clsutilerias.FillDDL(Category);
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString();
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0];
ddlCategory.DataBind();
The DropDownList has properties. You need to set SelectedIndex, SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera).
Edit: Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndexperhaps as much as get the SelectedIndex. Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do.
Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.)
It seems that you found your solution in checking IsPostback in the Page, and not re-populate the list if the value is true. That's good. If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution.
Another option, of course, would be to read the posted data in any event that happens before you do the data-binding. You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again, if you don't like having ASP.NET doing it for you.
When you are clicking the button, the entire page is refreshing that why you got the first index all the time
The best solution is :
if(IsPostBack == false)
{
//Save your data code here
}
You have other tricky solution which is :
put the dropboxmenu inside an update panel control
While binding datasource to combo, mention DataMember and DataValue property for combo box so that selecting elements from combo you get selectedValue as DataValue.
Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. so i just added:
if(!IsPostBack)
{
//here goes the code to fill the Drop down list
}
Sorry if the title is a little confusing, but basically my questions is this.
I am on Page A, Page A contains a search which filters data in a Webgrid. The webgrid has ten rows per page. I've searched for something in the filter, and the webgrid has now returned 27 rows of data, as there are 27 rows, there are now three pages displayed on my webgrid.
Now comes the bit I need help on, at the moment, I click a link in the webgrid and I'm moved away from the page onto Page B, at this point the page model is saved in the Session, so when I navigate back to Page A using a link, the webgrid still displays the same 27 rows I had when I initially searched. However, if I was on page 3 of the grid before on Page A, when I am returned to Page A from Page B, I am automatically back onto page 1.
How do I save the page number on the webgrid and enter it as an argument so that when I return to Page A from Page B, it takes me to page 3, not page 1 again.
Thanks in advance,
Yes, as long as the webgrid posts back to a controller every time it pages, you could capture the current page number in session, and then when you come back, you can reload to where you need to go. Add this to your model:
public int PageNumber { get; set; }
In your controller, set the PageNumber to 1 for default, or the value stored in session when saved. This value gets passed to the view, which you can then pass to the webgrid, which takes a page number to show.
I have view with 2 nested dropdown lists that filters data from database and show filtered result in a table.
In that table, i have an link to another view that shows details for selected database record.
Is it possible (and how) to keep selected values of master view dropdowns after user click on a "back" button in browser when viewing details view?
you should use viewstate
dont use session it is heavy weight object so take more loading on server
Why not put it in a Session
Request.Session["Value1"] = "Your value"
I am having 2 web formsand will have a drop down list on those web forms. If i select a value from drop down and click on ok i will get tranfer to next page. In that page i will have a drop down with the same values in the previous form . What i need is i would like to disable the selected value in the previos form and would like to display the remaining normal
I dont know your exact scenario but i would suggest that you look at whether you can maybe achieve your needs using a single form instead of two.
If that is not an option :-
If you are POSTING the form on click of OK, you can retrieve the value of the original page using PreviousPage. Refer eg below.
I am not quite sure what you mean by "disable the selected value in the previos form and would like to display the remaining normal" but once you retrieve this value, then you can manipulate the data in your current page any way you want.
DropDownList oldValue = (DropDownList)PreviousPage.FindControl("DropDownOldValue");
oldValue.SelectedValue - should then give you the value selected on the previous page
Got the answer
DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");
string str = oldvalue.SelectedValue.ToString();
ListItem i = DropDownList1.Items.FindByValue(str);
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");