Hide/Show controls with AJAX - asp.net

I am using an ASP.NET AJAX-Enabled Web application (ASP.NET 2.0 and AJAX Toolkit 1.0)
that contains one button and 2 UpdatePanels (UpdatePanel_1 and UpdatePanel_2)
The button is registered with RegisterAsyncPostBackControl in the ScriptManager object
UpdatePanel_1 is in "Conditional" update mode and contains a TextBox.
UpdatePanel_2 is in "Always" update mode and contains another TextBox
When the button is pressed its handler calls UpdatePanel_1.Update() that updates the value of the TextBox based on a randomly selected value in a list;
Also the UpdatePanel_2's TextBox is being updated automatically , also without page refresh
Based on the value of a boolean ViewState variable I would also like to hide/show the UpdatePanels alternatively but
I get the error :
"Sys.InvalidOperationException: COuld not find UpdatePanel with ID 'UpdatePanel_2' (or UpdatePanel_1).
If it is being updated dynamically then it must be inside another UpdatePanel"
How can it be done without adding extra wrapping UpdatePanels?
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager1.RegisterAsyncPostBackControl(Button1);
if (!IsPostBack)
{
Visibility = true;
}
UpdatePanel_1.Visible = !Visibility;
UpdatePanel_2.Visible = Visibility;
Visibility = !Visibility;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Panel1.Visible)
UpdatePanel_1.Update();
}
protected bool Visibility
{
get
{
return (bool)(ViewState["Visibility"] ?? true);
}
set
{
ViewState["Visibility"] = value;
}
}

The problem is that invisible controls aren't rendered to the client. So then trying to make them visible isn't going to work because as far as the client is concerned, they don't exist.
Try using style="display:none", or use different CSS classes and styles for visible and invisible panels, rather than setting visible=false;

You can invisible, or visible controls is child of updatepanel, not invisible, visible updatepanel, I try use updatemode = conditional but error, and then I visible controls add to updatepanel. Hopy help you
Thanks everybody post

Related

asp.net dynamic user control button click issue

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.

Propagation of events to User Controls in ASP.NET

I have a user control (Navigation) nested within another user control (Header) that is dynamically loaded from a Control class (Standard).
The user controls, Navigation and Header have AutoEventWireup = false.
The control class Standard calls loads the Header user control from a configuration item.
private void layoutAndRender(HtmlTextWriter output, string UserControlKey, NameValueCollection UserControlsConfiguration)
{
if(UserControlsConfiguration[UserControlKey] != null && UserControlsConfiguration[UserControlKey].ToString() != "")
{
string suc = System.Web.HttpContext.Current.Request.ApplicationPath + UserControlsConfiguration[UserControlKey].ToString();
UserControl ucToRender = (UserControl)this.Page.LoadControl(suc);
ucToRender.RenderControl(output);
}
}
My problem is that I want to initialize an object in the Navigation user control that can accept Page.Request and Page.Response, but events don't seem to be firing in the Navigation code behind.
The code I'm using to initialize my object is:
this.browser = new Browser(this.Request, this.Response);
I tried doing this during the Navigation constructor but this.Request and this.Response are not set at that time.
I tried using the statement in a void Page_Load(object sender, System.EventArgs e) method, but this doesn't seem to be firing, even if I have this.Load += new System.EventHandler(this.Page_Load); in the Navigation constructor.
I've also tried similar statements for Page_Init and Page_PreRender, but none of these seem to be firing.
Is it that a control loaded with LoadControl does not fire Load or Init events, if loaded the way I have loaded them, and the same goes for any user controls that it may include?
If AutoEventWireup is set to false for the controls that you want to load, then you should override the OnInit method to wire up the Load event handler for the controls. The Request and Response properties should be available from within Page_Load.
For example:
public class Header : Control
{
private void Page_Load(object sender, EventArgs e)
{
}
override protected void OnInit(EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
See MSDN for more info on AutoEventWireup:
http://support.microsoft.com/kb/324151
Unfortunately, this is legacy code, and not always done the right way.
In particularly, because the user control is loaded and rendered manually, it probably skips most of the event model that I wanted to take advantage of. Ideally, it should have done an AddControl() to the page, rather than rendering the control to a HtmlTextWriter.
My work around is to override the RenderControl method, and initialize the browser property before passing RenderControl up the chain.

How to implement paging in gridview in a modular popup?

I am using a modular popup to show gridview in a popup. Now I want to implement paging of this gridview. When I click on the page number the popup disappears. How do I avoid it? I want it to disappear only when clicking close button.
I assume that you're using the ModalPopupExtender from the AjaxControlToolkit.
If you're posting back you always need to call ModalPopupExtender1.Show() in codebehind when you want it to stay visible.
You could simplify it by using Page_PreRender if you have many events and you don't want to remember this always:
protected void Page_PreRender(object sender, System.EventArgs e)
{
// change "this" to somewhat appropriate for example your GridView
// "this" makes sense for example in a UserControl
if (this.Visible) {
this.ModalPopupExtender1.Show();
}
}

ASP .Net Gridview rowupdated - can it happen before the page load?

Here's the scenario:
-Gridview control
-Calendar control
I only want the calendar to show if a specific item is chosen in the drop down list which is in a gridview. When the grid view row is updated I want to change whether or not the calendar is visible. The calendar's visibility only shows correctly on the next post back.
Page_Load is called before events which are called before Render. There is no reason why you couldn't, in your event, check the value of the dropdownlist and set the Calendar control visible property, this would then knock into Render.
Try adding a check of IsPostBack before setting the loading your GridView. That will prevent you from overwriting it's values.
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
/*Populate your GridView*/
}
}
protected void GridView_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
/*show your calendar here if you need to*/
if(whatever) calendar.Visible = true;
}
This should work, if it doesn't then I'd recommend putting breakpoints in your Page_Load and RowUpdated methods and stepping through it, preferrably with a Watch on the gridview's datasource (it'll go red if it's changed) and a watch on calendar.Visible, to help you see if something has changed.
For the record, control events like OnRowUpdated will never fire before Page_Load unless explicitly called for some reason. Chances are you're just doing something where it's not updating the content of the GridView before it gets to the RowUpdated method, or it's overwriting the data in the GridView due to a lack of !IsPostBack check.

Firing Server Side Event from jQuery - __EVENTARGUMENT Does not Exist

I am trying to fire a form submit via jQuery and have a particular server side event fire.
Client Side Code:
$("input[name=__EVENTARGUMENT]").val("SomeArg");
form = $("body form");
form.submit();
Server Side Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Request["__EVENTARGUMENT"] == "SomeArg")
{
//do some stuff
}
}
}
The problem is, the "__EVENTARGUMENT" hidden input does not exist. I discovered that if I add an ASP.net server control dropdownlist with autopostback = true, the EVENTARGUMENT hidden input is created. Is there a cleaner way to have ASP.net create these hidden inputs, and allow me to get their values server side without adding controls I do not actually need on the page?
Add asp.net HiddenField control to the page
But you will have a problem which is to get this control id, which as.net generates automatically, you can solve this using the ClientID property for the hidden field like this article mentions

Resources