Question

Aspx form:

<asp:Table ID="viewV" runat="server">
  <asp:TableRow>
    <asp:TableHeaderCell>Name</asp:TableHeaderCell>
    <asp:TableHeaderCell>Type</asp:TableHeaderCell>
  </asp:TableRow>       
</asp:Table>

Code below:

vRepository getV = new vRepository();

var viewAllV = getV.GetV();

foreach (tblV singleV in viewAllV)
{
    TableRow tr = new TableRow();

    // Create four new cells for each row within the database

    TableCell vName = new TableCell();
    TableCell vType = new TableCell();

    // Attached values to each of these cells

    vName.Text = singleV.vName;
    vType.Text = singleV.tblVType.vName;

    // Add the cells to the table

    tr.Cells.Add(vName);
    tr.Cells.Add(vType);

    // Add the row to the table

    viewV.Rows.Add(tr);
}

I am currently populating a asp:table using LINQ, this is working fine.

public IQueryable<tblV> GetV()
{
    return (from getAllV in dc.tblV
            where getAllV.deleted != 1
            orderby getAllV.vName
            select getAllV).DefaultIfEmpty();
}

I have tried the GridView/ListView/DetailsView but can’t seem to access the GetV, I can only access the actual table.

I want to add a edit/delete button to the end of every row, so that the user can select the row they wish to edit/delete.

Once the user has selected either the edit/delete button, this should then access a different view (one for edit, one for add - within the same aspx file).

For example:

Name    Type

Example 1   Type 1  Edit Button Delete Button

Example 2   Type 1  Edit Button Delete Button

I want to be able to select the Edit Button for ‘Example 1’. This should then:

  1. Take me to the ‘edit view’,
  2. Auto populate the two text fields with the correct information,
  3. Update row (within database) with new information using unique ID(PK).

How came I produce multiple edit/delete buttons which somehow hold the unique ID for that row?

I thought it would be something like this:

TableCell vEdit = new TableCell();
vEdit.Button = singleV.vID;
tr.Cells.Add(vEdit);
Was it helpful?

Solution

If you really want to "hard-code" it, use ASP.NET MVC. Otherwise, you really should use some sort of databound control.

That said, if you insist on this approach you can probably do something like this:

Button editButton = new Button();
editButton.Text = "Edit";
editButton.CommandArgument = singleV.vID.ToString();
editButton.CommandName = "Edit";
editButton.OnCommand += new CommandEventHandler(EditButton_Command);

TableCell vEdit = new TableCell();
vEdit.Controls.Add(editButton);

tr.Cells.Add(vEdit);

Then from your event handler's CommandEventArgs you can retrieve the argument and change your view accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top