I have 2 Asp button in a page (buttonUsername and buttonReset). First one perform an AD search, and set a private variable, while second button perform an action, using variable previously set.
public partial class ResetPassword : System.Web.UI.Page {
private UserPrincipal tmpPwdUser;
protected void buttonUsername_Click(object sender, EventArgs e) {
this.tmpPwdUser = ...
}
protected void buttonReset_Click(object sender, EventArgs e) {
myObj.myFunction(this.tmpPwdUser); // --> this.tmpPwdUser is void
}
}
My problem: this.tmpPwdUser is correctly set in buttonUsername_Click function, but when buttonReset_Click event is triggered, variable this.tmpPwdUser is null. I guess that when event is triggered, page is reloaded, so each local variable is reset. Is there a way to preserve state when button si clicked?
As you suspect, a new instance of the form is created for the new request. You have to preserve the value of the variable between those requests. One way to do this is to store the value in ViewState.
You can create a helper property that stores and retrieves the value from ViewState:
private UserPrincipal TmpPwdUser
{
get
{
return ViewState["UniqueViewStateKey"] As UserPrincipal;
}
set
{
ViewState["UniqueViewStateKey"] = value;
}
}
try using the OnEvent property instead of the onclick
Poperty on button ( OnCommand="Submit_Command")
in code behind
Submit_Command(object sender, CommandEventArgs e)
{
}
Related
I'd like to bind a List<StockInList> to a GridView, show them to user, and get them back(perhaps user will edit them) to do something else. However, when I retrieved the bound item stockInLists, it's null. Reason I guess is that ASP create a new Code-Behind class Add_Inventories to handle request, so I lost the access to my bound item stockInLists.
Am I doing something wrong? What should I do to get them back correctly?
public partial class Add_Inventories : System.Web.UI.Page
{
private ShoppingDbContext shoppingDbContext = new ShoppingDbContext();
private List<StockInList> stockInLists;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var stockInListsFromSession =(List<StockInList>)Session["stock_in_list"];
if (stockInListsFromSession != null)
{
//save the stockInLists in private filed, so that I can get them back, however it's null when method `Add()` invoke.
stockInLists=new List<StockInList>(stockInListsFromSession);
GridView1.DataSource = stockInLists;
GridView1.DataBind();
}
else
{
//...
}
}
}
protected async void Add(object sender, EventArgs e)
{
foreach (var stockInList in stockInLists)
{
shoppingDbContext.StockInLists.Add(stockInList);
}
await shoppingDbContext.SaveChangesAsync();
Response.Redirect(Request.Url.AbsoluteUri);
}
}
The data in variables are lost when page is refreshed(Scope), to prevent it we should use Sessions, viewstate etc or make that variable as static(Not recommended).
In your case value stored in stockInLists will be lost if the page is refreshed,
store its data in viewstate ViewState["stockInLists"] = stockInLists;
store its data in Session Session("stockInLists") = stockInLists; (Recommended)
make it a static variable private List<StockInList> stockInLists; (Not Recommended)
I hope you understand it :)
I am trying to pass a simple string between pages but I don't know how to.
I created a test app where I click a button, the the value of "a" will pass on the next page (Page1.xaml.cs)
private void button1_Click(object sender, RoutedEventArgs e)
{
string a = "hello";
Page2 p2 = new Page2();
NavigationService.Navigate(p2, a);
}
Now, I want to extract the data from Page1 (Page2.xaml.cs)
private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
string str = (string)e.ExtraData;
}
And then subscribing in the constructor (Page2.xaml.cs)
public Page2()
{
InitializeComponent();
NavigationService.LoadCompleted += NavigationService_LoadCompleted;
}
However when I ran the program I get an error. Can someone point out what am I missing?
Without a good Minimal, Complete, and Verifiable code example, it's impossible to know for sure everything that would be needed to address your question. However, if all you're asking is how to allow data to pass from one page to the next when navigating, it seems to me that the NavigationService.Navigate(object, object) overload would be useful for you.
The second parameter is the data you want to pass. The target page can handle the NavigationService.LoadCompleted event (or any other appropriate one you prefer), where the object value that was passed to the Navigate() method can be retrieved via the NavigationEventArgs.ExtraData property.
For example, in your first page:
private void button_Click(object sender, RoutedEventArgs e)
{
Page2 p2 = new Page2();
NavigationService.Navigate(p2, v.str);
}
then in your second page:
private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
string str = (string)e.ExtraData;
// do whatever with str, like assign to a view model field, etc.
}
Of course, you'll subscribe the event handler, e.g. in your page's constructor or in XAML. For example:
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
NavigationService.LoadCompleted += NavigationService_LoadCompleted;
}
private void button_Click(object sender, RoutedEventArgs e)
{
NavigationService.GoBack();
}
private void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
string str = (string)e.ExtraData;
// do whatever with str, like assign to a view model field, etc.
}
}
Instead of setting the textbox DataContext, set the entire page’s datacontext to some class ie the ViewModel for page1. Implement the interface INotifyPropertyChanged and ensure that in the set of the string property, the NotifyPropertyChanged(“ElementName”) is raised.
Now create a new view with a corresponding view model like this which should also implement the interface INotifyPropertyChanged. Create a textbox and bind it to a string property like in first page. Ensure TwoWay binding for both properties to ensure both target and source is updated while data is changed.
Create the instance of both viewModels in MainWindow. When user navigates to 2nd user control, set
Page2ViewModel.TextBoxString = Page1ViewModel.TextBoxString;
Like this do vice versa while navigating from Page2 to Page1.
Page1ViewModel.TextBoxString = Page2ViewModel.TextBoxString;
This way, both the textboxes will be updated during navigation.
This is just an overall idea. You need to learn more about MVVM and WPF from some tutorials. Search in google.
Here is my example, maybe it helps someone
in Page1.cs
examsStr = result.Content.ReadAsStringAsync().Result;
List<Exam> exams = JsonConvert.DeserializeObject<List<Exam>>(examsStr);
for (int i = 0; i < exams.Count; i++)
{
btnExam.Click += BtnExam_OptionsShow;
btnExam.DataContext = exams[i];
}
private void BtnExam_OptionsShow(object sender, EventArgs e)
{
ExamRead examPage = new ExamRead();
examPage.DataContext = ((sender as Button).DataContext as Exam);
this.NavigationService.Navigate(examPage);
}
in Page2.cs
this.Loaded += ExamRead_Loaded;
private void ExamRead_Loaded(object sender, RoutedEventArgs e)
{
examObj = (exam.DataContext as Exam);
(exam.Children[2] as Label).Content = examObj.ExamName + " - Exam Options";
}
Page2 has attribute Name="exam" and examObj is global variable in that Page2 class
i have a textbox on .aspx page..On this page there is a user control .Inside this user contrl there is a button .I want o get the value of text box on button click which is not inside the user control .How can i do this
Please Help me .
write this line in you button click event of user control
protected void Button_Click(sender obj,EventArgs arg)
{
TextBox txtbox= (((MyPage)parent).FindControl("TextBoxid") as TextBox);
if(txtbox!=null)
(((MyPage)this.Page).FindControl("TextBoxid") as TextBox).Text;
//or
//(((MyPage)this.Parent).FindControl("TextBoxid") as TextBox).Text;
}
or
alternative is create the property in your page and access it in your user control
public string txtValue
{
get
{
return TextboxID.Text;
}
}
in button click event of user control
protected void Button_Click(sender obj,EventArgs arg)
{
string txtvalue = ((Mypage)this.Page).txtValue;
//or
//((MyPage)this.Parent).txtValue;
}
protected void MyButton_Click(object sender, EventArgs e)
{
string TextBoxValue;
TextBoxValue = MyTextBox.Text;
}
Is it what you want ?
Try use the following method,
((TextBox)USerControl.Parent.FindControl("txtbox")).Text
((TextBox)USerControl.Page.FindControl("txtbox")).Text
or
((YourPageType)USerControl.Page).TextBox.Text
With de-coupling in mind, I would recommend that if your user control needs to access information outside of it, then that information should passed in, not vice versa. The control shouldn't be responsible for where the information comes from, it just knows there is information. With this in mind, I would recommend bubbling the event to get the required information.
Event Bubbling
This will involve creating a new delegate, and then triggering it once the Button has been clicked, thus bubbling the event and allowing us to return the desired value, which in this case is the textbox value.
Step 1: Declare the delegate
// declare a delegate
public delegate string MyEventHandler(object sender, EventArgs e);
Step 2: Update the user control
// update the user control
public class MyUserControl : UserControl
{
// add the delegate property to your user control
public event MyEventHandler OnSomeButtonPressed;
// trigger the event when the button is pressed
protected void MyButton_Click(object sender, EventArgs e)
{
string someString = string.Empty;
if (this.OnSomeButtonPressed != null)
{
someString = this.OnSomeButtonPressed(this, e);
}
// do something with the string
}
}
Step 3: Update the page
// be sure to register the event in the page!
public class MyPage : Page
{
protected override void OnLoad(object sender, EventArgs e)
{
base.OnLoad(sender, e);
myUserControl.OnSomeButtonPressed += this.HandleUserControl_ButtonClick;
}
public string HandleUserControl_ButtonClick(object sender, EventArgs e)
{
return this.SomeTextBox.Text;
}
}
I have a custom class creating a dropdownlist control as below:
public class IHGridView : System.Web.UI.WebControls.WebControl
{
private string _dataSource = "not set yet";
public string DataSource
{
get { return _dataSource; }
set { _dataSource = value; }
}
}
EDIT:
protected override void OnInit(EventArgs e)
{
// VIewState is alive. When I select an option and submit, after postback it's selected value is the one I selected.
this.Controls.Add(_dropDownList);
}
or
protected override void CreateChildControls()
{
// VIewState is dead. When I select an option and submit, after postback it's selected value is the default one.
this.Controls.Add(_dropDownList);
}
So, now I come up with the result that I have to add control in "OnInit" void.
But, this "OnInit" is the first void that this class writes.
If I want to use a property like "DataSource" before, "OnInit" void...
How would I do that?
EDIT:
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
}
DataSource is set when the button in aspx page is fired.
why would you want to add _dropDownList twice? once in the OnInit should be enough, and OnInit is where it should be added if you want it in the control collection - thus having viewstate persisted and restored.
to access and bind _dropDownList for example, override the DataBind method - at which point all the properties will be available to you.
protected override void DataBind(){
base.DataBind();
_dropDownList.DataSource = this.DataSource;
_dropDownList.DataBind();
}
this is pseudo code, and has not been tested or validated
EDIT:
Call overriden DataBind method
protected void Button1_Click(object sender, EventArgs e)
{
IHGridViewTest2.DataSource = "fired";
IHGridViewTest2.DataBind();
}
I am creating a dynamic button in a custom class, outside of the .aspx codebehind. The custom class creates a Table object and generates a column of Buttons within that table. Once generated, the Table is loaded into a placeholder control. Everything is functioning well except for this problem:
How do I programmatically assign a Button Object a 'Click' event within the custom class?
MyButton.Click += new EventHandler(MyButtonClick);
This results in: 'The name 'MyButtonClick' does not exist in the current context' error.
I know it doesn't exist in the current context, but once the aspx page is rendered, the codebehind will include a method to handle 'MyButtonClick'. I don't know how store a Click event method name into a Button object from a custom class and pass it off to the aspx codebehind to be rendered.
You have to define an event in your custom control. Fire that event on button click so that your .aspx can handle it.
EDIT: Same principles apply to a custom class.
Control Code-Behind:
public delegate void ButtonEventHandler();
public event ButtonEventHandler ButtonEvent;
protected void Button1_Click(object sender, EventArgs e)
{
ButtonEvent();
}
.ASPX Code Behind:
protected override void OnInit(System.EventArgs e)
{
control1.ButtonEvent+=
new Control1.ButtonEventHandler (whatever_ButtonEvent);
}
protected void whatever_ButtonEvent()
{
//do something
}
Let's take this concept and apply it to a user control that has a textbox and two buttons. The user control is placed within a Gridview. When my code runs the method in my event handler method is always null. I think has to do w/the fact the a button is is in a user control which is in the gridview.
Here is my user control code.
public partial class User_Controls_GridViewFilter : System.Web.UI.UserControl
{
public event EventHandler UserControlButtonClicked;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
renderPage();
}
}
private void OnUserControlButtonClick()
{
if (UserControlButtonClicked != null)
{
UserControlButtonClicked();
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
OnUserControlButtonClick();
}
protected void btnReset_Click(object sender, EventArgs e)
{
OnUserControlButtonClick();
}
}
I register the control on the aspx page.
((User_Controls_GridViewFilter)gvMapLayer.HeaderRow.FindControl("FilterBox1")).UserControlButtonClicked
+= new ButtonEventHandler(User_Controls_GridViewFilter_UserControlButtonClicked);