Get Control Value From Web User Control - asp.net

Lets say I have three DropDownList controls in a web user control and they depend on each other.
Categories
Brands
Products
Explanation:
After I choose a category from Categories dropdown list, related brands are loaded in Brands DropDownList and same happens when I choose specific brand and they are all located in a web user control since I am using it too much on different pages, I don't want to copy and paste the same code on all the pages.
Problem: The pages can contain a GridView and DataSource control which needs an additional Where parameter to fetch all the data needed in and that parameter could depend on selected product within the Products DropDownList control.
Question: So how can I get that Selected Product Value from Products DropDownList to bind it to SQLDataSource or any other DataSource control.
My Thoughts: I belive I can solve this problem in the ways following.
I can use static variable which is updated once Products selected. That field variable could be public so everyone can reach it
Selected Products DropDownList can create a QueryString Field for me to grap the selected value.
In the same way, the dropdownlist can create a Session variable on the fly and I can fetch the value
It can create a hidden field maybe.
But: Well those are some of my thoughts but I found them so naive to implement. I need something elegant and satisfying to solve this problem. It should be something like a gateway from the Web User Control to outside world.
Maybe a separate class or a Property can help me in the gateway solution.
Anyways, I am waiting for your answers.

If I'm understanding the question correctly:
You can add a property to the user control that exposes the products DDL selected value.
You can also add and raise an event from the user control that fires when the products DDL selected value changes. Creating a custom event argument that contains the product value, allows it to get passed directly to the event handler.
Then your pages can handle the event raised by the user control, have the product value, and bind the grid.

You could bind the DropDownList.SelectedIndexChanged events to the same function, and test the SelectedValue properties of each DropDownList. If their SelectedValues are valid, bind the grid to your DataSource.
I've done this in the past when I needed users to input a certain amount of data before I could query the database. I set the Hidden property on my GridView if the DropDownLists weren't valid, and reset it when they were properly bound.

Related

How to generate a dropdown list from another?

I want to generate a drop down list from another drop down list. That is I have a dropdown of countries. When selecting a country,another dropdown must come with values as states of that specific country. How to do that in asp.net using c#?
for each country you have, add a new list item to the drop down list, with text the country and the value some id of the country. On the second drop down list, set the auto post back property to true and add an event to the on selected item change. In the event code, get the selected item and by the second ddl.
Try it!
Tip: add a hidden field on the page, and on the selected item changed event from the first ddl, set the value of the hidden field, the selected value. On the page_load event, verify if the value is string.empty and if is an id in the value. If it is, bind the second ddl.
The technology you're looking for is called a Cascading Dropdown.
If you are using WebForms then Ajax Control Toolkit then this has one built in:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
Otherwise you might need to do a further search if you are using MVC.
PS I'm not particularly proud of posting a link to the Ajax Control Toolkit as its not the best library out there but it is an easy drop in for what you want. If you're serious about doing a good job then I'd search for better options for cascading dropdowns.

EntityDataSource filtered by Selectedrow of a Gridview

If I have 2 entities that share a navigation property (In my case an entity called Groups and one called Requirements) and I have a Gridview that displays Groups and another Gridview that I want to display the Requirements in the navigation property of the selected Group, should I pull in all the Requirements in the Requirements DataSource and then filter in the Gridview or should I filter in the DataSource?
I tried filtering in the Requirements DataSource by doing some Linq in the OnQueryCreated event, but I realized that OnQueryCreated is called when the page is loaded and then not again. Is there another event I should use?
Basically: Should I filter in the DataSource and if so where? If I should filter somewhere else, where?

User inserting items in a table

I have created a simple web form on ASP.NET which includes a DropDownList with the names of all the records of a table (h. PhotoAlbums) in my database. When the user selects an album, all the items of this album (photos with tootlips) appear on the page in an order. I have carried out this by the aid of ListView control assigning a LINQ source as data source.
I would like by the selection of an album to grant the user the possibility to insert new items in the selected album at the same time and on the same page besides the appearance of the already existed items in the album. I think that I should merely use a DetailsView control and adjust this, so as to insert items. Is it though possible the DetailsView to appear after the selection of an album; not when the page is loaded initially? How?
If I understand your question,
You can Initially set the visible = "false" to the DetailsView. Then in your DropDownList OnSelectedIndexChanged method you can check the selected value and set the visible= "true". In your DropDownList you should set the property AutoPostBack="true"
Hope This Helps

How do I wire up a custom C# ASP.Net WebControl to post back a custom __EVENTARGUMENT?

I'm creating a custom ASP.Net GridView and I want to be able to alter the __EVENTARGUMENT value but I can't figure out how to capture the returned value on the server side.
I'm creating the ability to have a collapsible representation, so the first level is the standard GridView and I will insert additional rows via JavaScript if they expand the first level row.
My problem is how to create a link on the selecond level rows that posts back with custom data.
I believe the RaisePostBackEvent is where this value gets passed into; the base GridView class allows you to override it, so you should be able to tap into it there.
This is a part of the IPostBackEventHandler interface.
HTH.
Use __dopostback to raise the postback from javascript. Use the correct parameters so that this will be mapped to the right event

online quiz using ASP dot NET

Hii,
I need to develop an online quiz website that would be having MCQs. I would want to have one question appearing per page with a Numeric Pager so that the user can go back and forth.
I tried using the FormView for displaying the questions and RadioButtons. I created a class QANS that would hold the answer selected by the user for the questions that he did answer so that I can later sum up the total Score.
Now, the problem I'm facing is as below:
Let the user select a RadioButton, say R, on a PageIndex, say I, and then go to some other page, say K. When the user returns back to page I, none of the RadioButtons is selected.
I tried adding code for setting the Checked property of the RadioButton true in the Page_Load(), but it didn't help. I also tried the same with the PageIndexChanged and PageIndexChanging event, but the RadioButton doesn't get checked.
The RadioButton that was selected for a particular question has been stored and I am able to print which one it was using Response.Write() but I'm not able to keep the RadioButton Checked.
How shall this be done?
You should be able to make the radio button checked in the
protected override void OnPreRender(EventArgs e)
event of the page lifecycle
Have you considered using the .Net wizard control for this?
The ASP.Net 2.0 Wizard Control
You can save the state of the selected controls in session and repopulate them after the round trip.
It's odd that it's losing state after the postback. Is there any crazy code in you on_load or pageIndexChanging or anything?
From what I understand, you have to persist the checked state of the radiobutton across the pageindexes. You can use ViewState for that as that can preserve data across postbacks for a page.
something like this:-
// Define a list of question ids attempted
List questionIdsAttempted;
//Add the List to the ViewState
if(ViewState["QuestionIdsAttempted"]!=null)
{
questionIdsAttempted= new List()
ViewState["QuestionIdsAttempted"] = questionIdsAttempted
}
// Add the question id to the list when when the user attempts a question
questionIdsAttempted.Add(qId);
// Add the list back to the viewstate
ViewState["QuestionIdsAttempted"] = questionIdsAttempted
And in the page load in checked attribute of the radiobuttons , you can call a function with the current question id and the function will get the list from the viewstate and returns true or false based on if the id exists in the list.
Ofcourse, you need to check the nullability of the viewstate in various places based on the flow of the program.
Let me know

Resources