asp.net dynamic user control button click issue - asp.net

I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.

First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.

Related

asp.net - RadioButton "CheckedChanged" event does not work when it is unchecked

I have two RadioButtons on my Web Form and here is the code:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToLongTimeString());
}
When I check RadioButton2, time appears on the page but when I check RadioButton1(RadioButton2 is unchecked now) it does not update the shown time and removes it.
the event function must be called when it checks and unchecks.I don't understand what is wrong here and why the time disappears
(As it is obvious, I know about GroupName property and AutoPostBack. My problem is something else)
You can check property of each control and check AutoPostBack=True
and also if checked="true"
I tried it on different ways and opened my pages with different browsers and I figured out a few things. the most important of them which is related to my problem:
When a control Calls back, the page refreshes and that is why the time disappears. It sends the status of all controls using post method to itself and they would be the same way as they were. But the time is not written on a control and it would not be saved.
As I said if it was for example on a Label, the text would be kept:
Label1.Text = DateTime.Now.ToLongTimeString();
It answers the first question but I still think that the event function must be called when the RadioButton is being unchecked.

Asp.net go to previous page

I have 5 dropdownlist in asp.net.User selects 5 dropdownlist after that click to button.Button sends page to another page.If i go previous page dropdownlist selectedvalues and datas lose(it displays default values without selected values)
I tried below
Response.Redirect("PreviousPage.aspx");
datas losing is there any solution ?
Instead of using Response.Redirect(), you can change the PostBackUrl of the button to the target page, or use Server.Transfer(). Once there you should be able to access the properties you need from the Page.PreviousPage object.
Example using Server.Transfer:
Page1.aspx.cs:
protected void SubmitButton_Click(object sender, EventArgs e)
{
Server.Transfer("Page2.aspx");
}
Page2.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
var addressDropdownSelectedValue = ((Page1)Page).PreviousPage.AddressDropdown.SelectedItem.Value; //or SelectedValue
}
With the ScriptManager in the AJAX control toolkit (you get this in 4.0+, I think maybe even in 3.5), you can add history points. You add a history point and use that value to rebuild the page state.
http://msdn.microsoft.com/en-us/library/cc488548%28v=vs.140%29.aspx
You can also strongly type the previous page if you want to grab values from it. You do this with <%# PreviousPage %> directive. Then in code you could use Page.PreviousPage.FindControl

Events between an ASPX and ASCX

i'm a beginner in .NET, and search since yesterday morning to resolve my problem without finding the solution.
Here is my problem :
I create dynamically some User Controls by this way, because I need to give parameters :
List<ANNOUNCEMENT> listAnnouncement = getAnnoucements();
foreach(ANNOUNCEMENT ann in listAnnouncement)
{
if(ann.IS_CURRENT_ANNOUNCEMENT && currentAnnouncement == null)
{
currentAnnouncement = ann;
}
List<Object> listParams = new List<Object>();
listParams.Add(ann);
AnnouncementPresentation ap = (AnnouncementPresentation)(Controller.LoadControl(Page, "~/UserControls/AnnouncementPresentation.ascx", listParams.ToArray()));
/* important for the end of the method */
ap.modifyAnnouncementButtonClick += new EventHandler(modifyAnnouncementButtonClick);
pnl_announcements.Controls.Add(ap);
}
In this ASCX, I have a button, and when user will click on it, I want to call a method contained in my ASPX, so I do this in the ASCX :
public event EventHandler modifyAnnouncementButtonClick;
protected void btn_modify_announcement_Click(object sender, EventArgs e)
{
PageAdminAnnonces.currentAnnouncement = annonce;
modifyAnnouncementButtonClick(sender, e);
}
And this in the ASPX :
protected void modifyAnnouncementButtonClick(object sender, EventArgs e)
{
initListOfAnnouncement();
lbl_errors.Text = currentAnnouncement.TITLE;
}
I think everything works, but there is the problem : It works once, and at the end of the method, I delete my ASCX as you can see, and create new ASCX. But they don't have the methods, and when I click again, nothing works, so the ASPX is reloaded. After reloading, it works again.
Do i do something wrong?
According to the information in the comments, I suppose that your solution does not work because you are recreating the controls in the Click event handling method, which is very late in the page's lifecycle and should not be used for adding controls.
As mentioned in the comments, I suggest you to create the controls in Page_Init or Page_Load and not recreate them in the button's Click handling method. You should also assign a unique ID to each of them. Then, in the Click handler, you can use FindControl method to acces the created controls. Alternatively you can just save the references to the controls upon creation, so you can access them later easily.
Useful links:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
http://visualstudiomagazine.com/articles/2010/10/11/more-on-adding-controls-dynamically.aspx

User Control on a WebPart, PostBack / ViewState Problem

I'm using a Sharepoint WebPart to load a UserControl which has a button that does some processing on PostBack. I got a problem: when I click the button for the first time, the data loaded on ! IsPosback gets lost, but this does not occur when I click the button again. I think my problem is explained here: Sharepoint Lifecycle, but I haven't been able to find a workaround.
Any help would be really appreciated.
Additional Info:
I'm using EnsureChildControls on the WebPart's OnLoad event, and loading the UserControl on CreateChildControls.
I was able to fix this by programatically specifying an ID to the User Control.
E.g.:
protected void Page_Load(object sender, EventArgs e)
{
this.ID = "MyUserControlID";
}
More info here: http://bytes.com/topic/asp-net/answers/314816-dynamically-loaded-control-event-only-reached-2nd-postback
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (ViewState["MyStuff"] == null)
LoadMyStuffAndSaveToViewState();
else
DoSomethingWith(ViewState["MyStuff"]);
}

My code says a checkbox isn't checked when it is... ASP.NET

I have some checkboxes on a page. I get them using FindControl() in an UpdatePanel after pressing a button trigger, but the checked value is wrong. How can I get the correct checked value?
If you have any code that sets the values of the checkboxes on your page, make sure it isn't executing on postbacks, like this:
protected void Page_Load(object sender, EventArgs e) {
// Only set the checkboxes on GETs, not on POSTs
if (! this.IsPostBack) {
this.EmailMeUpdatesCheckbox.Value = false;
}
}
Actions triggered within UpdatePanels still go through the page lifecycle (which is why you have access to all your Page's state), so it may be clearing the user's selections before getting to the code in which you examine the checkbox values.

Resources