gridview sorting - asp.net

I have a gridview that I load from a linq query.
MyGridData is a the list that's returned from the linq query and that contains a variable called MyVariable. So, in the code behind I have:
MyGrid.DataSource = MyGridData;
MyGrid.DataBind();
I then went to the aspx source and added AllowSorting = true and to the boundfield I want to enable sorting for, I added SortExpression = "MyVariable".
When the page renders, if I click on the header of the column to sort the grid, I get a yellow death screen with this message:
The GridView 'MyGrid' fired event Sorting which wasn't handled
What am I doing wrong?
Thanks.

You have to implement the handler for the Sorting-Event in the Codebehind. Sort your datasource and bind your grid again.

Related

ModelBinding - Trigger gridview selectmethod with no parameter with the button click event

I have an aspx page with two ascx controls. The first ascx control contains the textbox and dropdown to allow the user to select the filter parameter. The 2nd control has a gridview to display the query results. The cs file has the gridview select method to call the get data. I notice that upon initial load, the gridview select method gets called. If any of the filter controls is selected, the gridview select method is also called.
Here is the problem, after the initial load, if I click on the search button with no selected filter parameter, the gridview select method does not get called. There is no direct call to the gridview select method. It is specified in the gridview markup language as follow.
I am using modelbinding in my gridview and have the selectmethod declared in my markup aspx file.
My question, how can I get the gridview select method to get call when I click on the button with no selected filtered parameter. Yes, I would like it to do another fetch even if the user makes no selection.
Thanks for any help.
<asp:GridView ID="gvCases" runat="server"
ItemType="OFAC.FOIA.BusinessEntities.NHObjects.VW_INBOX"
DataKeyNames="FOIA_Number"
AutoGenerateColumns="false"
SelectMethod="gvCases_GetData"
OnPreRender="gvCases_PreRender"
Width="100%">
I'd love to know the real answer to this question, but what I ended up doing was to add a HiddenField on the page that the SelectMethod pulled in with a Control Value Provider and then just change the value whenever I wanted to force a refresh. In my case I used a GUID so I wouldn't have to pull back the value, convert it and increment it.
protected void ForceGridViewRefresh()
{
ForceRefresh.Value = Guid.NewGuid().ToString();
}
On my select method:
public IEnumerable<TestViewModel> GetData([Control("ForceRefresh")] Guid? forceRefresh)
{
...
}
So far, this seems to be "working".
you can use GridID.DataBind() and it would immediately call the GridID_GetData() method.

dropdownlist is always null in oneditcommand in datalist

I'm new to asp.net trying to use datalist control.
on edititemtemplate section i have put up a dropdownlist control. It has values in it. What I want is to select a value in edit mode but when I go to event (as set in oneditcommand ) my codes cannot find dropdownlist though its in edititemtemplate. Please check my codes below
DropDownList ddl = e.Item.FindControl("ddlType") as DropDownList;
ddl.Items.FindByValue((e.Item.FindControl("lblType") as Label).Text).Selected = true;
What I'm doing wrong?
thanks
-Navi
take a look at this link. Typically, you access controls like this at runtime by handling either the DataList's ItemCreated or ItemDataBound event.

Find FooterTemplate in asp.repeater

I want to give a value to a control in asp.net repeater footer template.
Dim FooterTemplate As Control = TicketRepeater.Controls(TicketRepeater.Controls.Count - 1).Controls(0)
Dim lblFooter As Literal = TryCast(FooterTemplate.FindControl("TicketTotalNumberOfTickets"), Literal)
lblFooter.Text = TicketDataTable.Rows.Count.ToString()
Return with error:
Specified argument was out of the range of valid values.
Parameter name: index
AFAIU it cant find count of controls = no controls found?. Any suggestions?
Check points:
all ID's are correct
repeater has 3 part headertemplate,
itemtemplate, footertemplate
Help!
Update
The code actually work after databind(). Any ideas how to make it work before databind event?
It can't be done before databind. Until you bind that repeater to something, it's just an empty shell and has no controls. Binding is what creates the header, footer, and items.
Binding would wipe away anything to do with the footer, anyway, so if you intend to bind to that repeater (and why else use one?) there's no point in trying to manipulate it before you bind to it.
If what you want is to have an empty repeater with header and footer and later (perhaps after some action by the user) bind to a populated data source, you can try binding to an empty (but not null) data source. I'm not 100% sure, but I think that should produce a header and footer in your repeater but no items.

Getting the selected row from a ASP.Net FormView bound to ObjectDataSource in the ItemUpdating event

I have to maintain an ASP.net application in VB.Net.
There is a page with a FormView bound to a ObjectDataSource.
I have to add some business logic on the ItemUpdating event of this FormView.
Unfortunately, some the data that I need to add this business logic is not exposed on the FormView user-interface itself, so I can not use FindControl to get the values (I could add the controls, bind them to the fields I need and set their visible property to true, but that's ugly).
So, what I would need to do is to get access to the Data Row corresponding to the currently selected item in the FormView from the code behind as it has the data I need to add my business logic code.
Unfortunately, I don't manage to get access to the row.
Thanks in advance for your help.
Try this:
Dim myData As Object = DirectCast(formview1.DataItem, DataRowView)("MyColumn")
EDIT:
If I remember correctly the DataItem is Nothing on ItemUpdating so my solution above does not work, does it? Then you have to load it from your Datasource with the given ID(CommandArgument).
Thanks, I managed to sort it using the Select method of the ObjectDataSource object.
This returned me a DataView containing the row that was currently being edited.

Issue trying to bind rss feed to repeater

I am trying to bind a datatable to a repeater. To do this, according to a code sample, I need to call dataview() when choosing the datasource property of the repeater and then write databind().
When I do this, I still get the exception that the source to databind to is not derived from IDatasourceControl.
What is the correct way to bind a datatable to a repeater? I want each record to repeat itself in the repeater (obviously).
I have seen this link (http://blogs.x2line.com/al/archive/2008/06/21/3469.aspx). What exactly does Container.Dateitem in the expression on the ASPX page mean?
Thanks
With a repeater you should be able to just do the following:
rpYourRepeater.DataSource = dtYourDataTable;
rpYourRepeater.DataBind();
That's it.
Just verify that your DataSourceID is tied to a real field in your datatable or just leave the DataSourceID out completely as you don't really need it depending on what you are doing.
You don't need any of that dataview stuff unless you want to start putting filters on your data.
I'm guessing that you are attempting to bind to DataSourceID. You should bind to DataSource in your repeater.

Resources