user2341787
user2341787

Reputation:

How to fetch the Value of a LinkButton in a GridView Template Field

I Have a LinkButton In a TemplateField In a GridView. The LinkButton is bound to database which shows the time save in database.All I want is to fetch each linkbutton time value and compare it to current time and if linkbutton time is less than current time ,make it disable.

Any pointers will be helpful. Thanks in advance.

 <asp:GridView ID="grdview"  AutoGenerateColumns="false" runat="server" OnRowCommand="grdview_RowCommand">
      <Columns>
           <asp:BoundField DataField="AudiName" DataFormatString="Audi {0}" HeaderText="Audi Name" />
           <asp:TemplateField HeaderText="StartTime">
                <ItemTemplate>
                     <asp:LinkButton ID="lnkmovietime" runat="server" 
                          Text='<%# Eval("StartTime") %>'  
                          CommandName="time" 
                          CommandArgument='<%#Eval("AudiID") %>'>     
                     </asp:LinkButton>
                     <asp:HiddenField runat="server" ID="hf1" Value='<%#Eval("StartTime")%>' />
                </ItemTemplate>
           </asp:TemplateField>
      </Columns>
 </asp:GridView>

Upvotes: 0

Views: 1958

Answers (2)

afzalulh
afzalulh

Reputation: 7943

You can set the Enabled property of LinkButton comparing with the current time:

 <asp:LinkButton ID="lnkmovietime" runat="server" 
      Text='<%# Eval("StartTime") %>' 
      Enabled = '<%# ((DateTime)Eval("StartTime")< DateTime.Now )? false : true %>'           
      CommandName="time" CommandArgument='<%#Eval("AudiID") %>'>
 </asp:LinkButton>

Upvotes: 0

Mun
Mun

Reputation: 14328

You can use the OnRowDataBound event to hook into the data binding and disable the button.

Form

<asp:GridView ID="MyGridView"
              OnRowDataBound="MyGridView_RowDataBound"
              runat="server">
....
</asp:GridView>

CodeBehind

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Find the button
        var linkButton = (LinkButton) e.Row.FindControl("MyLinkButton");

        // Toggle enabled/disabled based on time
        // Using the button text is probably a bad idea, but is used here for demonstration purposes
        linkButton.Enabled = (Convert.ToDateTime(linkButton.Text) > DateTime.Now);
    }
}

The code above has not been tested, but should give you an idea of how you can approach this.

Upvotes: 1

Related Questions