Вы находитесь на странице: 1из 67

Copyright

tutorialspoint.c
om
ASP.NET
-
Managin
g State
Advertisements
HTTP ( Hyper
Text Transfer
Protocol) is a
stateless
protocol. When
the client
disconnects from
the server,the
ASP.Net engine
discards the
page objects.
This way each
web application
can scale up to
servenumerous
requests
simultaneously
without running
out of server
memory.Howeve
r, there need to
be some
technique to
store the
information
between
requests and to
retrieve itwhen
required. This
information i.e.,
the current value
of all the
controls and
variables for the
currentuser in
the current
session is called
the
State.ASP.Net
manages
four types
of state:View St
ate1.Control
State2.Session
State3.Applicatio
n State4.
View
State:
The View State
is the state of
the page and all
its controls. It is
automatically
maintained
across posts
bythe ASP.Net
framework.When
a page is sent
back to the
client, the
changes in the
properties of the
page and its
controls
aredetermined
and stored in the
value of a
hidden input
field named
_VIEWSTATE.
When the page
is againpost
back the
_VIEWSTATE
field is sent to
the server with
the HTTP
request.The view
state could be
enabled or
disabled for:
The entire
application -
by setting the
EnableViewState
property in the
<pages> section
of web.config
file
A page -
by setting the
EnableViewState
attribute of the
Page directive,
as <%@
PageLanguage="
C#"
EnableViewState
="false" %>
A control -
by setting the
Control.EnableVi
ewState
property.It is
implemented
using a view
state object
defined by the
StateBag class
which defines a
collection of view
state items. The
state bag is a
data structure
containing
attribute/value
pairs, stored as
stringsassociated
with objects.The
StateBag class
has the following
properties:
P r o p e
r t i e s
D e s c r
i p t i o
n
Item(name)The
value of the view
state item with
the specified
name. This is
thedefault
property of the
StateBag
classC o u n t T
h e n u m b e r
o f i t e m s i
n t h e v i e w
s t a t e c o l l
e c t i o n K e y s
C o l l e c t i o n
o f k e y s f o
r a l l t h e i t
e m s i n t h e
c o l l e c t i o n
AS P . NE T -
S t a t e
Ma n a g e me n t h t
t p : / / www. t u t o
r i a l s p o i n t . c o m
/ c g i -
b i n / p r i n t v e r s i
o n . c g i ? t u t o r i a
l = a s p . n e t &f . . .
1
o f
7
1 0
/ 8
/ 2
0 1
3
1 1
: 0
2
P M


V a l u e s C o l l
e c t i o n o f v
a l u e s f o r a
l l t h e i t e m
s i n t h e c o
l l e c t i o n The
StateBag class
has the following
methods
M e t h
o d s D
e s c r
i p t i
o n
Add(name, va
l ue)Adds
an i tem to
the vi ew
state col l ecti o
n and exi sti ng
i tem i s updat
edC l e a r R e
m o v e s a l l
t h e i t e m s
f r o m t h e
c o l l e c t i o n
Equal s(Obj ect
)Determi nes
whether the s
peci f i ed obj ec
t i s equal
to the current
obj ect. F i n a l i
z e Al l o ws i t
t o f r e e r e s o
u r c e s a n d p
e r f o r m o t h e
r c l e a n u p o
p e r a t i o n s . Ge
tEnumeratorRetu
rns an
enumerator that
iterates over
all the key/value
pairs
of theStateItem
objects
stored in the
StateBag
object.G e t T y
p e G e t s t h
e T y p e o f
t h e c u r r e n
t i n s t a n c e
. IsItemDirtyChe
cks a StateItem
object
stored in the
StateBag object
to evaluatewhet
her it has been
modified.R e mo
v e ( n a me ) R
e mo v e s t h e
s p e c i f i e d i
t e m. SetDirtyS
ets the state of
the StateBag
object as well as
the Dirty
property of each
of the StateItem
objects
contained by
it.SetItemDirtySe
ts the Dirty
property for the
specified
StateItem object
in theStateBag
object.T o S t r i n
g R e t u r n s a
S t r i n g r e p r
e s e n t i n g t h
e s t a t e b a g
o b j e c t .
Example:
The following
example
demonstrates
the concept of
storing view
state. Let us
keep a counter,
which
isincremented
each time the
page is post
back by clicking
a button on the
page. A label
control shows
thevalue in the
counter.The
markup file:
<%@ Page
Language="C#"A
utoEventWireup
="true"CodeBeh
ind="Default.a
spx.cs"Inherit
s="statedemo._
Default"
%><!DOCTYPE
html PUBLIC "-
//W3C//DTD
XHTML 1.0
Transitional//
EN""http://www
.w3.org/TR/xht
ml1/DTD/xhtml1
-
transitional.d
td"><html
xmlns="http://
www.w3.org/199
9/xhtml"
><head
runat="server"
><title>Untitl
ed
Page</title></
head><body><fo
rm id="form1"
runat="server"
><div><h3>View
State
demo</h3>Page
Counter:<asp:L
abel
ID="lblCounter
"
runat="server"
/><asp:Button
ID="btnIncreme
nt"
runat="server"
Text="Add
Count"onclick=
"btnIncrement_
Click"
/></div></form
>
AS P . NE T -
S t a t e
Ma n a g e me n t h t
t p : / / www. t u t o
r i a l s p o i n t . c o m
/ c g i -
b i n / p r i n t v e r s i
o n . c g i ? t u t o r i a
l = a s p . n e t &f . . .
2
o f
7
1 0
/ 8
/ 2
0 1
3
1 1
: 0
2
P M


</body></html>
The code behind
file for the
example is
shown here:
public partial
class _Default
:
System.Web.UI.
Page{public
int
counter{get{if
(ViewState["pc
ounter"] !=
null){return
((int)ViewStat
e["pcounter"])
;}else{return
0;}}set{ViewSt
ate["pcounter"
] =
value;}} prote
cted void
Page_Load(obje
ct sender,
EventArgs
e){lblCounter.
Text =
counter.ToStri
ng();counter++
;}}
This would
produce
following result:
Control S
tate:
Control state
cannot be
modified,
accessed directly
or disabled.
Session
State:
When a user
connects to an
ASP.Net website,
a new session
object is
created. When
session state
isturned on, a
new session
state object is
created for each
new request.
This session
state object
becomespart of
the context and
it is available
through the
page.Session
state is generally
used for storing
application data
like inventory or
supplier list, or a
customerrecord
or shopping cart.
It can also keep
information
about the user
and his
preference and
keep track
of pending
operations.Sessi
ons are
identified and
tracked with a
120-bit
SessionID, which
is passed from
client to server
andback as
cookie or a
modified URL.
The SessionID
is globally
unique and
random.
AS P . NE T -
S t a t e
Ma n a g e me n t h t
t p : / / www. t u t o
r i a l s p o i n t . c o m
/ c g i -
b i n / p r i n t v e r s i
o n . c g i ? t u t o r i a
l = a s p . n e t &f . . .
3
o f
7
1 0
/ 8
/ 2
0 1
3
1 1
: 0
2
P M





How do I Convert Word Doc to PDF u ...
5 Microsoft Dynamics CRM Online Sp ...
Aiming high - How to reach the top

BLOG

4 12,223
How to remove a field from the list view in
SharePoint 2010 using Client Object Model
By Vijai Anand in Blogs | SharePoint 2010 on Feb 13, 2012
In this blog you will see how to remove a field from the list view in SharePoint 2010 using
Client Object Model

inShare


3047
0
0








Description:

I have a list named "CustomList" which contains an item called "All Items". In this view i have
the folloowing fields.





In this you will see how to remove a field named "Calculated" from the view using Client
Object Model.


Code:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;

namespace COM
{
class Program
{
static void Main(string[] args)
{
// siteURL is the string that contains the site URL
string siteUrl = "http://serverName:50000/sites/Testing";
// ClientContext object is used to get the context for the SharePoint
objects
ClientContext clientContext = new ClientContext(siteUrl);
Web web = clientContext.Web;
List list = web.Lists.GetByTitle("CustomList");
View view = list.Views.GetByTitle("All Items");
ViewFieldCollection viewFields = view.ViewFields;
viewFields.Remove("Calculated");
view.Update();
clientContext.ExecuteQuery();

}
}
}


Summary:

Thus in this blog you have see n how to remove a particular field from the list view in
SharePoint 2010 using Client Object Model.

RELATED BLOGS
Get all the fields for the list view in SharePoint 2010 using Client Object Model
How to add a field to the list view in SharePoint 2010 using Client Object Model
Set the default value for the list field in SharePoint 2010 using Client Object Model
Make the list field read only in SharePoint 2010 using Client Object Model
Delete the list field in SharePoint 2010 using Client Object Model
Get all the views for the SharePoint 2010 list using Client Object Model
COMMENTS


Removing options from a List Box in
client side Javascript
We can remove the options from a list box by using client side JavaScript.
You can see the article explaining how to add options to a list box using
JavaScript. Here we will discuss on how to remove option on selection and
removing all the options in one go. We will use different functions and
connect them to buttons to execute them. Here are some functions and
there uses in removing the options. You can see the demo of this program
here.

While the page loads we will try to populate the list box by using onload
event of the body tag. The list box will be filled with data at the time of
display to the visitors. The detail on how to add options to list box you can
get here. So we will go to the next step
Removing all the options
To remove all the options from the list box we will loop through all the
elements of the list box and remove one by one. We will use for loop to
loop from 0 to selectbox.options.length-1. The total length ( or the number
of elements ) of the array we can get by using selectbox.options.length and
to this we are subtracting 1 as the first elements starts from 0 ( not from 1
). Here is the function
Here is the demo of the script
function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
selectbox.remove(i);
}
}

The for loop will remove all the elements with the value of i changing from
last element of the array to first element of the array.

Removing Selected Options
We can remove the options one by one or by selecting more than one
option and then by pressing the button. Here also we will use the similar
function like above but before deleting we will check if the option is checked
or not. selectbox.options[i].selected will return true if the option is
selected. This way we will check all the elements of the list box and if they
are checked then we will add the command selectbox.options.remove(i); to
remove that particular option from the list box. Here is the function.


function removeOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
if(selectbox.options[i].selected)
selectbox.remove(i);
}
}
Adding the options
There is a full tutorial on adding the options to list box. So here is the
function.


function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}


function addOption_list(selectbox){
addOption(document.drop_list.SubCat, "One","One");
addOption(document.drop_list.SubCat, "Two","Two");
addOption(document.drop_list.SubCat, "Three","Three");
addOption(document.drop_list.SubCat, "Four","Four");
addOption(document.drop_list.SubCat, "Five","Five");
addOption(document.drop_list.SubCat, "Six","Six");

}
Now we will see how the select box with the form and buttons are placed in
the body are of the page. Each button is connected to one function with on
click event handler. See here


<FORM name="drop_list" action="yourpage.php" method="POST" >

<SELECT id="SubCat" NAME="SubCat" MULTIPLE size=6 width=10>
</SELECT><br>
<input type=button onClick="removeOptions(SubCat)"; value='Remove Selected'>
<input type=button onClick="removeAllOptions(SubCat)"; value='Remove All'>
<input type=button onClick="addOption_list()"; value='Add All'>

</form>

Remove Items From a CheckBox List

up
vote1do
wn votefavorite
Here is main form:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="CheckDelete.aspx.cs" Inherits="CheckDelete" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList ID="chkItems" runat="server" style="width: 37px">
<asp:ListItem Value="A"></asp:ListItem>
<asp:ListItem Value="B"></asp:ListItem>
<asp:ListItem Value="C"></asp:ListItem>
<asp:ListItem Value="D"></asp:ListItem>
<asp:ListItem Value="E"></asp:ListItem>
<asp:ListItem Value="F"></asp:ListItem>
<asp:ListItem Value="H"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Delete" />
<br />
<br />
</form>
Code in Form:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < chkItems.Items.Count; i++)
{
if (chkItems.Items[i].Selected == true)
{
chkItems.Items.RemoveAt(i);
}
}

}
In my form, I want to delete the items that the user has checked off. However, if I select 3
items, at least one item will remain on the form after the user hits delete. What am I missing?
c# asp.net
share|improve this question asked Mar 24 '13 at 14:25



Scott Anderson
83

add a comment
3 Answers
activeoldest votes
up
vote2do
wn votea ccepted
You'll need to make a list of all the items you want to remove and then remove them one by
one.
e.g.
List<ListItem> toBeRemoved = new List<ListItem>();
for(int i=0; i<chkItems.Items.Count; i++){
if(chkItems.Items[i].Selected == true)
toBeRemoved.Add(chkItems.Items[i]);
}

for(int i=0; i<toBeRemoved.Count; i++){
chkItems.Items.Remove(toBeRemoved[i]);
}
In your example, you remove the items as you go which will change the index of the remaining
items that you've yet to loop through. This will result with you "missing" items as you loop
through. I imagine that's the cause of your problem.
share|improve this answer answered Mar 24 '13 at 14:35



Grant Clements
882412




Thank You. Your Awesome. Scott Anderson Mar 24 '13 at 14:40
add a comment

up
vote3do
wn vote
Try looping backwards, e.g.
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = chkItems.Items.Count -1 ; i >= 0; i--)
{
if (chkItems.Items[i].Selected == true)
{
chkItems.Items.RemoveAt(i);
}
}

}
share|improve this answer answered Mar 24 '13 at 14:35



Netricity
1,339715

add a comment
up
vote1do
wn vote
You can do like this.
> for (int i = 0; i < chkItems.Items.Count; i++)
{
if (chkItems.Items[i].Selected == true)
{
ListItem li =new ListItem();
li.Text = chkItems.Items[i].Text;
li.Value = chkItems.Items[i].Value;
chkItems.Items.Remove(li);
}
}
share|improve this answer answered Mar 24 '13 at 14:36



Ashfaq Shaikh
624412


How to remove an object from a list collection?

up
vote4do
wn votefavorite
I had to change on my lines of code around. I before had something like this
// this is in a static method.
List<string> mySTring = new List<string>();

mySTring.add("one");
mySTring.add("two");
However on one of my pages I have a dropdownlist that does not require the field "two" so
instead of writing duplicate code all I did was
myString.remove("two");
Now I need to change my list to a List<SelectListItem> myList = new
List<SelectListItem>();
So I have it now looking like this:
List<SelectListItem> myList = new List<SelectListItem>()
{
new SelectListItem() { Text = "one", Value = "one"},
new SelectListItem() { Text = "two", Value = "two"},
};
So now how do I remove the selectListItem that contains "two"? I know I probably could use
remove by index. But I might add to list in the future so I don't want to start hunting down and
changing it if the index changes.
Thanks
c# .net asp.net asp.net-mvc
share|improve this question asked Nov 8 '09 at 22:26



chobo2
14.6k59258498

add a comment
3 Answers
activeoldest votes
up
vote3do
wn votea ccepted
List<T> is, by default, going to be comparing object references
(unless SelectListItemimplements a custom equality method). So unless you still have a
reference to the second item around, you are going to have to get it either by reference, or
by finding the desired item:
var item = myList.First(x=>x.Value == "two");
myList.Remove(item);
Index may be easier...
share|improve this answer answered Nov 8 '09 at 22:33



Marc Gravell
483k9412861819




hmm was hoping to now have to search for it. I won't have the reference anymore, I guess I will make remove by
index or by finding. Not sure what to use. Just concerned in the future I add something to my list collection and all of
a sudden it is a different index and I am trying to figure out why it is not wokring and that is the reason.
chobo2 Nov 8 '09 at 22:37


Also how would the List collection sort method work on this? Because I was using the sort method on my list of
strings(thats why I am concerned the index will change). chobo2 Nov 8 '09 at 22:38


Well, you could always use a different container that doesn't require searching by brute force. spender Nov 8 '09 at
22:39


List<T>'s sorting works on standard sorting; it depends
whether SelectListItem implements IComparable or IComparable<SelectListItem>. Marc
Gravell Nov 8 '09 at 22:42


my list does not give me an option for "First" does certain list object not get it? chobo2 Nov 8 '09 at 22:55
show 2 more comments

up
vote3do
wn vote
You could use the RemovalAll method:
myList.RemoveAll(i => i.Text == "two");
Obviously this will get rid of all the items whose "Text" property is "two", but since you're
using it in a ComboBox I'm assuming you'll only have one item for each "Text" value.
share|improve this answer answered Nov 8 '09 at 22:35



Matt Hamilton
98.7k32248261

add a comment
up
vote0do
wn vote
we can make mouse selection over the autocomplete div by adding this following code
into the jquery autocomplete function
here i have used the id name as Requiredid
$("#Requiredid").autocomplete({focus:function(e,ui) {
document.getElementById('Requiredid').value = ui.item.label;
document.getElementById('hdnValue').value = ui.item.id;//If You
need this value you //can add this line
}});
share|improve this answer edited Nov 13 '13 at 10:33



kleopatra
31.4k124065
answered Nov 13 '13 at 10:17



Donil
1

add a comment

Вам также может понравиться