Dynamically Setting Runat = Server on ButtonField?

P

pbd22

Hi.

How do I add the runat=server attribute on a buttonfield link
dynamically?

thanks!
 
P

Patrice

It would be easier if it was already a server side control but then it would
be not necessary as it would be already a server side control ;-)

More seriously why can't you set this attribute once for all ? Just use the
runat="server" attribute? It doesn't mean you have actually to do something
with it. Use it or not as will.It should arm having this if this is not
needed.

You may want to explain what you are trying to do as you seems to have an
unusual scenario (this is just a guess but if you render directly HTML
markup, you could also create a server control dynamically instead and let
the server control render the HTML markup rather than creating "by hand" the
markup that you would like to turn into an HTML server control).
 
P

pbd22

It would be easier if it was already a server side control but then it would
be not necessary as it would be already a server side control ;-)

More seriously why can't you set this attribute once for all ? Just use the
runat="server" attribute? It doesn't mean you have actually to do something
with it. Use it or not as will.It should arm having this if this is not
needed.

You may want to explain what you are trying to do as you seems to have an
unusual scenario (this is just a guess but if you render directly HTML
markup, you could also create a server control dynamically instead and let
the server control render the HTML markup rather than creating "by hand" the
markup that you would like to turn into an HTML server control).

all I am trying to do is add an ID tag and an onclick event to the
anchor tags generated by the server.
I am getting in a bit of tailspin.

the tags are generated in a for loop like this:

foreach (DataColumn col in dt.Columns)
{

ButtonField bnfield = new ButtonField();

////Initalize the DataField value.
bnfield.DataTextField =
col.ColumnName;

// Center it
bnfield.ItemStyle.HorizontalAlign =
HorizontalAlign.Center;

//Initialize the HeaderText field
value.
bnfield.HeaderText = col.ColumnName;

bnfield.ButtonType = ButtonType.Link;

bnfield.
////Add the newly created bound field
to the GridView.
vGridView.Columns.Add(bnfield);


Can you please "show me" how I could add an ID and Onclick attribute
to these tags?

Thanks.
 
P

Patrice

Ah ok. To sort if off, it has nothing to do with the runat tag. A server
side control is just a control that is used server side (and if it comes
from aspx markup, it has a runat=server attributes so that ASP.NET knows it
should be instantiated server side. If instanciation is done in server side
code, this is by definition a server side control).

Now :

1) Keep in mind that what you define here is just a column so it doesn't
have an id as each control in this column will have its own id

2) IMO your best bet is to handle this in the RowDataBound event. In this
even the e.row pproerty gives you the current cells. You can then address
these cells. They'll contain a LinkButton object. You can then use its
methods to, get whatever you want.

Code could be similar to (VB.NET) :

For i As Integer = 0 To e.Row.Cells.Count - 1
For j As Integer = 0 To e.Row.Cells(i).Controls.Count - 1
If TypeOf e.Row.Cells(i).Controls(j) Is LinkButton Then
Dim lb As LinkButton = CType(e.Row.Cells(i).Controls(j),
LinkButton)
lb.Attributes.Add("onclick", "alert('My ID is " &
lb.ClientID & "');")
End If
Next
Next

Basically it browse all cells and all controls and if a control is a link
button it then add the onclick attributes.

--
Patrice


"pbd22" <[email protected]> a écrit dans le message de
(e-mail address removed)...
It would be easier if it was already a server side control but then it
would
be not necessary as it would be already a server side control ;-)

More seriously why can't you set this attribute once for all ? Just use
the
runat="server" attribute? It doesn't mean you have actually to do
something
with it. Use it or not as will.It should arm having this if this is not
needed.

You may want to explain what you are trying to do as you seems to have an
unusual scenario (this is just a guess but if you render directly HTML
markup, you could also create a server control dynamically instead and let
the server control render the HTML markup rather than creating "by hand"
the
markup that you would like to turn into an HTML server control).

all I am trying to do is add an ID tag and an onclick event to the
anchor tags generated by the server.
I am getting in a bit of tailspin.

the tags are generated in a for loop like this:

foreach (DataColumn col in dt.Columns)
{

ButtonField bnfield = new ButtonField();

////Initalize the DataField value.
bnfield.DataTextField =
col.ColumnName;

// Center it
bnfield.ItemStyle.HorizontalAlign =
HorizontalAlign.Center;

//Initialize the HeaderText field
value.
bnfield.HeaderText = col.ColumnName;

bnfield.ButtonType = ButtonType.Link;

bnfield.
////Add the newly created bound field
to the GridView.
vGridView.Columns.Add(bnfield);


Can you please "show me" how I could add an ID and Onclick attribute
to these tags?

Thanks.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top