在共享点中回发(单击按钮)期间无法保留标签的值

发布于 2024-12-03 13:15:14 字数 1598 浏览 1 评论 0原文

在编写用于连接两个 Web 部件的代码时,我创建了两个标签。两个标签的文本值都是在 OnPreRender 方法中写入的。但是,我忘记在 CreateChildControl 方法中为一个标签添加控件。 因此,在调试时,我注意到,在回发后,我忘记添加其控件的标签没有保留该值,并且它显示空字符串。但是另一个我添加的控件的标签能够保留该值

protected override void CreateChildControls()
    {
        base.CreateChildControls();




        btnup.Text = "  Update";

        this.Controls.Add(lblid);//**If i add this, the label retains the value during post back , otherwise its null**


        this.Controls.Add(lblname);
        this.Controls.Add(lbldesig);
        this.Controls.Add(tbdes);
        this.Controls.Add(lblcomp);
        this.Controls.Add(tbcomp);
        this.Controls.Add(btnup);

        btnup.Click += new EventHandler(btnup_Click);
    }


protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (connectionInterface != null)
        {

            id = connectionInterface.parameter1;

            SPWeb mysite = SPContext.Current.Web;
            SPList mylist = mysite.Lists["UpdateList"];
            SPListItemCollection itemcol = mylist.Items;

            foreach (SPListItem itm in itemcol)
            {
                string nm = itm["Company_Id"].ToString();
                if (nm.Equals(id))
                {
                    lblid.Text = itm["Company_Id"].ToString();
                    lblname.Text = itm["Name"].ToString();
                    l
                }

            }


        }
        else
        {
            lblname.Text = "nothing is recieved!";
        }

    }

为什么它会这样?

While writing code for connecting two webparts,I created a two labels. the text value for both the labels were written in OnPreRender method. However, i forgot to add control for one label in CreateChildControl method.
So while debugging, i noticed that, after post back, the label whose control i forgot to add didn't retain the value and it was showing empty string.But the other label who's control i added was able to retain the value.

protected override void CreateChildControls()
    {
        base.CreateChildControls();




        btnup.Text = "  Update";

        this.Controls.Add(lblid);//**If i add this, the label retains the value during post back , otherwise its null**


        this.Controls.Add(lblname);
        this.Controls.Add(lbldesig);
        this.Controls.Add(tbdes);
        this.Controls.Add(lblcomp);
        this.Controls.Add(tbcomp);
        this.Controls.Add(btnup);

        btnup.Click += new EventHandler(btnup_Click);
    }


protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (connectionInterface != null)
        {

            id = connectionInterface.parameter1;

            SPWeb mysite = SPContext.Current.Web;
            SPList mylist = mysite.Lists["UpdateList"];
            SPListItemCollection itemcol = mylist.Items;

            foreach (SPListItem itm in itemcol)
            {
                string nm = itm["Company_Id"].ToString();
                if (nm.Equals(id))
                {
                    lblid.Text = itm["Company_Id"].ToString();
                    lblname.Text = itm["Name"].ToString();
                    l
                }

            }


        }
        else
        {
            lblname.Text = "nothing is recieved!";
        }

    }

Why is it behaving like this?

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

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

发布评论

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

评论(1

允世 2024-12-10 13:15:14

这是正常行为。如果您不将控件添加到 Controls 集合中,ASP.NET 框架将不会在回发中保留其值,因此会在回发期间丢失。

This is a normal behavior. If you don't add the control to Controls collection, ASP.NET framework will not preserve its value in postback and hence will be lost during postback.

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