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?
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
Hidden Field
View State
Cookies
Control State
Query Strings
Server side
Session
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.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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:
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.
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
33 Points
425 Posts
Use a user control's public member variable to set a SQL parameter?
Nov 23, 2017 06:57 AM|Yeoman|LINK
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:
Here, the user control is instantiated with the category "rooms".
The user control's code behind looks like this:
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
Contributor
2990 Points
1210 Posts
Re: Use a user control's public member variable to set a SQL parameter?
Nov 24, 2017 06:31 AM|Deepak Panchal|LINK
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
Server side
below is an example of Session:
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
33 Points
425 Posts
Re: Use a user control's public member variable to set a SQL parameter?
Nov 28, 2017 12:56 PM|Yeoman|LINK
Dear Deepak,
thank you very much!
I have realized the state persistance using the Session like this:
The situation is that I have different aspx pages using the same ReportList widget:
Page1.aspx:
Page2.aspx:
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
Contributor
2990 Points
1210 Posts
Re: Use a user control's public member variable to set a SQL parameter?
Nov 29, 2017 08:23 AM|Deepak Panchal|LINK
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
33 Points
425 Posts
Re: Use a user control's public member variable to set a SQL parameter?
Nov 29, 2017 02:40 PM|Yeoman|LINK
Thanks,
it works now!
Magnus