473,387 Members | 1,515 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,387 software developers and data experts.

Dynamically added Linkbutton losing its value

I have a linkbutton which is added to a column in a datagrid
dynamically using a template I created. When I change the data in it
by not databinding and then when I later retreive the data from the
control after a post back it goes back to the value which was
dynamically bounded.

I have set up similar process with a checkbox template and it remebers
the value.

Are Linkbuttons not capable of this? and should I use something else?

Here is the code I used to change the values in the linkbutton:
Dim lbtn As LinkButton = dgi.Cells(colNum).Controls(0)
lbtn.Text = "Hello"
lbtn.CommandArgument = 1001

When the page has been rendered it shows up as "Hello". But then on a
post back I try and retreieve the data

userInputValues(i) = CType(e.Item.Cells(i).Controls(0),
LinkButton).Text

It has the old value.

Here is the Itemplate code
Public Class TemplateEditLinkButton
Implements ITemplate
Private ColName As String
Private SecondColName As String

Private SecondColAsDisplay As Boolean

Public Sub New(ByVal aColName As String, ByVal aSecondColName As
String, ByVal aSecondColAsDisplay As Boolean)
ColName = aColName
SecondColName = aSecondColName
SecondColAsDisplay = aSecondColAsDisplay 'so the secoond col
will be the lbtn Text, and first column will be the text
End Sub

Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Dim lbtn As LinkButton = New LinkButton
AddHandler lbtn.DataBinding, AddressOf Me.BindLinkButton
container.Controls.Add(lbtn)
End Sub

Sub BindLinkButton(ByVal s As Object, ByVal e As EventArgs)
Dim lbtn As LinkButton = s
Dim dgi As DataGridItem
dgi = CType(lbtn.NamingContainer, DataGridItem)
Dim textCol As String = ColName
Dim argCol As String = SecondColName

If SecondColAsDisplay Then 'switch it around
textCol = SecondColName
argCol = ColName
End If

If Not dgi.DataItem(textCol) Is DBNull.Value Then
lbtn.Text = dgi.DataItem(textCol)
Else
lbtn.Text = "[Empty]"
End If

lbtn.CommandArgument = dgi.DataItem(argCol)
End Sub
End Class

Jun 19 '07 #1
6 2582
Hi

It looks like you disabled ViewState for DataGrid/DataView. CheckBox restores
it value from the post-data while LinkButton needs ViewState.

If it is not the case, enable Trace option for this page and post it here
(for both not IsPostBackand IsPostBack)

-yuriy
I have a linkbutton which is added to a column in a datagrid
dynamically using a template I created. When I change the data in it
by not databinding and then when I later retreive the data from the
control after a post back it goes back to the value which was
dynamically bounded.

I have set up similar process with a checkbox template and it remebers
the value.

Are Linkbuttons not capable of this? and should I use something else?

Here is the code I used to change the values in the linkbutton:
Dim lbtn As LinkButton = dgi.Cells(colNum).Controls(0)
lbtn.Text = "Hello"
lbtn.CommandArgument = 1001
When the page has been rendered it shows up as "Hello". But then on a
post back I try and retreieve the data

userInputValues(i) = CType(e.Item.Cells(i).Controls(0),
LinkButton).Text

It has the old value.

Here is the Itemplate code

Public Class TemplateEditLinkButton
Implements ITemplate
Private ColName As String
Private SecondColName As String
Private SecondColAsDisplay As Boolean

Public Sub New(ByVal aColName As String, ByVal aSecondColName As
String, ByVal aSecondColAsDisplay As Boolean)
ColName = aColName
SecondColName = aSecondColName
SecondColAsDisplay = aSecondColAsDisplay 'so the secoond col
will be the lbtn Text, and first column will be the text
End Sub
Public Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Dim lbtn As LinkButton = New LinkButton
AddHandler lbtn.DataBinding, AddressOf Me.BindLinkButton
container.Controls.Add(lbtn)
End Sub
Sub BindLinkButton(ByVal s As Object, ByVal e As EventArgs)
Dim lbtn As LinkButton = s
Dim dgi As DataGridItem
dgi = CType(lbtn.NamingContainer, DataGridItem)
Dim textCol As String = ColName
Dim argCol As String = SecondColName
If SecondColAsDisplay Then 'switch it around
textCol = SecondColName
argCol = ColName
End If
If Not dgi.DataItem(textCol) Is DBNull.Value Then
lbtn.Text = dgi.DataItem(textCol)
Else
lbtn.Text = "[Empty]"
End If
lbtn.CommandArgument = dgi.DataItem(argCol)

End Sub

End Class

Jun 19 '07 #2
It has to be on but even when I turned it on or off in the datagrid, I
still had the same results.

Regarding the trace there is a whole lot of information which is
impossible to post here but maybe you were after the
Form collection.

_ctl0:gridTable:_ctl3:_ctl2 011787
_ctl0:gridTable:_ctl3:_ctl4 5
_ctl0:gridTable:_ctl3:_ctl5 May 06
_ctl0:gridTable:_ctl3:_ctl6 Mar 07
_ctl0:gridTable:_ctl3:_ctl7 on
_ctl0:gridTable:_ctl3:_ctl8 S
--------------
_ctl0:gridTable:_ctl3:_ctl3 contains the linkbutton but as you can
see it doesn't show up before or after the post back.

Also on another note the datagrid is on a user control which in turn
is on the page.

On Jun 19, 8:39 pm, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
Hi

It looks like you disabled ViewState for DataGrid/DataView. CheckBox restores
it value from the post-data while LinkButton needs ViewState.

If it is not the case, enable Trace option for this page and post it here
(for both not IsPostBackand IsPostBack)

-yuriy

Jun 20 '07 #3
Hi,

When do you create your link buttons? (oninit, onload ec)
Do you set link button properties before adding it to the parent control
or after? (before or after something.controls.Add(linkButton)

If you create your buttons in the OnInit phase and set its properties after
adding it to the page, you will not be able to these properties later as
they will be restored from the viewstate.

-yuriy
It has to be on but even when I turned it on or off in the datagrid, I
still had the same results.

Regarding the trace there is a whole lot of information which is
impossible to post here but maybe you were after the
Form collection.
_ctl0:gridTable:_ctl3:_ctl2 011787
_ctl0:gridTable:_ctl3:_ctl4 5
_ctl0:gridTable:_ctl3:_ctl5 May 06
_ctl0:gridTable:_ctl3:_ctl6 Mar 07
_ctl0:gridTable:_ctl3:_ctl7 on
_ctl0:gridTable:_ctl3:_ctl8 S
--------------
_ctl0:gridTable:_ctl3:_ctl3 contains the linkbutton but as you can
see it doesn't show up before or after the post back.
Also on another note the datagrid is on a user control which in turn
is on the page.

On Jun 19, 8:39 pm, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
>Hi

It looks like you disabled ViewState for DataGrid/DataView. CheckBox
restores it value from the post-data while LinkButton needs
ViewState.

If it is not the case, enable Trace option for this page and post it
here (for both not IsPostBackand IsPostBack)

-yuriy

Jun 20 '07 #4
On onload - i.e. add the user control in onload of the page, which
in turn adds the the columns in onload and then it binds the datagrid
with a copy of the datatable used to populate from the data grid.
Note this datatable holds the data from the database and doesn't get
update unless a save or delete has been called.

On Jun 20, 4:04 pm, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
Hi,

When do you create your link buttons? (oninit, onload ec)
Do you set link button properties before adding it to the parent control
or after? (before or after something.controls.Add(linkButton)

If you create your buttons in the OnInit phase and set its properties after
adding it to the page, you will not be able to these properties later as
they will be restored from the viewstate.

-yuriy

Jun 20 '07 #5
I reviewded the code you posted once again. I am not sure when this code
run
userInputValues(i) = CType(e.Item.Cells(i).Controls(0), LinkButton).Text?

Is it possible that you simply override your value on data binding? I.e.
set text to old value before this code executes?
You said that nothing changes if you disable gor enable viewstate, so I can
conclude that you databing your datagrid on every postback.

-yuriy

On onload - i.e. add the user control in onload of the page, which
in turn adds the the columns in onload and then it binds the datagrid
with a copy of the datatable used to populate from the data grid. Note
this datatable holds the data from the database and doesn't get update
unless a save or delete has been called.

On Jun 20, 4:04 pm, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
>Hi,

When do you create your link buttons? (oninit, onload ec)
Do you set link button properties before adding it to the parent
control
or after? (before or after something.controls.Add(linkButton)
If you create your buttons in the OnInit phase and set its properties
after adding it to the page, you will not be able to these properties
later as they will be restored from the viewstate.

-yuriy

Jun 20 '07 #6

That code is run when I am getting a user to choose a name from
another control not in the datagrid and I am passing the value to
this grid to the row which is in edit so I can change the value of the
linkbutton.

On the screen it does this the linkbutton text changes but when I read
the value after a postback I am getting the old data, even though on
screen the new value is shown.

I have to databind the grid on postback or the grid is empty after a
postback.
On Jun 20, 5:40 pm, Yuriy Solodkyy <y.dot.solod...@gmail.comwrote:
I reviewded the code you posted once again. I am not sure when this code
run
userInputValues(i) = CType(e.Item.Cells(i).Controls(0), LinkButton).Text?

Is it possible that you simply override your value on data binding? I.e.
set text to old value before this code executes?
You said that nothing changes if you disable gor enable viewstate, so I can
conclude that you databing your datagrid on every postback.

-yuriy
Jun 20 '07 #7

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

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status)...
3
by: keithb | last post by:
My code dynamically adds template fields to a GridView control. Everything seems to work OK, except when updating, because I haven't found a way to reference the dynamically added textboxes....
5
by: =?Utf-8?B?TWFyYyBXb29sZnNvbg==?= | last post by:
Hi, I have a strange issue occurring with LinkButtons that are dynamically added to each (data) row of my DataGrid on that grid's ItemDataBound event. Each LinkButton is assigned its own event...
5
by: tshad | last post by:
I found I can create Template columns dynamically - as long as I don't use objects that need onclick events, such as a LinkButton. Textboxes and Labels work fine. I create the Template columns...
0
by: Syoam4ka | last post by:
My project is about jewellery. I have devided my jewelery into main types, which each one of them has sub types, and each one those sub types has the jewellery. I have a tabcontainer. It includes...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.