0

I am trying to create Anchors in code behind and delete buttons as well for each Anchor. I dont know if i can call an event handler for each one of the link with an ID so i can delete the row specified from the database. This is what i have done. I created an anchor and an ASp button but not sure how i can call it with that ImageID. Is it possible? If so How? Thanks a lot in advance!! This is in C#, asp.net.

 HtmlAnchor apdf = new HtmlAnchor();
 apdf.ID = Guid.NewGuid().ToString("N");
 string ImageID = "";
 if (dsreport != null && dsreport.Tables[0].Rows.Count > 0)
 {
      apdf.InnerText = dsreport.Tables[0].Rows[0]["ImageName"].ToString();
      apdf.Attributes.Add("style", "font-weight: bold; font-size: 13px; margin: 0px;    font-family: Arial; color: #1e7c9b; text-decoration: underline");
      apdf.Target = "_blank";
      ImageSalesID = dsreport.Tables[0].Rows[0]["ImageID"].ToString();
      apdf.HRef = "PDFdownload.aspx?ID=" + ImageID;

 }   
 Button btnDelete = new Button();
 btnDelete.ID = Guid.NewGuid().ToString("N");
 btnDelete.OnClick += Eventhandler;
 btnDelete.Text = "Delete";
2
  • not sure exactly what you asking but you can know what button is cliked by reading the id of the clicked button Commented Apr 18, 2012 at 21:48
  • Don't create these in code behind. Use a Repeater and create a Command button and use the Repeater.ItemCommand and set the CommandArgument instead. It's cleaner, uses less code, is more standard, and easier. msdn.microsoft.com/en-us/library/… (Or use a DataList if you prefer). Commented Apr 18, 2012 at 21:51

2 Answers 2

2

I hope this may help you in some way

 Button btnDelete = new Button();
    btnDelete.Click += new EventHandler(button_Click);

    protected void button_Click (object sender, EventArgs e)
    {
        Button button = sender as Button;
        string buttonid = button.ID.ToString()
        // identify which button was what row to update based on id clicked and perform necessary actions
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! It helps me go half way. Is it possible to send something in new EventHandler(button_Click) like an ImageID or something??
0

From what I can gather, you're pressing a button, and then wanting to delete something that's related to said button; if this is the case I would recommend using the CommandArgument property on a button. You can also specify the CommandName as well which can assist in determine what type of action to occur. Here's a quick example:

<asp:Button ID="btnDelete" runat="server" CommandName="DeleteRow" CommandArgument="1" Text="Delete Row 1" OnClick="DeleteRow" />
<asp:Button ID="btnDelete" runat="server" CommandName="DeleteRow" CommandArgument="2" Text="Delete Row 2" OnClick="DeleteRow" />
<asp:Button ID="btnDelete" runat="server" CommandName="DeleteRow" CommandArgument="3" Text="Delete Row 3" OnClick="DeleteRow" />

All 3 buttons will trigger the same event. Then within the actual event itself you can check what the command arguments were on the sender, by casting it as a Button, like so.

protected void DeleteRow(object sender, EventArgs e)
{
    var buttonClicked = sender as Button;
    var rowId = buttonClicked.CommandArgument;
    var action = buttonClicked.CommandName;

    // do something depending on the action and the argument
}

Hope this helps. :)

Comments

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.