p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   General .NET (http://p2p.wrox.com/forumdisplay.php?f=122)
-   -   store multiple values in a string (http://p2p.wrox.com/showthread.php?t=13129)

bamboat_3 May 18th, 2004 01:18 PM

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


leion May 19th, 2004 01:40 AM

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


jlarouche May 19th, 2004 10:43 AM

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


planoie May 23rd, 2004 07:32 AM

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


All times are GMT -4. The time now is 01:24 PM.

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