Dinamically changing the Text Field of a LinkButton in a DataGrid

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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:
    > 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
    Carlos CR Guest

  5. #4

    Default 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

  6. #5

    Default 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:
    > 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
    Carlos CR Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139