ASP.NET Database searching using dropdown list with paging - asp.net

I created ASP.NET MVC Web App. I've got a question about searching in database using dropdown list with paging. I used this tutorial:
tutorial, so my code is the same.
And I also created dropdown list in View:
Search: #Html.DropDownList("type", ViewBag.currentType as string)
I tried to do it the same way as it was done in tutorial, but searching works fine only until I go to the second page. When page is changed, searching is reset to default, first type.
Creating my list:
var typeList = new List<string>();
var type = from o in db.Typies
orderby o.Name
select o.Name;
typeList.Add("all");
typeList.AddRange(type.Distinct());
ViewBag.type = new SelectList(typeList);
I can easily get selected item, but have no idea how to make it selected after page is changed. So, my question: How can I restore/set selected dropdownlist item when page is changed?

Problem solved. I figured out I can manage selected dropdownlist value between pages by saving it as a Session parameter and check that parameter on every page :)

Related

Create new view through code

Is it possible to add a new view from code that can be executed on a button click? In stack overflow, a new view is generated when we post a question. Is is possible to do this in ASP.NET MVC5?
It is not possible to create a new view from code. But why do you need it. I guess to provide a different URL to each table entry so that it indexes better. For that you can change the URL of your details view.

how to show search data after postback function call in asp.net

gridview details show exactly what as i want even search data also show fine but problem is when i post back page and return on search page search is not maintain in gridview i use gridview.Databind() function on page load use (!page.postback) function but then also not useful for me so please can you all give me any code for that
on page load i write but not useful for me
if(!page.postback)
{
sda = new SqlDataAdapter("Select Name,ContactNo,Address from gridviewtable",con);
ds = new DataSet();
sda.Fill(ds);
gvd_detail.DataSource = ds;
gvd_detail.DataBind();
}
Here are some solution :
1- After every user search , Redirect user to page with parameter( like query string) and then on that page retrieve parameter and show appropriate result
2-Asp.net pages by default maintain page state by "View-state" , First make sure ViewState property of the page is True , Then make sure in page life cycle where you have rebind gridview
3-If there is situation in which #2 solution didn't work , There are another solution and that is saving page state in ViewState page explicitly
Hope these help

ASP.NET MVC 2 - Retrieving the selected value from a dropdownlist and a listbox

I'm new to ASP.NET MVC and I'm using the example outlined in Steven Sanderson's book to create a multi-page, wizard like form for a web application.
(See http://books.google.com/books?id=lfPkn31fpNQC&pg=PA477#v=onepage&q&f=false for the exact example).
I have it working to the point where I can persist data across pages, but I have no idea how to do this for a DropDownList control or a ListBox control.
Can anyone show me how to do this?
Thanks.
Quite simply the dropdown list is an HTML select control, it's value will be in the form values posted to the action so if you set your form to post to an action then in the aciton you need:
public ActionResult RecieveForm(FormCollection values)
{
var dropdownSelectedvalue = values["nameofdropdown"];
...
work with result
...
return View()
}

I have a need to give the users the ability to update a dropdown and apply that to their submission

I have an application that I am writing in ASP.NET MVC 2 following the Nerd Dinner tutorial. I am very much a beginner and was looking for examples/tutorials I could follow that would enable me to learn how to code the following scenario:
A user has the option to select an option from a dropdown.
If the option is not there then they can enter a new option and add it to the database and list in the dropdown.
I would like this to be done without the user leaving the page and what they have entered so far.
I am using a simple Entity Framework 4.0 model which I have built a repository on top of so I have methods I can call to save the filled in user information.
If the entry already exists in the database then I would like to offer the user the chance to either select that entry or to continue adding the entry they request because it can be a list of names and of course you can have more than one person with the same name.
I have implemeted this and it follows the following workflow:
1) Provide a button next to the select list to add items
2) Populate the drop-downlist when the page loads
3) When the add button is clicked, implement the UI as you like, we use a jquery dialog box.
4) Post the value to a view (via jQuery Ajax) The view should return a JsonResult
5) Check to see if the item exists, get existing id if exists or add and get new id
6) Returns a JsonResult that contains the new list and the id
7) In the reply to the ajax post, repopulate the select list using jquery and select the item.
HTH

Postback with Modified Query String from Dropdown in ASP.NET

My asp.net page will render different controls based on which report a user has selected e.g. some reports require 5 drop downs, some two checkboxes and 6 dropdowns).
They can select a report using two methods. With SelectedReport=MyReport in the query string, or by selecting it from a dropdown. And it's a common case for them to come to the page with SelectedReport in the query string, and then change the report selected in the drop down.
My question is, is there anyway of making the dropdown modify the query string when it's selected. So I'd want SelectedReport=MyNewReport in the query string and the page to post back.
At the moment it's just doing a normal postback, which leaves the SelectedReport=MyReport in the query string, even if it's not the currently selected report.
Edit: And I also need to preserve ViewState.
I've tried doing Server.Transfer(Request.Path + "?SelectedReport=" + SelectedReport, true) in the event handler for the Dropdown, and this works function wise, unfortunately because it's a Server.Transfer (to preserve ViewState) instead of a Response.Redirect the URL lags behind what's shown.
Maybe I'm asking the impossible or going about it completely the wrong way.
#Craig The QueryString collection is read-only and cannot be modified.
#Jason That would be great, except I'd lose the ViewState wouldn't I? (Sorry I added that after seeing your response).
You need to turn off autopostback on the dropdown - then, you need to hook up some javascript code that will take over that role - in the event handler code for the onchange event for the dropdown, you would create a URL based on the currently-selected value from the dropdown and use javascript to then request that page.
EDIT: Here is some quick and dirty code that is indicative of what would do the trick:
<script>
function changeReport(dropDownList) {
var selectedReport = dropDownList.options[dropDownList.selectedIndex];
window.location = ("scratch.htm?SelectedReport=" + selectedReport.value);
}
</script>
<select id="SelectedReport" onchange="changeReport(this)">
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="baz">baz</option>
</select>
Obviously you would need to do a bit more, but this does work and would give you what it seems you are after. I would recommend using a JavaScript toolkit (I use MochiKit, but it isn't for everyone) to get some of the harder work done - use unobtrusive JavaScript techniques if at all possible (unlike what I use in this example).
#Ray: You use ViewState?! I'm so sorry. :P Why, in this instance, do you need to preserve it. pray tell?
If it's an automatic post when the data changes then you should be able to redirect to the new query string with a server side handler of the dropdown's 'onchange' event. If it's a button, handle server side in the click event. I'd post a sample of what I'm talking about but I'm on the way out to pick up the kids.
Have you tried to modify the Request.QueryString[] on the SelectedIndexChanged for the DropDown? That should do the trick.
You could populate your dropdown based on the querystring on non-postbacks, then always use the value from the dropdown. That way the user's first visit to the page will be based on the querystring and subsequent changes they make to the dropdown will change the selected report.
The view state only lasts for multiple requests for the same page. Changing the query string in the URL is requesting a new page, thus clearing the view state.
Is it possible to remove the reliance on the view state by adding more query string parameters? You can then build a new URL and Response.Redirect to it.
Another option is to use the Action property on the form to clear the query string so at least the query string does not contradict what's displayed on the page after the user selects a different report.
Form.Action = Request.Path;
You can use the following function to modify the querystring on postback in asp.net using the Webresource.axd script as below.
var url = updateQueryStringParameter(window.location.href,
'Search',
document.getElementById('txtSearch').value);
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("searchbutton", "",
true, "aa", url, false, true));

Resources