0

I'm displaying a gridview with several entries of data, one of the data fields is date, I want the user to be able to click any date on the gridview, and use it to start a process on the same page, which would include hiding the gridview and displaying general info regarding that specific date, no insert, update or anything database related, only a call to a process. The same thing being done manually right now by selecting from a calendar and clicking a regular button.

How can I make this field only a linkbutton?

I already tried adding the field manually on ASP this way:

<asp:GridView ID="myGrid" runat="server" CssClass="Gridview">
 <Columns>
  <asp:HyperLinkField Text="test"  />
 </Columns>
</asp:GridView>

But it only creates a new field which is something I don't need. Also it must be done on code-behind since that's where I'm creating the datatable which later gets sent to the gridview, like this:

Dim myTable As DataTable = New DataTable
Dim column As DataColumn
Dim row As DataRow

column = New DataColumn()
column.ColumnName = "ID"
myTable.Columns.Add(column)

column = New DataColumn()
column.ColumnName = "Date"
myTable.Columns.Add(column)

For myRow As Integer = 0 To days
 row = myTable.NewRow()
 row("ID") = "Some ID"
 row("Date") = datevalue.AddDays(myRow).ToString("dd/MM/yyyy")
 myTable.Rows.Add(row)
Next

myGrid.DataSource = myTable
myGrid.DataBind()

Everything works fine, but I somehow need to make that date field a linkbutton, any ideas?

user1676874
  • 875
  • 2
  • 24
  • 45

1 Answers1

1

You should use a TemplateField for this.

<Columns>
  <asp:TemplateField>
     <ItemTemplate>
        <asp:LinkButton id="LinkButton1" Text="Click Me"            OnClick="LinkButton_Click" runat="server"/>
      </ItemTemplate>
     </asp:TemplateField>
</Columns>

Code-behind:

void LinkButton_Click(Object sender, EventArgs e) 
{
   // Insert your code
}

You probably want to have specific information based on the row the button was clicked. You could use a Button and set the CommandArgument to an identifier:

<asp:Button ID="Button1" runat="server" Text="Click me"  CommandArgument='<%#Eval("YourColumn") %>'/>
citronas
  • 19,035
  • 27
  • 96
  • 164