Is that possible to add some server script to DataGrid?

G

Guest

hi, all
I just got a question here when I am using the datagrid, my code like this:
<asp:DataGrid id="DataGridBanner" runat="server" style=....>
.....
<Columns>
<% if(m_adUser.IsWebMaster()) { %>
<asp:EditCommandColumn Visible='<%# m_adUser.IsWebMaster()%>'
ButtonType="PushButton" UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<% } %>
.......
because I only want to show the edit button to web master.
But when I run it, it shows up an error and says, " Code blocks are not
supported in this context", So, can i do this in this page, or I must write
another similar page especially for web master?

Thanks
 
E

Eliyahu Goldin

You don't need any ifs. You need just to databinding Visible property, that
is what you are already trying to do.

<Columns>
<asp:EditCommandColumn Visible='<%# IsWebMaster %>'
ButtonType="PushButton" UpdateText="Update" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>

IsWebMaster should be a protected or public property of your page class. It
should return true if the user is the web master.

Eliyahu
 
B

Brock Allen

You can also handle the DataGrid's ItemDataBound event. The e.Item passed
as a parameter is the row that was just populated. You can then manipulate
the controls in that row.
 
G

Guest

thanks,
I found some fields, it works. But for gernal gound column, like this:
<asp:BoundColumn Visible='<%# IsWebMaster %>' DataField="CreatorID"
HeaderText="CreatorID"></asp:BoundColumn>
it says:
CS0117: 'System.Web.UI.WebControls.BoundColumn' does not contain a
definition for 'DataBinding'
I think this is reasonale. So probably I have to convert it to template and
do it like others columns. Or move all of this logic to the data bind event
routine.
 
P

Patrick Olurotimi Ige

So try changing it to Templates
and use DataGrid's ItemDataBound event by manipulating the e.item.
That could save your time.
Patrick
 

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,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top