Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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,

C#
// 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!
Posted

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.
C#
public string L1Subject
{
    get { return ViewState["Subject"]!=null?ViewState["Subject"].ToString():null;}
    set { ViewState["Subject"]=value; }
}


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;
through which you are setting the value of Subject1 again
set { Subject1 = Subject1 != null ? Subject1 : null; }
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.
 
Share this answer
 
v4
Comments
snsrkrishna 16-Oct-12 1:21am    
thanks for the points (set{}), which you have stated. please refer my reply to "Marcus Kramer".
TheCoolCoder 16-Oct-12 2:05am    
Your question ("OUT" value is having state only in SelectedIndexChanged event then how can i access the value out side of the block) answers itself , you have to maintain the out parameter's state manually. A simple public property of a usercontrol doesnt maintain its state across postbacks unless you make it do so. One method to maintain a property's state is what i have specified ofcourse at the expense of increased load to page by adding a viewstate variable. There are other workarounds like using a hiddenfield or implement LoadViewState, TrackViewState and SaveViewState for the control.Refer this
snsrkrishna 16-Oct-12 8:17am    
thank you,
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.
 
Share this answer
 
Comments
snsrkrishna 16-Oct-12 1:18am    
"After postback you have to set the value of L1Subject from the client side object that contains the value"

thanks for response, may i know how to set value "after postback", as you said the "OUT" value is having state only in SelectedIndexChanged event, then how can i access the value out side of the block, here i decalre subject1 as public variable
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 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900