locked
FormView Conditional Format Error RRS feed

  • Question

  • User-1314346660 posted

    Hello Experts,

    I have a FormView (FormView1) that is populated when the user selects a value in a dropdown list (ddList1). This all works ok. I am trying to apply a background colour to a label in FormView1 when the Value = "Y". This works ok.

    The problem I have is when the user selects a value in ddList1 that has no records in FormView1 then the page errors rather than just appearing with no data.

    Here is my c#

                Label lbl1 = (Label)FormView1.FindControl("MyLabel");
                if (lbl1.Text == "Y")
                {
                    lbl1.BackColor = System.Drawing.Color.Green;
                }
                else lbl1.BackColor = System.Drawing.Color.Red;

    I really appreciate any help someone could give me on this one!

    Billson3000

    Monday, October 21, 2019 6:12 PM

Answers

  • User-1716253493 posted

    Try check FormView1.PageCount > 0 before FindControls inside it

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2019 12:18 AM
  • User-1314346660 posted

    Here is my full cs corrected following advice from oned_gk. I am currently trying this on FormView Pre-render. This now works. Thanks very much oned.

    protected void FormView1_PreRender(object sender, EventArgs e)
            {
    Label lbl1 = (Label)FormView1.FindControl("MyLabel"); if (FormView1.PageCount > 0 && lbl1.Text == "Y") { lbl1.BackColor = System.Drawing.Color.Green; }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2019 5:12 AM

All replies

  • User-1716253493 posted

    Try check FormView1.PageCount > 0 before FindControls inside it

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2019 12:18 AM
  • User288213138 posted

    Hi Billson3000,

    The problem I have is when the user selects a value in ddList1 that has no records in FormView1 then the page errors rather than just appearing with no data.

    According to your description, I can't reproduce your question.

    And since FormView is populated by the user select a value in a dropdownlist, then in what scene FormView will not record?

    My suggestion is to set breakpoints and debug your code when formview has no records.

    If you still can't solve the problem, please post your aspx and aspx.cs code.

    Best regards,

    Sam

    Tuesday, October 22, 2019 1:43 AM
  • User-1314346660 posted

    Here is my full cs corrected following advice from oned_gk. I am currently trying this on FormView Pre-render. This now works. Thanks very much oned.

    protected void FormView1_PreRender(object sender, EventArgs e)
            {
    Label lbl1 = (Label)FormView1.FindControl("MyLabel"); if (FormView1.PageCount > 0 && lbl1.Text == "Y") { lbl1.BackColor = System.Drawing.Color.Green; }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 22, 2019 5:12 AM