Dinamically changing the Text Field of a LinkButton in a DataGrid

C

Carlos CR

Hello,

I've run into a problem and I'm not sure if this is a limitation of
dotNet or I'm doing something bad,

I've a TemplateColumn in a DataGrid with a LinkButton of which I would
like to dinamically change the Text Field.

The DataGrid definition is like this:

<form runat="server">
<asp:DataGrid id="ClientsDataGrid" runat="server"
width="100%"
AutoGenerateColumns="false"
[...]
<asp:TemplateColumn
Visible="True">
<HeaderTemplate>
Login
<asp:LinkButton ID="loginASC" CommandName="Sort"
CommandArgument="login ASC" runat="server" Text="<%# upArrow %>" />
<asp:LinkButton id="loginDESC" CommandName="Sort"
CommandArgument="login DESC" runat="server" Text="<%# downArrow %>" />
</HeaderTemplate>
<ItemTemplate>
<a href="edit.aspx?id=<%#DataBinder.Eval(Container.DataItem,
"id_cliente")%>"><%# DataBinder.Eval(Container.DataItem, "login") %></a>
</ItemTemplate>
</asp:TemplateColumn>

The EventHandler for the LinkButton commands looks like this:

void ItemCommand(Object sender, DataGridCommandEventArgs e) {
if(e.CommandName == "Sort") {
SortField = (String) e.CommandArgument;
if(((LinkButton)e.CommandSource).ID.EndsWith("ASC") ) {
Trace.Write("New value=" + upSelArrow );
((LinkButton)e.CommandSource).Text = upSelArrow;
} else {
Trace.Write("New value=" + downSelArrow);
((LinkButton)e.CommandSource).Text = downSelArrow;
}
}
BindGrid();
}

If I turn on tracing I can see the "New value.." message, but the TExt
property doesn't change and it gets the declarative value, instead of the
one given in the method.

What I'm doing wrong?

Thanks in advance for any help..

Carlos CR
 
R

Rick Spiewak

Are you re-binding the grid? You should look at changing the linkbutton text
in the itemdatabound event.
 
C

Carlos CR

Are you re-binding the grid? You should look at changing the
linkbutton text in the itemdatabound event.

Hello,

First of all, thank you very much for answering.

As for the problem, yes I'm rebinding the grid..

As you suggested, now I change de text in the itemdatabound event and
it works, but I'm wondering if there is an {easier|more efective} way of
doing it.

The code I use to obtain the linkButton is this:

void ItemBound(Object sender, DataGridItemEventArgs e) {
string sortButtonID = (string)ViewState["sortButtonID"];

if(e.Item.ItemType.ToString() == "Header") {
for(int a=0; a < e.Item.Cells.Count; a++) {
ControlCollection cc = e.Item.Cells[a].Controls;
for(int b=0; b < cc.Count; b++) {
if(cc.ID != null && (cc.ID == sortButtonID) ) {
LinkButton bu = (LinkButton)cc;
if(bu.ID.EndsWith("ASC") ) {
bu.Text = upSelArrow;
} else {
bu.Text = downSelArrow;
}
ViewState["sortButtonID"] = "";
}
}
}
}
}

Thanks again for your answer.

Carlos CR
 
R

Rick Spiewak

You should be able to use .FindControl to get to your control without
iterating over everything. Also, you can directly compare the item type
without converting to a string. This will also be easier to read, and run
faster, if you define a variable as a datagrid item, and set it to e.item.
Then, (for example) test for itm.ItemType = ListItemType.Header, and
itm.FindControl(sortButtonID) to get to your control.

Carlos CR said:
Are you re-binding the grid? You should look at changing the
linkbutton text in the itemdatabound event.

Hello,

First of all, thank you very much for answering.

As for the problem, yes I'm rebinding the grid..

As you suggested, now I change de text in the itemdatabound event and
it works, but I'm wondering if there is an {easier|more efective} way of
doing it.

The code I use to obtain the linkButton is this:

void ItemBound(Object sender, DataGridItemEventArgs e) {
string sortButtonID = (string)ViewState["sortButtonID"];

if(e.Item.ItemType.ToString() == "Header") {
for(int a=0; a < e.Item.Cells.Count; a++) {
ControlCollection cc = e.Item.Cells[a].Controls;
for(int b=0; b < cc.Count; b++) {
if(cc.ID != null && (cc.ID == sortButtonID) ) {
LinkButton bu = (LinkButton)cc;
if(bu.ID.EndsWith("ASC") ) {
bu.Text = upSelArrow;
} else {
bu.Text = downSelArrow;
}
ViewState["sortButtonID"] = "";
}
}
}
}
}

Thanks again for your answer.

Carlos CR
 
C

Carlos CR

You should be able to use .FindControl to get to your control without
iterating over everything. Also, you can directly compare the item
type without converting to a string. This will also be easier to read,
and run faster, if you define a variable as a datagrid item, and set
it to e.item. Then, (for example) test for itm.ItemType =
ListItemType.Header, and itm.FindControl(sortButtonID) to get to your
control.

Hello Again Rick,

it works!! Thank you very much.

The final code (just in case anyone is following the thread with the
same problem):

----------- 8< -------------- 8< -----------
void ItemBound(Object sender, DataGridItemEventArgs e) {
string sortButtonID = (string)ViewState["sortButtonID"];
// Make sure there is a sort field selected
if((sortButtonID != null) && // Make sure a sort field is selected
(e.Item.ItemType == ListItemType.Header) )
{
LinkButton lb = (LinkButton)e.Item.FindControl( sortButtonID );
if( lb != null ) {
if(lb.ID.EndsWith("ASC") ) {
lb.Text = upSelArrow;
} else {
lb.Text = downSelArrow;
}
}
}
}
----------- 8< -------------- 8< -----------

Carlos CR
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top