0

I've got a formview whose load event just decided to stop working. I did some debugging and noticed that it was reaching the code, but for whatever reason, the attributes that I am adding in the load event are no longer displaying on the screen. It's as if something is happening after the formview's load event that is reloading it without any of my extra attributes. The only modification that I have done before it stopped working is that I added a session variable in the page before it. That should not cause such a drastic change.

Here is my code:

    protected void FormView1_Load(object sender, EventArgs e)
{
    RadioButton rbinjury = (RadioButton)FormView1.FindControl("rbinjury");
    RadioButton rbproperty = (RadioButton)FormView1.FindControl("rbproperty");
    RadioButton rbboth = (RadioButton)FormView1.FindControl("rbboth");
    RadioButton rbyes = (RadioButton)FormView1.FindControl("rbyes");
    RadioButton rbno = (RadioButton)FormView1.FindControl("rbno");
    RadioButton rbyes2 = (RadioButton)FormView1.FindControl("rbyes2");
    RadioButton rbno2 = (RadioButton)FormView1.FindControl("rbno2");
    RadioButton rbam = (RadioButton)FormView1.FindControl("rbam");
    RadioButton rbpm = (RadioButton)FormView1.FindControl("rbpm");

    TextBox txtdate = (TextBox)FormView1.FindControl("txtdate");
    DropDownList ddlhour = (DropDownList)FormView1.FindControl("ddlhour");
    DropDownList ddltime = (DropDownList)FormView1.FindControl("ddltime");
    if (FormView1.CurrentMode == FormViewMode.Insert || FormView1.CurrentMode == FormViewMode.Edit)
    {
        txtdate.Attributes.Add("onfocus", "unfocus();");
        locList.Attributes.Add("onChange", "postBack();");
        ddlhour.Items.Insert(0, new ListItem("Hour", "0"));
        ddlhour.Items.Insert(1, new ListItem("12", "12"));
        ddltime.Items.Insert(0, new ListItem("Minute", "0"));
        for (int i = 1; i < 12; i++)
        {
            String hour = Convert.ToString(i);
            ddlhour.Items.Add(new ListItem(hour, hour));
        }

        for (int i = 0; i < 61; i++)
        {
            String time = "";
            if (i < 10)
            {
                time = ":0" + Convert.ToString(i);
            }
            else
            {
                time = ":" + Convert.ToString(i);
            }
            ddltime.Items.Add(new ListItem(time, time));
        }
        //-----------------------------------------handle radio buttons----------------------------------------------------------------
        rbinjury.Attributes.Add("Onclick", "radio('rbinjury','result');");
        rbproperty.Attributes.Add("Onclick", "radio('rbproperty','result');");
        rbboth.Attributes.Add("Onclick", "radio('rbboth','result');");

        rbyes.Attributes.Add("Onclick", "radio('rbyes','inj');");
        rbno.Attributes.Add("Onclick", "radio('rbno','inj');");

        rbyes2.Attributes.Add("Onclick", "radio('rbyes2','dmg');");
        rbno2.Attributes.Add("Onclick", "radio('rbno2','dmg');");

        rbam.Attributes.Add("Onclick", "radio('rbam','time');");
        rbpm.Attributes.Add("Onclick", "radio('rbpm','time');");
    }}

Any idea what would cause the load event to stop working? If I place this same code in the page's save state complete event, it does work, but I should not have to...

Muhammad Akhtar
  • 50,838
  • 36
  • 132
  • 186
Aaron
  • 7,201
  • 12
  • 33
  • 37

1 Answers1

0

you need to use formview Databound event instead of formview load event to set values, try using this

 protected void frm_DataBound(object sender, EventArgs e) 
{
    if (frm.CurrentMode == FormViewMode.Edit) 
    {
      TextBox txtdate = (TextBox)frm.FindControl("txtdate");
      txtdate.Attributes.Add("", "");
    } 
}

Also check these threads. ASP.NET Can not Change Visibility of a Formview control FormView.FindControl(): object reference error

Community
  • 1
  • 1
Muhammad Akhtar
  • 50,838
  • 36
  • 132
  • 186