473,799 Members | 3,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(colNu m).Controls(0)
lbtn.Text = "Hello"
lbtn.CommandArg ument = 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.Ce lls(i).Controls (0),
LinkButton).Tex t

It has the old value.

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

Private SecondColAsDisp lay As Boolean

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

Public Sub InstantiateIn(B yVal container As Control) Implements
ITemplate.Insta ntiateIn
Dim lbtn As LinkButton = New LinkButton
AddHandler lbtn.DataBindin g, AddressOf Me.BindLinkButt on
container.Contr ols.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.Nami ngContainer, DataGridItem)
Dim textCol As String = ColName
Dim argCol As String = SecondColName

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

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

lbtn.CommandArg ument = dgi.DataItem(ar gCol)
End Sub
End Class

Jun 19 '07 #1
6 2600
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(colNu m).Controls(0)
lbtn.Text = "Hello"
lbtn.CommandArg ument = 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.Ce lls(i).Controls (0),
LinkButton).Tex t

It has the old value.

Here is the Itemplate code

Public Class TemplateEditLin kButton
Implements ITemplate
Private ColName As String
Private SecondColName As String
Private SecondColAsDisp lay As Boolean

Public Sub New(ByVal aColName As String, ByVal aSecondColName As
String, ByVal aSecondColAsDis play As Boolean)
ColName = aColName
SecondColName = aSecondColName
SecondColAsDisp lay = aSecondColAsDis play 'so the secoond col
will be the lbtn Text, and first column will be the text
End Sub
Public Sub InstantiateIn(B yVal container As Control) Implements
ITemplate.Insta ntiateIn
Dim lbtn As LinkButton = New LinkButton
AddHandler lbtn.DataBindin g, AddressOf Me.BindLinkButt on
container.Contr ols.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.Nami ngContainer, DataGridItem)
Dim textCol As String = ColName
Dim argCol As String = SecondColName
If SecondColAsDisp lay Then 'switch it around
textCol = SecondColName
argCol = ColName
End If
If Not dgi.DataItem(te xtCol) Is DBNull.Value Then
lbtn.Text = dgi.DataItem(te xtCol)
Else
lbtn.Text = "[Empty]"
End If
lbtn.CommandArg ument = dgi.DataItem(ar gCol)

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.contr ols.Add(linkBut ton)

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.contr ols.Add(linkBut ton)

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.Ce lls(i).Controls (0), LinkButton).Tex t?

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.contr ols.Add(linkBut ton)
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.Ce lls(i).Controls (0), LinkButton).Tex t?

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
10808
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
0
2474
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 added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
8
4318
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 button at runtime: //----- Code snippet protected System.Web.UI.WebControls.PlaceHolder ImageHolder; private void Page_Load(object sender, System.EventArgs e)
2
1950
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) to determine what options should be available for this particular item (which is an issue... small issue tracking system). Each of these options is an action that may be performed on the issue and I am dynamically creating LinkButtons for each...
3
2887
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. FindControl doesn't work, and the textboxes cannot be accessed using grid.Rows.Cells.Control. Can someone help? Thanks, Keith
5
4719
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 handler to deal with the Command event, but for some reason I can get this to work in a C# project but not in a newly created VB.NET one. The relevant VB.NET is as follows:
5
2305
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 like so: Dim column as TemplateColumn = new TemplateColumn() column.HeaderText = "Template Column" column.ItemStyle.Width = Unit.Pixel(width) column.HeaderStyle.Width = Unit.Pixel(width)
0
1375
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 tabpanels such as:Catalog,Terms,SiteMap.ViewCart ,etc. All the jewellery Intro is in the Catalog panel. when I first click the Catalog panel it Introduces me all the main types of the jewellery(It's all done dynamically from the cs file).The...
3
5278
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 add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.