Using POST or SESSION to capture data
I'm currently working on 开发者_JAVA百科web application using VB in ASP.NET.
Right now I have 1 page with panels that we are using to show/hide depending on the flow. When the user first comes in, he/she is presented with a gridview containing a list of clients. Then the user, clicks a link from a row and is presented a form where he/she can update the clients' info.
Originally, I had a HYPERLINKFIELD that put the clientId in the url, then I'd grab the url and do the code appropriately
<asp:HyperLinkField Text='<%$ Resources:Resource, ManageClient %>' DataNavigateUrlFields="CLIENT_ID" DataNavigateUrlFormatString="~/clients/manage.aspx?clientId={0}" />
Now, I'm rethinking that and wondering if it's better to use SESSIONs to grab the clientID via this instead
<asp:TemplateField Visible="false"><ItemTemplate><asp:Label runat="server" ID="hidClientId" Text='<%# Bind("CLIENT_ID")%>' Visible="false" /></ItemTemplate></asp:TemplateField>
<asp:ButtonField Text='<%$ Resources:Resource, ManageClient %>' CommandName="Manage" />
Or use the POST method, which I'm not sure how it works in .NET (but I've used it in PHP)
Any help would be greatly appreciated.
I think you should use querystrings to be honest (as in your first example). The main reason is that this will allow the back/forward/refresh buttons to work properly.
If you use a post or postback type of method then you run into the situation where a user can hit refresh and get the nasty form resubmission message. There are times when a form post makes sense, but I don't think sending a single small parameter is one of them.
Now, if you still want the functionality of a session where it remembers state, then you can still do that with querystrings. Simply pass the querystring to the page, set the session to the querystring value. Next time the page comes up without a querystring, write code that'll check if it has a session value and use that instead.
e.g.
Dim iClientID As Integer
If IsNumeric(Request.QueryString("ClientID")) Then
iClientID = CInt(Request.QueryString("ClientID"))
Session("ClientID") = iClientID
ElseIf IsNumeric(Session("ClientID")) Then
iClientID = CInt(Session("ClientID"))
End If
I'd prefer to use querystrings too. Depends if this data is sensitive or not. If it is sensitive I'd use session since cross page posting is a nightmare.
精彩评论