Dynamically added Linkbutton losing its value

R

ree321

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
 
Y

Yuriy Solodkyy

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
 
R

ree321

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

Yuriy Solodkyy

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
 
R

ree321

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

Yuriy Solodkyy

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
 
R

ree321

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.
 

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

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top