ASP.NET three-level databinding - asp.net

I have custom objects I am binding to using ObjectDataSource. I have three-level binding: a DropDownList (Department) that filters the next DropDownList (Category) that filters a GridView (Questions). Each ObjectDataSource binds to the previous control's SelectedValue (except the first one, of course).
Everything works fine only to the next level (Department to Category and Category to Questions). When I change the Department, the Category list gets updated correctly, but the Questions that show up are from the previously selected category.
How can I get this three-level binding to work correctly? I can't figure out whether I am missing something. If I have to, I could implement SelectedIndexChanged on the first list and manually force the update on the grid, but this is not ideal. Thanks for your help!
A bit more info: I don't have a default "select an item" option. That means that when I change the Department, the first Category is automatically selected. I was hoping the binding would be smart enough to trickle that all the way down. It was smart enough that I didn't have to do the if (!IsPostBack) { // Load data }.
I currently have implemented Department_SelectedIndexChanged() and simply done a Questions.DataSource = Questions.DataSource;. That seems to "refresh" everything properly. Is there a better way to do this?

Could you clear the grid instead until the second value is refreshed? Are you looking for an AjAX appraoch, or does this use postbacks?
There isn't an automatic solution that I'm aware of so you would need to do something like you mentioned because how else would the page know to refresh the grid?

Related

ASP.Net Grid View return to page index

I know others have had a similar issue as me, but no one so far seems to have a solution to it. Basically, I have a simple GridView with a pager and I can get the page index from it (GridView1.PageIndex etc..). But how can I retain this when a user clicks on a hyperlink bound in the Gridview to another page, update some details, then come back to the page with the GridView. So they go to page 4, click the link, update details, then go back.
I can set the value in the session and get it form the page I go to, but on databound I am checking for it and trying to set it, but it just does not work. Is there a built-in function that Microsoft thought of?
Ok, so after sucking on some caffeine I discovered that I needed to set the new Page Number on PageIndexedChanged to a session variable (best option) and on DataBinding check the session and if it exists, get the value and apply it, otherwise set it to 0 (page 1) - works a treat, hope it helps someone else.

ASP.NET how to pass a reference to a particular control

I'm generally an ASP.NET MVC guy, so the "standard" ASP.NET stuff is a little difficult for me to wrap my brain around. I've tried looking for the answer, but the keywords I'm using seem to be too generic... I get a lot of close answers, but not what I'm actually looking for.
I have a grid that is populated from a data set. One of the fields is a dropdown with 4 possible statuses. When the user selects a status, an event is fired in the codebehind to make the change in the db immediately.
There is a particular status that I need to confirm, because once it's selected, it's irreversible. Figuring out how to have the back end pop up a confirmation box was annoying, but I think I have that part done now.
The problem is, if the user confirms that the status they selected for the dropdown was intended, I need to disable any further changes to that dropdown, either by disabling the control or by removing the row altogether. With this requirement, I imagine I need to pass a reference to the specific control that fired the event back to the script, so that it can pass it through the postback, where I would need to consume it.
I have no idea how to pass a reference to the control (what can be used as a reference?) and I have no idea how to use that reference in the postback.
Any help would be greatly appreciated.
;p i was waiting for you to find my post on the issue lol.
but to put it simply, you postback to the page, all members are still available to you if you instantiated something in codebehind. if not, then use FindControl to pull them from DOM. here's the passing values stuff.
as long as you don't kill the lifecycle, you're fine: Passing dropdownlist selected value to another page's dropdown list
and here is the linkspam (full docs): How do I keep TCP/IP socket open in IIS?
probably the articles on session-state and page lifecycle will be of most use.
To prompt the user for confirm add this attribute to dropdownlist.
onchange="return confirm('Confirmation Message');"

ASP.NET dynamic controls data exchange in postback

Please excuse me for a probably low quality of this question, since I'm not a web dev, so I possibly don't now some obvious things and don't know what to Google for. I think problem must have some simple solution, but I'm struggling with it for two days now, so I feel myself pretty stupid :-).
I have a custom control which is a set of checkboxes which are added dynamically based on a property which is set in OnLoad event of a page. I have two such controls on a page and second control items should be based on items selected in first control.
The problem is, I can't figure out, how to catch on autopostback which boxes were selected in first control before second contol is constructed to pass this data to it?
Take a look at this.
http://forums.asp.net/t/1440174.aspx
Since your building them dynamically, they are not as easy to find as webforms would like to be, if you added them to the page and wired up events and such.
Your going to look at the Request.Forms list, and search thru it for any controls you want.
I believe checkboxes are like radio buttons, they only return if they are checked, which is good, cause you want to know which ones were checked.
I've used same solution as in the accepted answer for this question: Dynamically Change User Control in ASP.Net , just need to assign an unique id for each dynamically created CheckBox in custom control. Not as clean solution as I want but at least it works.
You can save the data in the ViewState, QueryString or as Session before moving to the next page and you can do modifications based on it.

ASP.net - Adding a control in code, how do I get it via FindControl()?

I create a table in code, add rows and then cells to it. In each cell there is a checkbox which has an ID I set.
I add this table to an updatepanel, and later on I want to find out which checkboxes the user has checked/unchecked.
I don't know the best way, so what I did was create a button that checks. I kept a reference to the checkboxes, and tried changing the checkedvalue in code, but this did not update my webpage. I figure it loses the reference once it is added in code.
So I tried to use Page.FindControl and passed the original ID I gave it, but it doesn't find it. I am guessing this is because it puts a load of crap before the ID before adding it to the page...
So basically, what do I do to check the if the checkboxes I added have been altered or not? A CheckedChange event didn't work either.
You may have to use a recursive FindControl

Footertemplate in gridview

I am using datatable to fill gridview.
How can i add blank row in datatable to view footertemplate even if i don't have any data ?
I am using asp.net 2005. I am using gridview control to add,edit and modify the data. i put add control at footertemplate. it's working fine but i face one problem that when ever there is no data in the grid then footertemplate is not visible. I want footertemplate will be visible all the time so that user can add data even though there is no data available.
Can any body help me out...
Thanks
I ran into this as well. If I remember correctly, there is no way to actually fix it. I worked around the issue by creating an empty data template that had the controls I needed to be able to add the data.
There are a couple tricks to doing this, and most of them are kind of "ad hoc". The route I took was to intercept my datasource and check the row count. If the row count was 0, then I went ahead and injected my own row into the dataset with a coded "blank" value. Then in the databinding event, I made sure that the empty row just put empty strings into the proper fields, allowing the footer to still render.
The other route to go would be to essentially subclass the GridView class and add a few extensions so that you can still display the header/footer when there is no data. Matt Berseth has a nice little article on how to accomplish this.

Resources