Viewstate and TabPanel - asp.net

I have been working on a asp.net project last ~6 months where that was one .aspx that was being loaded with different controls. This page and those controls had their own UpdatePanels, etc etc. In other words I had to deal with a bag of viewstate issues. It seems like whenever I think I get viewstate and its details completely I get something like what I am about to describe below. This might have to do with control state, which is from what I understand "necessary" viewstate you cannot turn off. Or this might have to do something with AJAX.
Anyway take a look at this example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" EnableViewState="false" Inherits="ControlDisabledViewStateTesting._Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (treeView.SelectedNode != null)
ContentPanel.Controls.Add(ContentPanel.TemplateControl.LoadControl("MyUserControl.ascx"));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<ajx:ToolkitScriptManager runat="server" />
<asp:TreeView ID="treeView" runat="server">
<Nodes>
<asp:TreeNode Text="First Node" Value="111"/>
<asp:TreeNode Text="Second Node" Value="222"/>
</Nodes>
</asp:TreeView>
<Asp:Panel ID="ContentPanel" runat="server" />
</div>
</form>
</body>
</html>
And here is the user control mark up:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="ControlDisabledViewStateTesting.MyUserControl" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajx" %>
<ajx:TabContainer ID="tabContainer" ActiveTabIndex="0" runat="server">
<ajx:TabPanel ID="tab" HeaderText="Tab1" runat="server"/>
<ajx:TabPanel ID="tab2" HeaderText="Tab2" runat="server" />
</ajx:TabContainer>
As you have noticed, Viewstate is turned off on the page level, so none of the controls should be using viewstate.
Click on "First Node".
User control gets loaded with two tabs.
Select the second tab.
Click on "Second Node"
User controls get loaded again, with the "SECOND TAB" selected already.
Is this an issue with viewstate/controlstate or does it have something to do with AJAX part of the TabPanel?
I really appreciate if someone can shed some lights as to what is happening here and how I can turn off this functionality.
Thanks,
Mike

The value for the active tab isn't being stored in view state. It appears to have to do with the ASP.NET AJAX framework and the value is being loaded from the post data.
To change the behavior you could derive from the TabContainer and override the LoadClientState method so that the ActiveTabIndex isn't changed.
You can view the source code for the TabContainer on codeplex:
http://ajaxcontroltoolkit.codeplex.com/SourceControl/changeset/view/1014bf767f65#Server%2fAjaxControlToolkit%2fTabs%2fTabContainer.cs

Related

Dynamically Loaded Existing Panel cause issue of PostBack

At first See the Code below.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="test.aspx.vb" Inherits="BLL.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlCustom" runat="server"></asp:Panel>
<asp:Panel ID="pnlDiv1" runat="server">
<asp:DropDownList ID="ddl_Status" runat="server"></asp:DropDownList>
</asp:Panel>
<asp:Panel ID="pnlDiv2" runat="server">
<asp:DropDownList ID="ddlImg1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlImg1_SelectedIndexChanged"></asp:DropDownList>
</asp:Panel>
<asp:Panel ID="pnlDiv3" runat="server">
<asp:DropDownList ID="ddlImg2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlImg2_SelectedIndexChanged"></asp:DropDownList>
</asp:Panel>
</div>
</form>
</body>
</html>
I want to hide and show the panel in different order as per the requirement. i don't want to change the content of the panel. pnlDiv1,pnlDiv2,pnlDiv3 will bind in pnlCustom.
Now, In Page_Load Event i am setting all panel style to "display:none" except "pnlCustom" and dynamically adding pnlDiv1/pnlDiv2/pnlDiv3 in "pnlCustom" and set its style to "display:inline".
In my application problem is If i change the value of "ddlImg1" the page gets postback and all the value reset. this is same for the "ddlImg2".
Note:Values are bind in dropdown in Page_Load if its not postback.. so, anyone can explain what's an issue?
This could be a possible issue with the ViewState.
The following link might be helpful to you:
ASP.NET DropDownList not retaining selected item on postback

Button in UpdatePanel not firing Click event: RequiredFieldValidator the cause

I'm working on a page that consists of Telerik RadToolTip, an UpdatePanel that contains a Button, and a few user controls. There is also a Master Page, which is where the ScriptManager is located.
Previously, I had a RadAjaxManager on the Master Page and RadAjaxProxyManager. I removed them and began to implement standard Update Panels because of complexities within the User Controls.
Now, however, I can't seem to get any of this page to behave as expected. When I place a Button in the UpdatePanel, I expect a Partial Postback and I expect the Button's OnClick event to be fired. However, when I run this code with a breakpoint in the btnPostBack_Click event code, the breakpoint never gets hit.
If I cut and paste the same controls with the same settings into a new project, the breakpoint gets hit. Is there something in the environment of the site or could Telerik be interfering with the Button's Click event? I've removed all RadAjaxManager/Proxies.
<%# Page Title="" Language="C#" MasterPageFile="~/AV.Master" AutoEventWireup="true"
CodeBehind="AssetView.aspx.cs" Inherits="AV_ASP_UI.AssetPages.AssetView" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%# Register TagPrefix="uc" TagName="AssetDiagram" Src="~/Controls/AssetDiagram.ascx" %>
<%# Register TagPrefix="uc" TagName="AssetInfo" Src="~/Controls/AssetInfo.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
function DoPartialPostBack(command, id) {
document.getElementById("<%= hfldId.ClientID %>").value = id;
document.getElementById("<%= btnPostBack.ClientID %>").click();
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:HiddenField ID="hfldId" runat="server" />
<telerik:RadToolTip ID="RadToolTip1" runat="server" Modal="true" ShowEvent="FromCode"
Position="Center" RelativeTo="BrowserWindow" HideEvent="FromCode" TargetControlID="hfldFireToolTip">
</telerik:RadToolTip>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Button ID="btnPostBack" runat="server" Text="Button" style="visibility: hidden" OnClick="btnPostBack_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Panel ID="pnlWorkArea" CssClass="pnlWorkArea" runat="server" Height="770px" onMouseDown="hideSVGTtip(event);">
<uc:AssetDiagram ID="ucAssetDiagram" runat="server" CanvasHeight="720" CanvasWidth="276" />
<uc:AssetInfo ID="ucAssetInfo" runat="server"/>
</asp:Panel>
</asp:Content>

ASP.net GridView doesn't show

I have GridView on my page:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged">
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
And i use button to set grid view datasource:
DataAdapter adapter=new DataAdapter(SqlCommand,SqlConn);
DataTable tbl=new Datatable();
adapter.Fill(tbl);
GridView1.DataSource=tbl;
From debug mode i can see that datatable is filled property and does contain data. But i can see nothing in the screen. What's the problem
P.S. Found the simillar question except that in that question no data source was set
You are missing to call databind method here.Use following code :
DataAdapter adapter=new DataAdapter(SqlCommand,SqlConn);
DataTable tbl=new Datatable();
adapter.Fill(tbl);
GridView1.DataSource=tbl;
GridView1.DataBind();
Let me know it is working for you or not ?

Access to MVC Models from standard ASCX UserControl

I've got a simple ASCX user control (non-MVC). The user control has a property on it (let's call it Title or something).
I've also got an MVC Partial View which contains the user control.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<SomeModel>" %>
<%# Register TagPrefix="tag" TagName="Control" Src="~/path/to/ascx/control.ascx" %>
... snip ...
<tag:Control ID="myControl" runat="server" />
What I'd like to be able to do is something like this:
<tag:Control ID="myControl" runat="server" Title="<%= Model.Title %>" />
... so that I can access some property of the model inside my Control.
Unfortunately this method doesn't work, as I get a message saying "This is not scriptlet. Will be output as plain text".
Is what I'm trying to do even possible? If so, does anyone have any ideas how I can try and do it?
If it's relevant, this is a .Net 4 project, with MVC 2 components.
Thanks!
Neil
Is what I'm trying to do even possible?
Yes but it would be extremely ugly and it involves using a code behind (in an ASP.NET MVC view!!!):
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="AppName.Views.Home.Index"
CodeBehind="Index.aspx.cs"
%>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Txt" runat="server" />
</asp:Content>
and in the corresponding code behind Index.aspx.cs (beurk..., I will vomit):
public partial class Index : System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>
{
protected void Page_Load(object sender, EventArgs e)
{
Txt.Text = Model.Title;
}
}
which could be improved like this to avoid the code behind nonsense:
<%# Page
Language="C#"
MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>"
%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Txt.Text = Model.Title;
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Label ID="Txt" runat="server" />
</asp:Content>
I wouldn't recommend you doing something like this if you intend to migrate to ASP.NET MVC 3 and the Razor view engine one day.
I found a solution that's a little cleaner. Still not to my liking, but good enough to wash my hands of this annoying issue.
<% ViewBag.Item = item; %>
<gc:Ribbon runat="server">
<Content>
<strong><%= ViewBag.Item.DataItem.Name %></strong>
</Content>
</gc:Ribbon>
Instead of passing an object into the user control to use within, assign it to the ViewBag (for each iteration if you're looping), and use it globally. If memory serves me right, I believe MVC 2 uses ViewData["loosely typed name"] instead of the dynamic ViewBag.

Asp.net web forms control in asp.net mvc

I'm trying to inject an asp.net classic web control to my asp.net mvc application. This control doesn't use view state or post back so it works fine.
But I can't plug a real-time provided value in its attribute. This code
<my:Control runat="server" Tag="<%: Model.ID %>" />
ends up with the tag value just set explicitly to "<%: Model.ID %>".
Is there a way to make it work?
I believe the syntax is as follows:
<my:Control runat="server" Tag="<%# Model.ID %>" />
The other gotcha is you must call .DataBind() on the Control at some point after the Control has been initialized. This probably means taping into the Page_Load or OnInit events of the Page.
<%# Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script runat="server">
protected override void OnInit(EventArgs e)
{
this.Label1.DataBind();
base.OnInit(e);
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%= Html.Encode(ViewData["Message"]) %></h2>
<asp:Label ID="Label1" runat="server" Text="<%# DateTime.Now %>" />
</asp:Content>
Or, if you have access to the source, you could add a call to .DataBind() somewhere before the final Render. You would have to experiment with that.
Hope this helps.
Well, seems like the only way to do this is injecting code the control renders to the page directly. In my case the control renders a silverlight object with some javascript so I put it at the page as is.

Resources