FormView_Load 被覆盖 C# ASP.NET

发布于 2024-08-08 03:04:58 字数 2878 浏览 8 评论 0原文

我有一个表单视图,其加载事件刚刚决定停止工作。我做了一些调试,发现它正在到达代码,但无论出于何种原因,我在加载事件中添加的属性不再显示在屏幕上。就好像在表单视图的加载事件之后发生了一些事情,在没有任何额外属性的情况下重新加载它。在它停止工作之前我所做的唯一修改是我在它之前的页面中添加了一个会话变量。这不应该导致如此剧烈的变化。

这是我的代码:

    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');");
    }}

知道什么会导致加载事件停止工作吗?如果我将相同的代码放置在页面的保存状态完成事件中,它确实可以工作,但我不必......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2024-08-15 03:04:58

您需要使用 formview Databound 事件而不是 formview load 事件来设置值,请尝试
使用此

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

同时检查这些线程。
ASP.NET 无法更改一个Formview控件
FormView.FindControl():对象引用错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文