Dynamically adding a linkbutton to a page

  • Thread starter Thread starter Mark Fitzpatrick
  • Start date Start date
M

Mark Fitzpatrick

Your best bet is to wire up an event to the linkbutton. Basically, you can
set a variable in the viewstate, and whenever the click event of the button
fires you fetch that variable from the viewstate, increment it, then you can
set the text of that linkbutton. You'll probably have to set the text of the
link button during the ItemCreated event of the datalist. You'll have to
also handle an initial testing so that when the viewstate variable is
null(as is the case with the initial viewing) it will set the variable to
zero or 1.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
Hi Mark

Not exactly sure why yours doesn't do what you want, but here's what I got to work

In the code-behind..

Private m_intContainerNow As Intege
Public Function ContainerNow() As Strin
m_intContainerNow +=
Return m_intContainerNow.ToStrin
End Functio

And in the html..

<Columns><asp:TemplateColumn HeaderText="My Column"><ItemTemplate><asp:LinkButton id=lnkButton runat="server" Text="<%# ContainerNow() %>"></asp:LinkButton></ItemTemplate></asp:TemplateColumn></Columns

I tested on a datagrid, but should be no different

hth

Bil


----- Mark wrote: ----

Hi all, I have a datalist that will contain a number of ASP.NET lin
buttons

Code belo
=====================

<asp:DataList id="DataList1" runat="server" GridLines=Non
ItemStyle-Height="10px" ItemStyle-CssClass="menu" CellSpacing="1"><ItemTemplate><asp:linkbutton id="btnRedirect" runat="server" text='<% CounterNow(
%>' /></ItemTemplate></asp:DataList><script language="visualbasic" runat="server"
Dim nLoop as integer =
function CounterNow(
if nLoop = 3 the
Response.Write("Your 3rd page"
else if nLoop = 1 the
Response.Write("Your 1st page"
else if nLoop = 2 the
Response.Write("Your 2nd page"
els
Response.Write("Your " & nLoop & "th page"
end i
nLoop = nLoop +

end Functio
</script
=====================

I am trying to get the link text to be shown as "Your 3rd page" etc by usin
the inline function CounterNow but this function never seems to execute.
have tried placing the function in the code behind page and have tried <
=CounterNow() %> but still have no luck

The datalist is successfully binding to the database as if I replace th
call to the function CounterNow with just "Here" it works fine.

Any help appreciate
Cheer
Mar
 
P.S. You might have just been missing the # in the <% ... %>. If I leave that off in my test, no errors but my column comes up empty. Cheers.
 
BTW, could you check the timezone setting on your PC? Something is throwing
off the sort.
 
Hi all, I have a datalist that will contain a number of ASP.NET link
buttons.

Code below
======================

<asp:DataList id="DataList1" runat="server" GridLines=None
ItemStyle-Height="10px" ItemStyle-CssClass="menu" CellSpacing="1">
<ItemTemplate>
<asp:linkbutton id="btnRedirect" runat="server" text='<% CounterNow()
%>' />
</ItemTemplate>
</asp:DataList>
<script language="visualbasic" runat="server">
Dim nLoop as integer = 1
function CounterNow()
if nLoop = 3 then
Response.Write("Your 3rd page")
else if nLoop = 1 then
Response.Write("Your 1st page")
else if nLoop = 2 then
Response.Write("Your 2nd page")
else
Response.Write("Your " & nLoop & "th page")
end if
nLoop = nLoop + 1

end Function
</script>
======================

I am trying to get the link text to be shown as "Your 3rd page" etc by using
the inline function CounterNow but this function never seems to execute. I
have tried placing the function in the code behind page and have tried <%
=CounterNow() %> but still have no luck.

The datalist is successfully binding to the database as if I replace tht
call to the function CounterNow with just "Here" it works fine..

Any help appreciated
Cheers
Mark
 
Thanks. Must be your ISP's system is out of whack. It is putting the zone
as GMT + 10
 
I am actually in Gisborne, New Zealand first city in the world to see the
sun, so the time is right, it is actually 1:40 pm on 31st March here :)

You would think the timestamp would be issued by the news server not the
client though...

Cheers
Mark
 
Back
Top