I'm doing an asp.net application with one page. In this page, I have one usercontrol defined. This usercontrol has a menu (three buttons) and 3 usercontrols defined too. Depending on the clicked button one of the three usercontrols turn to visible true or false.
In these three usercontrols I have a button and a message, and I want to show the message "It's NOT postback" when the button of the menu is clicked, and when the button of the usercontrol is clicked the message will be "YES, it's postback!!!".
The question is that using property "IsPostBack" of the usercontrol or the page the message will never be "It's NOT postback" because of the clicked button of the menu to show the nested usercontrol.
This is the structure of the page:
page
parent usercontrol
menu
nested usercontrol 1
message
button
nested usercontrol 2
nested usercontrol 3
I know it can be done using ViewState but, there is a way to simulate IsPostBack property or know when is the true usercontrol postback?
Thanks.
I have the solution for this problem. When an element fires an event is sent to the server in the Request.Form collection, so I created a property in nested usercontrols that checks if there is a child control (defined in the usercontrol itself) in Request.Form collection:
public bool IsUserControlPostBack
{
get
{
foreach (Control c in Controls)
foreach(string key in Page.Request.Form.AllKeys)
if( c.ClientID == key.Replace('$','_'))
return true;
return false;
}
}
This code can be a property of the usercontrol or, if it's called a lot of times, a variable that it's set on the OnInit event of the usercontrol.
Related
I have a radiobuttonlist that lives on a user control. This user control lives in a repeater on a parent user control, and that user control lives on a page with a submit button.
So something like this:
<page>
<UserControl1>
<Repeater>
<UserControl2>
<radiobuttonlist>
</UserControl2>
</Repeater>
</UserControl1>
<Submit button />
</page>
The radiobuttonlist is dynamically populated in the code-behind of UserControl2. The problem is that when I submit the form, I need to access the SelectedValue of the radiobuttonlist, and that value is always empty. Even if I first fire the methods that populate the radiobuttonlist, the selectedvalue of the RBL is empty. I have a SelectedIndexChanged event handler on the RBL, but it never fires.
What do I need to do to be able to get the SelectedValue of the radiobuttonlist when I cause the parent page to postback?
I got it working. I guess it was an order of operations issue. The fix was to dynamically declare the event handler of the radiobuttonlist in the OnInit() of UserControl2.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
rblOptions.SelectedIndexChanged += new EventHandler(ctrlOptions_SelectedIndexChanged);
}
Once I did that, the event started firing, even though I was re-instantiating the UserControl on postback. Since the event was firing, I was able to obtain the Selected Index without needing to keep it in ViewState.
When you post your datas, you re-bind your datas so you erase your selected event or values.
Try with this code in your Page_Load (Of your User Control)
If(! IsPostBack)
{
//You build RadioButtonList
}
And persist your datas with ViewState, EnableViewState="true"
I have a dynamically created bill of material with each line item being a dynamic user_control. the user_control has a textbox for quantity entry. When I click the submit button i'd like to get all the quantities from each textbox but the controls have all disappeard on the page load and the page shows zero controls.
I know you can turn on autopostback for the textbox then catch each individual text_changed_event but that doesn't seem efficient. I'd like to just loop through all of them when user clicks submit button, then take them back to the same bill of material page.
First of all the reason why controls disappear on postback is that they were added to your page dynamically and when postback occurred the information about the dynamic controls were lost and page had no information about these dynamic controls.
Now about getting the values from controls inside dynamic user controls, you have use the FindControl method or have to iterate through the Controls collection of user control to get the reference to TextBoxes.
An idea about how to do this:
//1. Using ID of user control
//inside button_click method
protected void btnSubmit_Click(...)
{
TextBox txt1 = idOfUserControl.FindControl(textBoxId);
}
//2. Using type of user control
//inside button_click method
protected void btnSubmit_Click(...)
{
foreach(Control c in Page.Controls)
if(c is YourUserControlClass)
{
YourUserControlClass control = (YourUserControlClass)c;
TextBox txt1 = c.FindControl(textBoxId);
}
}
So I have a ListView (assignmentsListView) in an UpdatePanel, being filtered by a DropDownList in the same UpdatePanel. The DropDownList has a list of persons in it and uses autopostback, and the ListView shows the tasks those persons are assigned to.
I am trying to use code similar to this:
protected void assignmentsListView_DataBound(object sender, EventArgs e)
{
string resFirstName = Utilities.getResourceFirstName(resDdl.SelectedValue);
if (assignmentsListView.Items.Count <= 0)
{
//Show error message
}
else
{
//Try to find the ImageButton in the ListView's header template.
ImageButton exportButton = (ImageButton)assignmentsListView.FindControl("ImageButton3");
//Register this button as a PostBack event in the Master Page
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
ScriptManager.RegisterPostBackControl(exportButton);
}
}
When I first load the page, the DropDownList shows the first person in the list, and the ListView correctly shows that persons tasks.
If I then select a person who I know has zero tasks, I get an error at the RegisterPostBackControl() method, saying the passed-in control cannot be null.
When debugging, at the RegisterPostBackControl method, it shows that the ListView Items collection has >0 elements in it (the number of elements matches the person selected before the current person).
What's going on? Any suggestions?
In Asp.Net Web Forms applications, the order of events are not always what you'd want. For your case, the new person selection is probably applied after this method is executed. The best thing you could do is force databindings in an earlier event (like Page_Init)
I have created a usercontrol to capture education details, it contains 5 textboxes and an functionto insert that values into my db. I have added the usercontrol 5 times to a page. I have a button on my aspx page which I want to be able to click and call the function to insert the values.
Can anyone suggest how I can do this?
The function which inserts values into your db should not be in your usercontrols but in your page. Does your usercontrol has a save button itself or only the page? On first variant you should raise an event and the page could catch it to save the values. On second variant you should iterate through your usercontrols(added dynamically or fixed?). You should add public properties which return the textbox's values to the page, then you can save these values into the db.
Put a public method on your user control.
// Inside MyUserControl
public void SaveIt() {
// logic to perist the values to db.
}
From your ASPX page (or code behind) when the button is clicked, loop over your user control instances and call the method:
// On Click...
MyUserControl[] arrControls = new MyUserControl[] {myUC1, myUC2, myUC3, myUC4, myUC5};
foreach (MyUserControl c in arrControls)
c.SaveIt();
How to dynamically Change DefaultButton Property of the Form from UserControl present in a page.Page is loaded into a master page contains Update Panel.
When Try to change the Default button in onload event of the UserControl, It is not changing.
if(lastpage)
{
this.Page.Form.DefaultButton = btnSave.UniqueID;
}
else
{
this.Page.Form.DefaultButton = btnNext.UniqueID;
}
Quote from msdn:
The following scenarios for setting
the default postback button on a page
are not supported during asynchronous
postbacks:
* Changing the DefaultButton programmatically during an asynchronous post back.
http://msdn.microsoft.com/en-us/library/bb386454.aspx
Could you not postback the state of the form and then work out which action to take rather than passing this information based on which button was clicked?