Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Dynamically added Linkbutton losing its value

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ree321 - 19 Jun 2007 04:55 GMT
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
Yuriy Solodkyy - 19 Jun 2007 11:39 GMT
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
[quoted text clipped - 60 lines]
>
> End Class
ree321 - 20 Jun 2007 05:23 GMT
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.

> Hi
>
[quoted text clipped - 5 lines]
>
> -yuriy
Yuriy Solodkyy - 20 Jun 2007 07:04 GMT
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.
[quoted text clipped - 24 lines]
>>
>> -yuriy
ree321 - 20 Jun 2007 08:11 GMT
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.

> Hi,
>
[quoted text clipped - 7 lines]
>
> -yuriy
Yuriy Solodkyy - 20 Jun 2007 08:40 GMT
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
[quoted text clipped - 13 lines]
>>
>> -yuriy
ree321 - 21 Jun 2007 00:35 GMT
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.

> I reviewded the code you posted once again.  I am not sure when this code
> run
[quoted text clipped - 6 lines]
>
> -yuriy

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.