locked
Use a user control's public member variable to set a SQL parameter? RRS feed

  • Question

  • User260076833 posted

    Hello,

    I have a user defined control "ReportList", which should show a list of reports, based on a selected category.
    The control is instantiated like this:

    <cmp:ReportList runat="server" ID="lst_Report" category="rooms" />
    

    Here, the user control is instantiated with the category "rooms".

    The user control's code behind looks like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace MyProject
    {
        public partial class ReportList : System.Web.UI.UserControl
        {
            public String div
            {
                get
                {
                    return (src_Report.SelectParameters["category"].DefaultValue);
                }
                set
                {
                    src_Report.SelectParameters["category"].DefaultValue = value;
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack)
                    lvw.DataBind();
            }
    
            protected void lvw_DataBinding(object sender, EventArgs e)
            {
                // doesn't work: category is null here
                // src_Report.SelectParameters["category"].DefaultValue = value;
            }
        }
    }

    I want to set the SQLDataSource parameter "category" based on the public member variable "category".

    First, I tried to do this in the ListView's DataBinding event, but here, the category variable is null.
    Then, I tried to do it in the "set" method for the category variable. This works, when I open the page, but when I navigate around and return to the page again, the SQL parameter is "forgotten" and the member variable is null again.

    How would you assure that the SQL parameter "category" is always set to the value of the category variable passed on control instantiation?

    Thanks
    Magnus

    Thursday, November 23, 2017 6:57 AM

Answers

  • User347430248 posted

    Hi Yeoman,

    try to fetch and set new value of session on page_Load and try to pass that value to other functions whenever you need it.

    or try to create a function that fetch and set new value to session and use it on multiple pages.

    Regards

    Deepak 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 8:23 AM

All replies

  • User347430248 posted

    Hi  Yeoman,

    you had mentioned that,"This works, when I open the page, but when I navigate around and return to the page again, the SQL parameter is "forgotten" and the member variable is null again. How would you assure that the SQL parameter "category" is always set to the value of the category variable passed on control instantiation?"

    here, I think that you need to use state management techniques. with the help of that you can preserve the value and access and use that value on any page of the application.

    there are (1) server side state management (2) Client side state management techniques

    Client side

    1. Hidden Field
    2. View State
    3. Cookies
    4. Control State
    5. Query Strings

    Server side

    1. Session
    2. Application

    below is an example of Session:

    Session["Count"] = Convert.ToInt32(Session["Count"]) + 1;//Set Value to The Session
    Label2.Text = Session["Count"].ToString(); //Get Value from the Sesion 

    you can use it in your code to maintain the value which can solve your issue.

    References:

    ASP.NET State Management Overview

    State Management in ASP.NET - Introduction

    A Beginner's Tutorial on ASP.NET State Management

    Regards

    Deepak

    Friday, November 24, 2017 6:31 AM
  • User260076833 posted

    Dear Deepak,

    thank you very much!

    I have realized the state persistance using the Session like this:

        public partial class ReportList : System.Web.UI.UserControl
        {
            private const String ctx_Division = "ctx_Division";
    
            public String div
            {
                get
                {
                    String t = (String) Session[ctx_Division];
                    return (t);
                }
                set // this setter seems to be called only once per instantiation of ReportList
                {
                    Session[ctx_Division] = value;
                    src_Report.SelectParameters["div"].DefaultValue =
                    (String) Session[ctx_Division];
                }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                src_Report.SelectParameters["div"].DefaultValue =
                (String)Session[ctx_Division];
    
                if (IsPostBack)
                    lvw.DataBind();
            }
    
    ...

    The situation is that I have different aspx pages using the same ReportList widget:

    Page1.aspx:

    ...
    <cmp:ReportList runat="server" ID="lst_Report" div="rooms" />
    ...

    Page2.aspx:

    <cmp:ReportList runat="server" ID="lst_Report" div="persons" />

    Now when I open Page1.aspx, I get the list of rooms, which is correct. When I then open Page2.aspx, I get the list of persons, which is also correct.
    But when I then open Page1.aspx again, I still see a list of persons, which is wrong.

    It seems that the parameter is passed only when the widget is first instantiated. Then, the session variable is set. But when the widget is used a second time, the setter for the parameter isn't called.

    I cannot use different session variables because the state management must reside inside the user defined control.

    Any ideas?

    Thanks
    Magnus

    Tuesday, November 28, 2017 12:56 PM
  • User347430248 posted

    Hi Yeoman,

    try to fetch and set new value of session on page_Load and try to pass that value to other functions whenever you need it.

    or try to create a function that fetch and set new value to session and use it on multiple pages.

    Regards

    Deepak 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 8:23 AM
  • User260076833 posted

    Thanks,

    it works now!


    Magnus

    Wednesday, November 29, 2017 2:40 PM