Showing posts with label inherits. Show all posts
Showing posts with label inherits. Show all posts

Wednesday, March 21, 2012

Please help me fix this code to make it work properly

default.asp code

<%

@dotnet.itags.org.PageLanguage="VB"AutoEventWireup="true"CodeFile="Default.aspx.vb"Inherits="_Default" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server"><title>Untitled Page</title>

</

head>

<

body><formid="form1"runat="server"><div><asp:ScriptManagerID="ScriptManager1"runat="server"EnablePartialRendering="True"></asp:ScriptManager><asp:UpdatePanelID="UpdatePanel1"runat="server"><ContentTemplate><asp:LabelID="Label1"runat="server"Text="Label"></asp:Label></ContentTemplate><Triggers><asp:AsyncPostBackTriggerControlID="Timer1"EventName="Tick"/></Triggers></asp:UpdatePanel><asp:TimerID="Timer1"runat="server"Interval="2000"></asp:Timer></div><br/><asp:TextBoxID="TextBox1"runat="server"AutoPostBack="True">Input Test</asp:TextBox><asp:ButtonID="Button1"runat="server"Text="Button"/></form>

</

body>

</

html>

vb code

Partial

Class _DefaultInherits System.Web.UI.PageDim tempAsStringProtectedSub Page_Load(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.LoadIf Page.IsPostBackThen

Label1.Text = temp

EndIfEndSubProtectedSub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.ClickDim iAsIntegerFor i = 0To 100

temp =

String.Format("{0} {1} {2}", TextBox1.Text, i,"<br />")NextEndSub

End

Class

The problem im getting is each time the page do a postback the temp's value is nothing. What do i need to look into to make it work?

Thanks!!

ASP.NET creates a new page object to service each request. This typically makes the application faster, more scalable and more reliable.

Therefore, you cannot maintain state in a field within a class (as you're attempting to do above).

You either need to store the data on the page (using ViewState, which generates a hidden field in the HTML) or in a Session object.

Both of these use simple indexing to put data in and get data out, as shown below:

ViewState("TempValue") = 10 ' put a value into the ViewState collection for this page

Dim i As Integer = CInt( ViewState("TempValue" ) ) ' get the value back out of the ViewState collection

Normally you should add code to make sure that the value you're reading is not nothing.

Be aware that ViewState data is present in the client HTML and can thus be read (and altered) unless you turn on ViewState protection. ViewState can really only be used on a single page.

Session data, on the other hand, is maintained on the Server and is thus more secure. Session can also be used across multiple pages.

You might want to check out some of the posts in the State Management forum (and in the ASP.NET documentation) for more details on ASP.NET state management technologies.


hi,

is it the case that you want to display value of temp in label if button is clicked,

simple after your for loop write this Label1.Text = temp in button click.

regards,

satish.


I'll go read on that topic some more .. Thanks


What i want to do is to have the temp value update each time from the loop

hi,

in your button click you've written temp = some value in for i=1 to 100, ok. so what will happen is at last it will contain values for i=100.

to persist value of temp you could store it in viewstate or a hidden field with runat=server. then in page load set temp = viewstate("temp")

then set lable.text = temp.

regards,

satish.

Friday, March 16, 2012

Please help override property

Hi,
I've never developed in CSharp before and I'm trying to adapt an
existing user control written in CSharp. The user control inherits from
the DataGridColumn. I'd like for one of my controls properties to
override that of the DataGridColumn. Could somebody help with the
syntax? Currently my code for the property is as follows:
public virtual string HeaderStyle
{
get
{
object savedState = null;
savedState = this.ViewState["HeaderStyle"];
if (savedState != null)
{
return (string)savedState;
}
return "";
}
set
{
this.ViewState["HeaderStyle"] = value;
}
}
When I try to compile I get the message 'HeaderStyle hides inherited
member system.web.ui.webcontrols.datagridcolumn.headerstyle. To make
the current member override that implementation, add the override
keyword. Otherwise add the new keyword'. Please could someone confirm
that instead of
public virtual string HeaderStyle
I should have
public overrides string HeaderStyle
Thanks,
PaulI think it should be "override", without the "s".
An "override" is actually a "virtual" but it emphasize that it override
the virtual function declared in one of its base class. The thing is it
makes the purpose clearer, and avoid the case of accidental overriding
when you actually want a new virtual method, and acidentally create a
new virtual method while you actually want to overriding.