1

I am creating a dynamic table for listing files.

It shows filename, filesize, dateModified columns. In addition, I have added one more columns: Delete.

The method which lists files of a folder in a table.

public void listFile()
{
    var dir = new DirectoryInfo(selectedFolder);

    Table fileTable = new Table();

    foreach (var file in dir.GetFiles())
    {
        TableRow tr = new TableRow();

        TableCell td1 = new TableCell();
        TableCell td2 = new TableCell();
        TableCell td3 = new TableCell();
        TableCell td4 = new TableCell();

        Label name = new Label();
        Label size = new Label();
        Label dateMod = new Label();
        LinkButton btn_delete = new LinkButton();

        name.Text = file.Name;
        size.Text = (file.Length / 1024) + " KB";
        dateMod.Text = file.LastWriteTime.ToLongTimeString();

        btn_delete.Text = "Delete";
        btn_delete.Click += new EventHandler(btn_delete_Click);

        td1.Controls.Add(name);
        td2.Controls.Add(size);
        td3.Controls.Add(dateMod);
        td4.Controls.Add(btn_delete);

        tr.Controls.Add(td1);
        tr.Controls.Add(td2);
        tr.Controls.Add(td3);
        tr.Controls.Add(td4);
    }

    filePanel.Controls.Add(fileTable);
}

protected void btn_delete_Click(object sender, EventArgs e)
{
    //Delete file
}

Now I want to delete the file when I click on the corresponding delete button. But problem is how will computer know which file to be deleted? I must pass the filename to the delete method.

5
  • Have you consider having a member variable holding the strings to the file then you can access that within the button Commented Sep 23, 2014 at 13:39
  • @karlsweeney how? can you explain? Commented Sep 23, 2014 at 13:40
  • wpf or winform or other? Commented Sep 23, 2014 at 13:41
  • 2
    @zkanoca : Have you consider using a repeater instead of creating the table from code behind? Commented Sep 23, 2014 at 13:43
  • @zkanoca: Have a look at my answer. Hope it will help you to solve your problem. Commented Sep 23, 2014 at 13:59

4 Answers 4

3

You can pass command argument to the link button as mentioned below:

btn_delete.CommandArgument =  [ID of the file]

and on click event of the link button you can access it as mentioned below:

protected void btn_delete_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    var id = btn.CommandArgument;
}

Reference: LinkButton.CommandArgument Property

Sign up to request clarification or add additional context in comments.

Comments

2

The answer from SpiderCode and RePierre about using the CommandArgument is a correct. It might however help you to understand that the CommandArgument is nothing more than a thin wrapper around the ViewState concept.

ViewState is one way how you can transport data between client and server. If you ever run into a similar problem and you don't have commandargument available you can resort to viewstate.

Take a look at the LinkButton.cs

public string CommandArgument {
            get {
                string s = (string)ViewState["CommandArgument"];
                return((s == null) ? String.Empty : s);
            }
            set {
                ViewState["CommandArgument"] = value;
            }

So you can either use the CommandArgument or use ViewState directly.

Comments

1

You can use the CommandArgument property of btn_delete to pass the path to the file:

btn_delete.CommandArgument = file.Name;

And in the event handler you just have to get the value as following:

protected void btn_delete_click(object sender, EventArgs e)
{
    var button = sender as Button;
    var filename = button.CommandArgument;
}

Comments

-2

You can do this by creating a UserControl which is inheritated from the main Control. In my example I extended a Textbox with an address variable.

In that way you bind a value to the control, which you can use later.

public class ExtendedTextBox : System.Windows.Forms.TextBox
{
    private int? _Address = null;

    [Description("Address of variable")]
    [DefaultValue(null)]
    public int? Address
    {
        get { return _Address; }
        set { _Address = value; }
    }
}

2 Comments

How does this fit in with their button press?
You should use the standard ButtonClickedEventHandler, So you can fetch the sender by the argument "sender".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.