Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 18th, 2004, 01:18 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default store multiple values in a string

Hi Friends,

I am new user of ASP.NET. I'm trying to store the multiple values in
a string, but very time it override the pervious value whenever I
call the function through a command button.

Please help me out.

Here I also enclosing the sample coding for your reference

<%@Page Language=VB Debug=True %>
<Script runat="server">

Dim WorkExperience as String


Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)


lblMessage.Text = "Press a button !"

WorkExperience +=" Hello !"

End Sub



Sub SubmitBtn_AddValue(Sender as Object, E as EventArgs)



WorkExperience + = "IT Officer"
WorkExperience + = "01,"
WorkExperience + = "2002,"
WorkExperience + = "02,"
WorkExperience + = "2002,"
WorkExperience + = "Hello2,"
WorkExperience + = "+"


lblMessage.Text= WorkExperience

End Sub

</Script>

<html>
<body>
<form runat="server">
<asp:Label
id="lblMessage"
Text=""
runat="server"
/>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<asp:Button
id="butAlone"
text="Add Value"
OnClick="SubmitBtn_AddValue"
runat="server"
/>

<BR>
<BR>
</form>
</body>
</html>

Try to run the above cited code. You will find a button named
as "Add Value", if u press this button it will display a dummy
value, now I want that if you press this Add Value more than once
time then it must display the dummy value respectively.

For Example: Press this button 3 times then it must show the same
value 3 times.

Please reply me soon because all my work is halt due to this problem

Looking forward to your reply...

Regards,
Mustafa Bamboat
bamboat_3@hotmail.com

Reply With Quote
  #2 (permalink)  
Old May 19th, 2004, 01:40 AM
Authorized User
 
Join Date: Mar 2004
Location: Rawang, Selangor, Malaysia.
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mustafa,
There are few ways to solve your problem.

1) Use session

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
lblMessage.Text = "Press a button !"
'To get back WorkExperience last result
WorkExperience = session.Contents("WE")
'To avoid clear session
session.Contents.Remove("WE")
WorkExperience +=" Hello !"
End Sub

Sub SubmitBtn_AddValue(Sender as Object, E as EventArgs)
WorkExperience + = "IT Officer"
WorkExperience + = "01,"
WorkExperience + = "2002,"
WorkExperience + = "02,"
WorkExperience + = "2002,"
WorkExperience + = "Hello2,"
WorkExperience + = "+"
'To store value into session
session.Contents("WE") = WorkExperience
lblMessage.Text= WorkExperience
End Sub


2) Use label.text

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
if lblMessage.text = ""
    lblMessage.Text = "Press a button !"
else
WorkExperience = lblMessage.text
end if
WorkExperience +=" Hello !"
End Sub

Sub SubmitBtn_AddValue(Sender as Object, E as EventArgs)
WorkExperience + = "IT Officer"
WorkExperience + = "01,"
WorkExperience + = "2002,"
WorkExperience + = "02,"
WorkExperience + = "2002,"
WorkExperience + = "Hello2,"
WorkExperience + = "+"
lblMessage.Text= WorkExperience
End Sub


Hopefully this will help you

Reply With Quote
  #3 (permalink)  
Old May 19th, 2004, 10:43 AM
Registered User
 
Join Date: Apr 2004
Location: Montreal, Quebec, Canada.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Better way for doing this is using the ViewState bag. Almost the same than using Session, except that ViewState are'nt taking server memory.

Best practice is : Save small common object type into ViewState (Ie, string,int,datetime) and save Huge objects (XmlDocument,Dictionary,etc..) into the Session bag.

PS: ViewState value will only longs while you are posting back a page, Session will last as long the session is alive.


========================
Using ViewState bag :
At the Page_PreRender event, Save the String into the ViewState :
ViewState("viewstateKey") = WorkExperience

At Page_Load get the previously ViewStateValue
WorkExperience = ViewState("viewstateKey")
=========================
Jonathan
jlarouche@dev-id.com

Reply With Quote
  #4 (permalink)  
Old May 23rd, 2004, 07:32 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

A more important issue here is that you must understand the postback and viewstate models of ASP.net. The value of the asp label is maintained in viewstate automatically. So when you call the button click handler, you only need to get the current value of the label out and append to it. Also, you need to set the label's default text only on the FIRST pagehit (use IsPostback in the Page_Load method).

<%@Page Language=VB Debug=True %>
<Script runat="server">

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostback Then
        lblMessage.Text = "Press a button !"
    End If
End Sub

Sub SubmitBtn_AddValue(Sender as Object, E as EventArgs)

    Dim WorkExperience as String = lblMessage.Text
    WorkExperience +=" Hello !"
    WorkExperience + = "IT Officer"
    WorkExperience + = "01,"
    WorkExperience + = "2002,"
    WorkExperience + = "02,"
    WorkExperience + = "2002,"
    WorkExperience + = "Hello2,"
    WorkExperience + = "+"

    lblMessage.Text= WorkExperience

End Sub

</Script>

<html>
<body>
    <form runat="server">
        <asp:Label id="lblMessage" Text="" runat="server" />
        <BR>
        <BR>
        <BR>
        <BR>
        <BR>
        <BR>
        <asp:Button id="butAlone" text="Add Value" OnClick="SubmitBtn_AddValue" runat="server" />

        <BR>
        <BR>
    </form>
</body>
</html>




Peter
-------------------------
Work smarter, not harder
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to store "SET" values to Vector ? msg2ajay JSP Basics 1 September 4th, 2007 06:45 AM
store hyperlink into a string. europhreak Classic ASP Basics 1 February 2nd, 2006 11:16 PM
Store Array of Form Values lbunnell Beginning PHP 1 June 21st, 2005 10:13 AM
store values of recieved xml Steven1984 XML 0 April 17th, 2005 11:22 AM



All times are GMT -4. The time now is 12:22 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.