473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ViewState and more.

Hi,
I need to understand a basic process.

I generate dynamic linkbuttons like

public int PageIndex
{
get
{
if (ViewStage["pg"]==null)
return 0;
else
return Convert.ToInt32(ViewState["pg"].ToString());
}

}

protected void Page_Load(object sender, EventArgs e)
{
// I change my code for this post
myLabel.Text = PageIndex.ToString();
}
protected void addLink()
{
for (int i=0;i<3;i++){
myButton=new LinkButton();
myButton.CommandName="MyCommand";
myButton.CommandArgument+=new
System.UI.WebControls.CommandEventHandler(MyButton _Click);
myButton.Text="Page " + Convert.ToString(i+1);
}
}

protected void MyButton_Click(object sender, CommandEventArgs e)
{
ViewState["pg"]=e.CommandArgument.ToString();
}
My problem, I suppose it's very simple to find the issue, is when I click a
linkbutton, the Label is not updated (I need to click a second time to have
the right previous value).
I m not sure that I m clear (due to my poor english).
In fact I need to use this code to add it inside an UpdatePanel.

I thank you in advance for your help

Stan

Jun 30 '07 #1
7 1135
On Jun 30, 10:31 pm, "Stan SR" <s...@pasdepam.netsunset.comwrote:
My problem, I suppose it's very simple to find the issue, is when I click a
linkbutton, the Label is not updated (I need to click a second time to have
the right previous value).
I m not sure that I m clear (due to my poor english).
In fact I need to use this code to add it inside an UpdatePanel.
Hi Stan

Once button is clicked, first the Page_Load method and then
MyButton_Click is executed.

Change

protected void Page_Load(object sender, EventArgs e)
{
}

protected void MyButton_Click(object sender, CommandEventArgs e)
{
ViewState["pg"]=e.CommandArgument.ToString();
// I change my code for this post
myLabel.Text = PageIndex.ToString();
}

and then it will work.

Hope it helps

Jun 30 '07 #2
Alexey,

Thanks for your reply.
I suppose if I need to display the first Page I have to place the code
Page.IsPostback test inside the Page_Load Event ?
So I add this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) myLabel.Text = PageIndex.ToString();
}

But I seems it doesn't work when I press the button.
When I add breakpoints inside the MyButton_Click, the process doesn't stop
after pressing the button.

I suppose that my code is not correct.
I m talking about this part

protected void addLink()
{
for (int i=0;i<3;i++){
myButton=new LinkButton();
myButton.CommandName="MyCommand";
myButton.CommandArgument+=new
System.UI.WebControls.CommandEventHandler(MyButton _Click);
myButton.Text="Page " + Convert.ToString(i+1);
}
}

Any Idea ?
Stan

"Alexey Smirnov"
On Jun 30, 10:31 pm, "Stan SR" <s...@pasdepam.netsunset.comwrote:
>My problem, I suppose it's very simple to find the issue, is when I click
a
linkbutton, the Label is not updated (I need to click a second time to
have
the right previous value).
I m not sure that I m clear (due to my poor english).
In fact I need to use this code to add it inside an UpdatePanel.

Hi Stan

Once button is clicked, first the Page_Load method and then
MyButton_Click is executed.

Change

protected void Page_Load(object sender, EventArgs e)
{
}

protected void MyButton_Click(object sender, CommandEventArgs e)
{
ViewState["pg"]=e.CommandArgument.ToString();
// I change my code for this post
myLabel.Text = PageIndex.ToString();
}

and then it will work.

Hope it helps
Jun 30 '07 #3
On Jun 30, 11:45 pm, "Stan SR" <s...@pasdepam.netsunset.comwrote:
Alexey,

Thanks for your reply.
I suppose if I need to display the first Page I have to place the code
Page.IsPostback test inside the Page_Load Event ?
So I add this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) myLabel.Text = PageIndex.ToString();
}

But I seems it doesn't work when I press the button.
What is ViewStage? I suppose it has to be a ViewState

--------------------------------------------------
if (ViewStage["pg"]==null)
--------------------------------------------------

It should return 0 at the beginnig

Jun 30 '07 #4
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
if (ViewStage["pg"]==null)
ViewStage??? Is that a typo...?
--
http://www.markrae.net

Jun 30 '07 #5
Hi

try assigning ID to you dynamically created controls lke:

myButton.ID = "btn_" + i.ToString();

-yuriy
Alexey,

Thanks for your reply.
I suppose if I need to display the first Page I have to place the code
Page.IsPostback test inside the Page_Load Event ?
So I add this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) myLabel.Text = PageIndex.ToString();
}
But I seems it doesn't work when I press the button.
When I add breakpoints inside the MyButton_Click, the process doesn't
stop
after pressing the button.
I suppose that my code is not correct.
I m talking about this part
protected void addLink()
{
for (int i=0;i<3;i++){
myButton=new LinkButton();
myButton.CommandName="MyCommand";
myButton.CommandArgument+=new
System.UI.WebControls.CommandEventHandler(MyButton _Click);
myButton.Text="Page " + Convert.ToString(i+1);
}
}
Any Idea ?
Stan
"Alexey Smirnov"
>On Jun 30, 10:31 pm, "Stan SR" <s...@pasdepam.netsunset.comwrote:
>>My problem, I suppose it's very simple to find the issue, is when I
click
a
linkbutton, the Label is not updated (I need to click a second time
to
have
the right previous value).
I m not sure that I m clear (due to my poor english).
In fact I need to use this code to add it inside an UpdatePanel.
Hi Stan

Once button is clicked, first the Page_Load method and then
MyButton_Click is executed.

Change

protected void Page_Load(object sender, EventArgs e)
{
}
protected void MyButton_Click(object sender, CommandEventArgs e)
{
ViewState["pg"]=e.CommandArgument.ToString();
// I change my code for this post
myLabel.Text = PageIndex.ToString();
}
and then it will work.

Hope it helps

Jun 30 '07 #6
Ooops
I meant ViewState..

In fact, the problem is related to the dynamic LinkButtons.
When I postBack the page, they are lost and it's the reason why the
MyButton_Click is not raised.

Stan

"Mark Rae" <ma**@markNOSPAMrae.neta écrit dans le message de
news:Og*************@TK2MSFTNGP06.phx.gbl...
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:ON**************@TK2MSFTNGP03.phx.gbl...
> if (ViewStage["pg"]==null)

ViewStage??? Is that a typo...?
--
http://www.markrae.net
Jun 30 '07 #7
"Stan SR" <st**@pasdepam.netsunset.comwrote in message
news:uF**************@TK2MSFTNGP06.phx.gbl...
In fact, the problem is related to the dynamic LinkButtons.
When I postBack the page, they are lost and it's the reason why the
MyButton_Click is not raised.
Dynamic controls need to be recreated every time, and in either the
Page_PreInit or Page_Init methods - Page_Load is too late...
--
http://www.markrae.net

Jul 1 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
7
by: et | last post by:
I'm not sure I understand the use of the ViewState. Do I understand correctly that values of controls are automatically held in a hidden control called ViewState? If so, then why can't we get...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
7
by: Mantorok | last post by:
It's not unusual (to be lo....cough) for me to have VIEWSTATE ranging from 1 full page to several pages..... This seems in-efficient - any ideas how to reduce the thing? Thanks Kev
5
by: John Kotuby | last post by:
Hi all, After more than a year programming with ASP.NET 2.0 and VB I am still finding it difficult to leave some habits from classic ASP behind. this is particularly true with cross-page posting....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.