Webform not displaying - asp.net

I have to port an application to .NET, so I decided to go with ASP Webforms.
I have my sample database (which is empty) set up and I have a few webforms (GridView, DetailsView). For a 'new' record page, I have a DetailView, but when I run it the server doesnt serve any form. I suspect it is because the tables are empty, so how do I set it up so it renders the form so I can add a New record.
Thanks, Also if anyone could point me to a good Webforms tutorial that covers the basics it would be great. Google has millions of those but most of them, including the msdn are cumbersome and get into lots of details that confuse me.

What I had to do in this case is use the <EmptyDataTemplate> Field. This happens because when the DataSet is empty, the Form doesnt renders anything unless it's specified in the EmptyDataTemplate.

Related

GridView - What is the purpose of GridViewSortEventArgs.SortDirection and DataView.Sort anyway if people use Session/Viewstate instead?

I really do not get it.
I've worked more with the ASP.NET MVC Framework but less with WebForms.
I have googled alot about how to sort on GridView but mostly it ends up using the Session or the ViewState which I do not prefer.
Instead, this how-to explains how it should work but it does not work for me.
It ends up that
after clicking on a column name and doing the postback the gridview stays in the same sorting as before.
GridViewSortEventArgs.SortDirection is always "Ascending" (there are also some threads about that behavior)
Do you have any idea how it could/should work as explained in the how-to mentioned above and without using Session/ViewState? Or maybe the how-to is just not working at all?
Kind regards
Update 1:
The question is more general and also related to the how-to, not to my implementation.
My implementation is not different than in the how-to.
Some more things to mention:
- I am forced to use the ASP.NET 3.5 framework
- In my implementation, I have created a simple GridView in a user control (.ascx file) which contains that logic in code-behind and then loaded that user control on a simple, empty page (.aspx file).
Maybe can anyone confirm if the how-to works for him?
There is no purpose for the GridViewSortEventArgs.SortDirection! It was a feature that (as far as I know) was never implemented fully by Microsoft: it always returns Ascending, all the time. Therefore, everyone has come up with their own favorite workaround for the lack of support for bi-directional gridview sorting! I know it seems strange to hear that there is a property there that simply does not get used, but that is the truth as far as I know!
Here are a lot of workarounds for the problem of why the sort direction is always ascending...
GridView sorting: SortDirection always Ascending
Personally, I prefer my own solution, which does NOT use ViewState or Session, but rather plain old vanilla hidden fields! Hidden fields are very "cheap" in my opinion, since I always turn off ViewState for all WebForm projects... Here is a link to the hidden field solution:
https://stackoverflow.com/a/25657044/796858
From what I understand, you can manipulate the underlying result set if you load your results into a DataView
http://msdn.microsoft.com/en-us/library/system.data.dataview%28v=vs.110%29.aspx
With a DataView, you can set the sort direction and then bind that DataView to the grid. The reason people use Session or ViewState is to toggle between postbacks what the user has selected as their sort.
The only other way you can track which direction the user is sorting by, that I can think of without fetching from an external data source, is by using Request.Querystring
I've never had luck with GridView sorting and have always resulted to using a DataView with a sort to get sorts to work properly.

What is all this stuff in ASP.NET HTML output?

Please see this:
VIEWSTATE" id="_VIEWSTATE" value="/wEPDwUKMTMzMTM1NTAzNA9kFgJmD2QWAgIDD2QWJgIDDw8WBB4LTmF2aWdhdGVVcmwFKGh0dHA6Ly93d3cubGVhcm5kZXZub3cuY29tL1Byb2R1Y3RzLmFzcHgeBFRleHQFCVN1YnNjcmliZWRkAgUPDxYCHwAFHWh0dHA6Ly9vbmxpbmUubGVhcm5kZXZub3cuY29tZGQCBw8PFgIfAAUnaHR0cDovL3d3dy5sZWFybmRldm5vdy5jb20vRGVmYXVsdC5hc3B4ZGQCCQ8PFgIfAAUoaHR0cDovL3d3dy5sZWFybmRldm5vdy5jb20vUHJvZHVjdHMuYXNweGRkAgsPDxYCHwAFJ2h0dHA6Ly93d3cubGVhcm5kZXZub3cuY29tL1NhbXBsZXMuYXNweGRkAg0PDxYCHwAFKWh0dHA6Ly93d3cubGVhcm5kZXZub3cuY29tL3N1YnNjcmliZS5hc3B4ZGQCDw8PFgIfAAUjaHR0cDovL3d3dy5sZWFybmRldm5vdy5jb20vRkFRLmFzcHhkZAIRDw8WAh8ABSxodHRwOi8v........ (it's 10 times bigger but I've removed it)
Is this HTML that ASP.NET produce? And does ASP.NET MVC do the same as well?
That is called ViewState.
ASP.NET MVC does not produce ViewState.
ASP.Net MVC does not produce viewstate, but that doesn't necessarily mean it's faster.
First of all, if you have that much viewstate in an ASP.Net webforms page you're probably doing something wrong. It's not that hard to avoid large viewstates in webforms.
Secondly, the things webforms puts into viewstate mostly substitutes for things mvc pages need to rebuild on each request. This tends to work in mvc's favor on public internet sites, where the bandwidth is both more expensive and slower than using additional server resources. For the private intranet on your company's LAN, it tends to favor webforms where upstream bandwidth from browser to web server is free, plentiful, and fast.
Web forms uses viewstate to maintain control state when you post back to the server. For instance, say you have a text box and a button on a web form. You type something in the text box and click the button. The button performs a post back to the server. When the page reloads, unless you cleared the text box yourself in server-side code, the text box would still contain whatever typed before you clicked the button.
What actually happened was that the data in the text box was stored in the hidden viewstate field and loaded into the text box again upon the page refreshing. The viewstate looks like a jumbled mess because it is base64 encoded. You can store things in the viewstate yourself manually also. In the page_load event of your page just do this:
this.ViewState["someVariable"] = "some value";
Now, if you did this on a blank web form, when you load the page you will see a viewstate field in the html like the one you pasted (except a lot smaller). If you copied the value from that hidden field and pasted it into a viewstate decoder (like this one) you would see someVariable with a value of "some value".
Now put a button on the form and in the button's click even do this:
Response.Write(this.ViewState["someVariable"].ToString());
You will see "some value" written out on the page. What you did was manually store something in the viewstate, it got rendered to the page in that hidden field, then that hidden field got posted back to the server and you read from it to display the data.
This is the primary functionality of web forms. MVC does not use viewstate. MVC relies on another technique known as model binding which would be out of the scope of your question. Google "asp.net mvc model binding" for more information on the way mvc maintains information across posts.
Slightly off-topic example of a viewstate joke:
I have a rather large community site called U413. I recently redesigned it to use ASP.net MVC 2.0. It's a community for programmers and they razz me all the time for using webforms. Well now that it doesn't use webforms I decided to play a little joke on them. MVC does not use viewstate but I went into a webform and generated a viewstate that contained some data and manually pasted the hidden field into my view in my MVC app.
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjkPFgIeE1U0MTNTZWNyZXRBY2Nlc3NLZXkFM2h0dHA6Ly93d3cudTQxMy5jb20vQ29udGVudC9VNDEzU2VjcmV0QWNjZXNzS2V5LmpwZ2RkpshYUvbSiUuE0YaYNBH0rvTGj4qEcquSqQeUs9ZpuIQ=" />
Already several people have gone snooping through the html source hoping to find little secrets or exploits and encountered this. They immediately decoded it and got a good laugh. Copy the value and paste it into the decoder I linked to earlier if you want to see.

Telerik RadGrid: grid clientside pagination

I have a web service which returns me some data,I am massaging this data and using this as datasource for my radgrid (telerik). The datasource is quite large, and would like to paginate it. I found couple of problems when I paginate it in the server side
I have to bind the grid again for pagination, which essentially means I have to make a call to WS again to get the data. This is an expensive call for me. I would rather forgo the benefits of pagination and would display all the results in the same page, except for it would be a bit clumsy
During the postback RadGrid1.Items.Count happens to be the number of items getting paginated (25- in my case) which is expected as all the items in the datasource are not getting bound. This of course is not an issue. The real issue is that we have some checkboxes which get checked based on some business condition. We add this to our business object/DB later. So if the user has not navigated all the pages, these "checked" items do not get added as pagination limits the "Items" in the grid to those which get bound for that particular page index.
My Thoughts:
I would rather have some sort of client side pagination, where we can hide/show contents than going to the server and doing a databind every time. Though it will return all the results, the UI will not be clumsy and the grid would have "all the items" during postback
Is there a way to do it ?
If it were a regular asp.net gridView, can someone point me to a good article which would serve my purpose
Ram
PS: who else think radgrid is crazy ? (unfortunately I did not make this choice)
Recently during my project implementation (which is built with RadControls and RadGrid in particular) I found a handy example that shows how to get grid data from web service and implement paging with a single call to that web service. Check it out:
http://demos.telerik.com/aspnet-ajax/grid/examples/client/declarativedatabinding/defaultcs.aspx
Dick
[I don't have any experience in using RadGrid as well as Telerik, so this solution may/may not work for you]
Silverlight 3 has native support for pagination, you can get more info at PagedCollectionView
BRIJ MOHAN also wrote a nice tutorial on how to leverage this class as well as other cool features like column grouping.
http://weblogs.asp.net/brijmohan/archive/2009/08/01/silverlight-3-datagrid-columns-grouping-using-pagedcollectionview.aspx

A big dilemma - ASP.NET and jQuery

I have a wizard style interface where I need to collect data from users. I've been asked by my managers that the information is to be collected in a step by step type process.
I've decided to have a page.aspx with each step of the process as a separate user control. step1.ascx step2.ascx etc...
The way it works now, is that when the initial GET request comes in, I render the entire page (which sits inside of a master page) and step1.ascx. When then next POST request comes in for step 2 (using query string step=2), I render only step2.ascx to the browser by overriding the Render(HtmlTextWriter) method and use jQuery html() method to replace the contents of a div.
The problem with this whole approach, besides being hacky (in my opinion) is that it's impossible to update viewstate as this is usually handled server side.
My workaround is to store the contents of step1.ascx into temporary session storage so if the user decides to click the Back button to go back one step, I can spit out the values that were stored for it previously.
I feel I'm putting on my techy hat on here in wanting to try the latest Javascript craze as jQuery with .NET has taken a lot of hack like approaches and reverse engineering to get right. Would it be easier to simply use an updatepanel and be done with it or is there a site with a comprehensive resource of using jQuery to do everything in ASP.NET?
Thanks for taking the time to read this.
Another approach, that might be easier to work with, is to load the entire form with the initial GET request, and then hide all sections except the first one. You then use jQuery to hide and show different parts of the form, and when the final section is shown the entire form is posted in one POST to the server. That way you can handle the input on the server just as if the data entry was done in one step by the user, and still get the step-by-step expreience on the client side.
You could just place all your user controls one after another and turn on the visibility of the current step's control and turn on other controls when appropriate . No need to mess with overriding Render(). This way the user controls' viewstate will be managed by the server. and you can focus on any step validation logic.
Using an UpdatePanel to contain the steps would give the ajax experience and still be able to provide validation on each step. If you are OK with validating multiple steps at once, Tomas Lycken's suggestion (hide/show with JQuery), would give a fast step by step experience.
Did you look into using the ASP.NET Wizard control? It's a bit of a challenge to customize the UI, but otherwise it's worked well for me in similar scenarios.

Facebook Wall functionality using ASP.Net

I want to create something similiar to a facebook wall on my social site. I want to store the posts in an sql database and that should be rather simple. What I am looking for is a good way to display the posts? I guess I don't even really know where to start, as I can only think of using a loop to display asp:textboxes. Which obviously is not correct.
I want there to be multiple posts displayed on the page, which should include:
the user who posted,
the text posted,
the date posted,
and if I want to get crazy...a means of deleting/editing the post.
I really have no idea of how to implement this concept, so any ideas would help greatly.
To get started, view this article from asp.net on the Repeater control, or this great article.
The Repeater control lets you databind to a list of objects, and then define one template for how that object should be displayed. Then, ASP.NET will handle showing it as many times as necessary. You can write the code-behind for dealing with delete and edit as if there were only one instance on the page.
go ahead with jquery, use a lot of ajax. for the mark up, use a repeater control with all clean html mark up (instead of server side controls which would generate a lot of unnecessary mark up quickly creating performance issues)
only populate x number of items on initial load, and then using jquery pull data as required based on user action. you can serve this data via json, decode json on client side using jquery and perform a loop which converts this json into appropriate html and injects it into the correct html element
should be simple ;-)
ASP.NET gives you lots of ways to do this. A Repeater, DataGrid, GridView are the first that come to mind. If you'd rather use ASP.NET MVC, there's always the good old foreach loop.
Additionally, check out ListView too.

Resources