无法从Web用户控件访问aspx的公共属性值

无法从Web用户控件访问aspx的公共属性值

本文介绍了无法从Web用户控件访问aspx的公共属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得一个字符串值,该字符串值是函数调用的"out参数"的结果,该值是ascx页中DropDownList selectedIndexChange()事件的结果,在调试值中,当我从DB中检索值时,我试图从主机页面访问,我得到的是空值.

此ascx中的所有控件都包装为updatePanel控件,

I''m unable get a sting value which is the result of a "out parameter" of a function call which is result of DropDownList selectedIndexChange() event in ascx page, In debugging value is retriving from DB, when i''m tryin to access from host page, i''m getting null value.

all controls in this ascx are wrapped to updatePanel control,

// Class of WebUserControl, Class name is TagQs
    public partial class TagQs : System.Web.UI.UserControl
    {
        public string ConString, Subject1, Subject2, Subject3, Subject4, Subject5, description;

        public string L1Subject
        {
            get { return Subject1;}
            set { Subject1 = Subject1 != null ? Subject1 : null; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // First time Page loading only
                //Subject1 = Subject2 = Subject3 = Subject4 = Subject5 = null;
            }
            else
            {
                // Partial page loading
                //Desc.Text = description;
            }

     }

// selectIndexChanged even of WUC (TagQs)        protected void DDLSubL1_SelectedIndexChanged(object sender, EventArgs e)
        {
                 // populate takes arguments and return result with a "out" parameter
            if (populate("PopulateL2Subjects", DDLSubL2, DDLSubL1, out Subject1))
            {
                DDLSubL2.Visible = true;
                this.L1Subject = Subject1;      // here working perfectly, till the postback
                ViewState["sub"] = Subject1;
            }
            else
            {
                DDLSubL2.Visible = false;
                DDLSubL2.Items.Clear();
                DDLSubL2.Items.Add("Select chapter");
            }
        }

///============================

// Code of HOST page
 public partial class Obj : System.Web.UI.Page
    {

       private string L1, L2, L3, L4, L5;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {


            }
            else
            {

            }
        }

     }

// Here i'm accessing Property value
 //TagQ is the ID of WUC in host page   protected void BtnPreview_Click(object sender, EventArgs e)
        {

            L1 = TagQ.L1Subject;        // returns null always,
            L1 = (string)ViewState["sub"];
        }





Help me please!

推荐答案

public string L1Subject
{
    get { return ViewState["Subject"]!=null?ViewState["Subject"].ToString():null;}
    set { ViewState["Subject"]=value; }
}



同样,在属性内执行的操作也没有意义,您正在通过再次将Subject1的值作为OUT参数传递给函数



Also what you are doing inside the property doesnt make sense, you are setting the value of Subject1 by passing it as an OUT parameter to the function

populate("PopulateL2Subjects", DDLSubL2, DDLSubL1, out Subject1)

来设置它,而您正在使用

again you are using

this.L1Subject = Subject1;

,通过它再次设置Subject1的值

through which you are setting the value of Subject1 again

set { Subject1 = Subject1 != null ? Subject1 : null; }

同样,您在属性的设置区域内使用的逻辑有点奇怪,我应该说(对不起),您已检查Subject1是否为null,如果为null则将其设置为null,否则将再次使用其自身的值进行设置!! !这意味着您的set块什么都不做",如果您只将set块留在其中,只要将set块留空(set {}),这不会有任何区别.

如果我错了,请随时纠正我.

Also the logic you have used inside the set area of property is kinda funny i should say(sorry), you have checked if Subject1 is null and if null you set it with null else you set it again with its own value!!! that means ''your set block does nothing'' and it wouldnt make any difference if you left your set block empty( set { } ) provided you are only doing what you are doing now inside it.

Feel free to correct me if i am wrong.



public string L1Subject
        {
            get { return Subject1 != null ? Subject1 : Subject1 = s1.Value; }
            set { Subject1 = value; }
        }



.ASPX页面
L1 = Tagq.L1Subject.ToString();


*这里s1是ascx中的隐藏字段,
*并且L1是aspx
中的字符串* Tagq是ascx的类名称

感谢TheCoolCoder&再次是Marcus Kramer.



.ASPX page
L1 = Tagq.L1Subject.ToString();


* here s1 is a hidden field in ascx,
* and L1 is a string in aspx
* Tagq is class name of ascx

Thanking TheCoolCoder & Marcus Kramer again.


这篇关于无法从Web用户控件访问aspx的公共属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:55