无法从Web用户控件访问aspx的公共属性值 [英] unable to access public property value from a web user control to aspx

查看:98
本文介绍了无法从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!

推荐答案

一种快速的解决方案是使用viewstate来存储和检索属性值,但是我不建议您使用它,而无需仔细检查它是否绝对必要.
A quick solution is to use viewstate to store and retrieve the property value, but i dont advise you use it without double checking if it is absolutely necessary.
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.


回发后,您必须从包含该值的客户端对象中设置L1Subject的值.如果不这样做,则L1Subject将保持为空.
请记住,asp.net是无状态的.通过在SelectedIndexChanged事件中设置值,您仅在请求到服务器的持续时间内设置它.下一次回发将再次从空白状态开始.
After postback you have to set the value of L1Subject from the client side object that contains the value. If you do not, L1Subject will remain null.
Remember that asp.net is stateless. By setting the value in the SelectedIndexChanged event, you are only setting it for the duration of the request to the server. The next postback will again start with a blank state.


在ascx中添加HiddenFiled并为控件启用viewstate并设置该值.最佳做法是,将公共属性添加到ascx,并通过指定"Classname.PropertyName"从aspx页面访问属性值.

.ASCX页面
Add a HiddenFiled in ascx and enable viewstate for the control, and set the value. For best practice, add a public property to ascx, and access property values from aspx page, by specifying "Classname.PropertyName".

.ASCX page
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的公共属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆