ViewState and more.

S

Stan SR

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
 
G

Guest

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
 
S

Stan SR

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

"Anon User"
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
 
G

Guest

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
 
Y

Yuriy Solodkyy

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
"Anon User"
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
 
S

Stan SR

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 said:
if (ViewStage["pg"]==null)

ViewStage??? Is that a typo...?
 
M

Mark Rae

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top