Ask a Question related to ASP.NET Data Grid Control, Design and Development.
-
Carlos CR #1
Dinamically changing the Text Field of a LinkButton in a DataGrid
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.Da taItem,
"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
Carlos CR Guest
-
Changing to British date form a US style (text) field
Hello all I have a document tracking database set up in FM Pro. One of the fields I use is a "Due Date" field which is scripted to add two days... -
Retreving the text value of LinkButton in template column of a datagrid
Hi, I'm tring to use template column in a datagrid and added a link button in item template. I'm loading the value of link button dynamically... -
Populating a DataGrid dinamically
Hi Tracy, well here we go again How can i put the data in a DataGrid in an especific column and row for example make a function where i can say... -
Changing text in a text field
I have another noob question, I'm trying to change the text in a filed depending on what the user clicks on. What's the line of code I need to... -
Changing button's URLs based upon text field
I have created a quiz using the Coursebuilder extensions in DWMX, which puts their score in a text field on the page (no data base is used). What I... -
Rick Spiewak #2
Re: Dinamically changing the Text Field of a LinkButton in a DataGrid
Are you re-binding the grid? You should look at changing the linkbutton text
in the itemdatabound event.
"Carlos CR" <Ccrobles2QUITAME@wanadoo.com> wrote in message
news:Xns94DEEBC5ED108CarlosCR@130.133.1.4...> 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.Da taItem,
> "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
Rick Spiewak Guest
-
Carlos CR #3
Re: Dinamically changing the Text Field of a LinkButton in a DataGrid
"Rick Spiewak" <rickspiewak@mindspring.com> wrote in
news:ukNXPxXMEHA.2252@tk2msftngp13.phx.gbl:
Hello,> Are you re-binding the grid? You should look at changing the
> linkbutton text in the itemdatabound event.
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[b].ID != null && (cc[b].ID == sortButtonID) ) {
LinkButton bu = (LinkButton)cc[b];
if(bu.ID.EndsWith("ASC") ) {
bu.Text = upSelArrow;
} else {
bu.Text = downSelArrow;
}
ViewState["sortButtonID"] = "";
}
}
}
}
}
Thanks again for your answer.
Carlos CR
Carlos CR Guest
-
Rick Spiewak #4
Re: Dinamically changing the Text Field of a LinkButton in a DataGrid
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" <Ccrobles2QUITAME@wanadoo.com> wrote in message
news:Xns94DF84808D6DCCarlosCR@130.133.1.4...> "Rick Spiewak" <rickspiewak@mindspring.com> wrote in
> news:ukNXPxXMEHA.2252@tk2msftngp13.phx.gbl:
>>> > 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[b].ID != null && (cc[b].ID == sortButtonID) ) {
> LinkButton bu = (LinkButton)cc[b];
> if(bu.ID.EndsWith("ASC") ) {
> bu.Text = upSelArrow;
> } else {
> bu.Text = downSelArrow;
> }
> ViewState["sortButtonID"] = "";
> }
> }
> }
> }
> }
>
> Thanks again for your answer.
>
> Carlos CR
Rick Spiewak Guest
-
Carlos CR #5
Re: Dinamically changing the Text Field of a LinkButton in a DataGrid
"Rick Spiewak" <rickspiewak@mindspring.com> wrote in
news:uoWYSpcMEHA.3472@TK2MSFTNGP09.phx.gbl:
Hello Again Rick,> 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.
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
Carlos CR Guest



Reply With Quote

