locked
Deleting weight record RRS feed

  • Question

  • I am displaying weight data in a table and for each record, I am creating a delete LinkButton programatically and associating it with a CommandArgument - btnLnk_Click . When I click on 'delete' LinkButton for any record, it should run btnLnk_Click method to delete that record, but it is not happening. The control does not go to btnLnk_Click method. Please check my code and suggest correction. The same logic works for deleting medications record.

     

     

    void PopulateWeightMeasurementTable()

     

    {

    .......

    TableCell deleteCell = new TableCell();
    LinkButton btnLnk = new LinkButton();
    btnLnk.ID = weight.Key.Id.ToString();
    btnLnk.Text = "Delete ";
    btnLnk.Visible = true;
    btnLnk.CommandArgument = weight.Key.Id.ToString();
    btnLnk.Click += new EventHandler(btnLnk_Click);
    btnLnk.Attributes.Add("onclick", "javascript:if(confirm('Do you really wish to delete record # "+counter+" ?') == false) return false;");
    deleteCell.HorizontalAlign = HorizontalAlign.Center;
    deleteCell.Controls.Add(btnLnk);
    row.Cells.Add(deleteCell);

    .....

     

    }

    protected void btnLnk_Click(object sender, EventArgs e)
        {
            string KeyID = ((LinkButton)sender).CommandArgument;
            Guid ItemID = new Guid(KeyID);
            Weight wt = (Weight)PersonInfo.SelectedRecord.GetItem(ItemID, HealthRecordItemSections.Xml);
            try
            {
                this.PersonInfo.SelectedRecord.RemoveItem(wt);
            }
            catch (Exception e1)
            {
                Status.Text = e1.ToString();
            }
            Status.Text = "Record Deleted";
            PopulateWeightMeasurementTable();

        }

    Monday, January 4, 2010 8:13 PM

Answers

  • Hi PatD,

    As this method  PopulateWeightMeasurementTable() is placed in !IsPostBack  condition, The code which is there in PopulateWeightMeasurementTable() method is not being executed and that is the reason btnLnk_Click event is not fired.

    So please place the code directly in Page_Load event to fire btnLnk_Click event.

    Regards,
    Madan Kamuju

    Wednesday, January 6, 2010 9:15 PM

All replies

  • Hi PatD,

    I have tried to repro the issue with your code and Issue is not reproing. Could you please check the Postback Conditions in the code where you are calling PopulateWeightMeasurementTable() method.

    Please let me know if you are still facing the issue .

    Hope this helps you.

    Regards,
    Madan
    Monday, January 4, 2010 10:07 PM
  • I am doing PopulateWeightMeasurementTable() in Page_Load method, on a button hit method and in the btnLnk_Click method. The probem is that the control never goes in btnLnk_Click method.
    if (!IsPostBack)
            {
               ....

            if (user_FromDate.Text != string.Empty && user_ToDate.Text != string.Empty)
            {
                PopulateWeightMeasurementTable();
                ViewState["RowCreated"] = "1";
            }

            }
    Wednesday, January 6, 2010 7:13 PM
  • Hi PatD,

    Can you please keep the code in IsPostback condition to fire the btnLnk_Click  event.

    Please see the below code.

    if(IsPostback)
    {

    .......
         
     if (user_FromDate.Text != string.Empty && user_ToDate.Text != string.Empty)
            {
                PopulateWeightMeasurementTable();
                ViewState["RowCreated"] = "1";
            }

    }

    Please let me know if you face the isssue still.

    Hope this helps you.

    Regards,
    Madan K
    Wednesday, January 6, 2010 7:39 PM
  • Sorry, I did not understand what you want me to try...How do I fire btnLnk_Click  event in the IsPostback? This needs to be fired when 'Delete' btnlnk is clicked.
    Wednesday, January 6, 2010 8:53 PM
  • Hi PatD,

    As this method  PopulateWeightMeasurementTable() is placed in !IsPostBack  condition, The code which is there in PopulateWeightMeasurementTable() method is not being executed and that is the reason btnLnk_Click event is not fired.

    So please place the code directly in Page_Load event to fire btnLnk_Click event.

    Regards,
    Madan Kamuju

    Wednesday, January 6, 2010 9:15 PM
  • Thank You. That works. :)
    • Proposed as answer by Madan kamuju Wednesday, January 6, 2010 9:25 PM
    Wednesday, January 6, 2010 9:21 PM