Viewstate lost on postback for .net Sitecore page - asp.net

I'm getting some strange behaviour with viewstate being lost on postback for a .net application using Sitecore. I'm assuming it might be some config variable somewhere but I'm new to Sitecore and don't really know where to start looking.
UPDATE: Sitecore has now gotten back to us with an answer. We had recently added the dtSearch module, and AutomaticDataBind was set to true in the dtSearch.config which overrides the setting in the web config. We've now removed it and it works fine again.
I've made a mini test if that might help. It's two usercontrols on one page, both with a repeater. When updating the viewstate gets lost so even if I'm binding the updated repeater again the data for the other one will be lost.
Usercontrol 1:
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Repeater1_ItemBind">
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="Literal1"></asp:Literal>
</li>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> myTestList1 = new List<string>();
myTestList1.Add("a");
myTestList1.Add("b");
Repeater1.DataSource = myTestList1;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemBind(object sender, RepeaterItemEventArgs e)
{
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
Literal1.Text = (string)e.Item.DataItem;
}
Usercontrol 2:
<asp:Repeater runat="server" ID="Repeater2" OnItemDataBound="Repeater2_ItemBind" OnItemCommand="Repeater2_Command">
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="Literal2"></asp:Literal>
<asp:LinkButton ID="Update" CommandName="Update" runat="server">
update
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
private string test = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
test = "a";
Repeater2.DataSource = test;
Repeater2.DataBind();
}
}
protected void Repeater2_ItemBind(object sender, RepeaterItemEventArgs e)
{
char c = (char)e.Item.DataItem;
Literal Literal2 = (Literal)e.Item.FindControl("Literal2");
Literal2.Text = c.ToString();
}
protected void Repeater2_Command(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Update")
{
test = "b";
Repeater2.DataSource = test;
Repeater2.DataBind();
}
}
Does anyone have any ideas what might be going on? Let me know if I need to provide any more information. The most annoying thing is that it was working last week but I have no idea what has changed!
Thanks,
Annelie

Do you have System.Web.UI.WebControls.Repeater in the "typesThatShouldNotBeExpanded" section of your web.config?
I've found that there are definitely some things that don't work with the regular PostBack model in Sitecore... but this Repeater should be OK.
One trouble area is having FieldRenderers inside Repeaters. They don't seem to restore the Item property correctly on Postback.

As far as I can see, this thread describes exactly the same problem, but with DataGrid. See if setting AutomaticDataBind to 'false' in web.config helps.

Related

ASP.NET passing variables from web form to code behind

i'm developing a web application for my company, but i'm coming up against a very simple problem that i don't now how to resolve. I search a lot on internet but i can't find anything.
I need to call a code behind method from an asp.net controls passing variables.
This is the method in the code behind (file.aspx.cs):
protected void SayHello(object sender, EventArgs e, String RandomName)
{
Response.Write(PrintedString);
}
And this is a asp.net control that call the method through OnLoad event :
<asp:Label ID="Label1" runat="server" Text="Hello" OnLoad="Visibility('ciao mamma')"></asp:Label>
What's wrong with this simple thing? Where i'm wrong?
Please answer to this simple question, it's driving me crazy...Thanks.
You can pass an argument to the event handler by the following way, but please note it will work for only asp button.
<asp:Button ID="btn" runat="server" Text="Click" OnCommand="btn_Command" CommandArgument="YourArgumentValue"/>
and then in the code behind file you do like this :
protected void btn_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
It will work for only asp button because they have a onCommand attribute with them.
whenever you will click on the button this code behind file code gets executed.
Hope this helps.
You can set the value to label on Page_Load event
You can use below code.
protected void Page_Load(object sender, EventArgs e)
{
string UserName="test";
Label1.Text="Hello "+ UserName;
}
Is this what you need?
Markup:
<asp:Label ID="lbl" runat="server"><%=SayHello("foo") %></asp:Label>
Code behind:
protected string SayHello(string name)
{
return "Hello " + name;
}

Why is my repeater control empty on postback?

I think this is a "doh" moment caused by me not having dome WebForms dev for a few years..
I have a repeater which which contains a bunch of checkboxes:
<asp:Repeater EnableViewState="true" ID="IDTypesRepeater" runat="server" OnItemDataBound="IdTypesRepeaterItemDataBound">
<HeaderTemplate/>
<ItemTemplate>
<asp:CheckBox EnableViewState="true" ID="chkIdType" Text="<%# ((KeyValuePair<string,int>)Container.DataItem).Key %>" runat="server" />
<asp:HiddenField ID="idType" Value="<%# ((KeyValuePair<string,int>)Container.DataItem).Value %>" runat="server"/>
<br />
</ItemTemplate>
</asp:Repeater>
I need to get the checkboxes that are selected in the code behind:
foreach (RepeaterItem repeaterItem in IDTypesRepeater.Items)
{
if ( ((CheckBox)repeaterItem.FindControl("chkIdType")).Checked )
{
// Do something
}
}
But on postback, this code isn't working! I know about always databinding a repeater, so I've done this:
protected void Page_Load(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
So this repopulates the repeater, but the Update code never finds any checked checkboxes.. Any ideas?
Bind in the Page_Init event
protected void Page_Init(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
Be sure to use the !Page.IsPostBack method in your pageload.
Otherwise, the Repeater will keep getting reset, and all your checkboxes
will be in there default value (unchecked)
This should fix it. You are binding the control on postback hence losing the values. You can bind it after handling any event to show the updated record.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
}

Template variable in ASP.NET

I have to add a Page's variable into a ItemTemplate but dont know how.
For example:
<rad:RadMenuItem ID="f" runat="server" Text="Products">
<ItemTemplate>
<div class="pitem"><%= MyText %></div>
</ItemTemplate>
</rad:RadMenuItem>
The MyText variable does exist in the context and has value but the text does not show up
Another question:
How can I add MyText to the ASP.NET page like the following?
<asp:Button Text="<%=MyText%>" .../>
I dont want to edit the code like btn.Text=MyText, just want to do that on the .aspx file as required.
Answers to second question. (Btw, you should only ask one question at a time here on Stack Overflow.)
You could use
<asp:Button Text="<%# MyText %>" />
if you call DataBind() in you code behind.
public void Page_Init(object sender, EventArgs e)
{
DataBind();
}
If the databind is expensive, I believe you could use this code, so that you only call it once, and then save the values in the ViewState.
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBind();
}
}

Not getting user input from TextBox

I'm starting to loose my nerves on some completely banal thing: I don't get user input from a TextBox :S
I do something like this (code behind aspx):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
}
this._presenter.OnViewLoaded();
txtBox1.Text = "blah";
}
protected void Button1_Click(object sender, EventArgs e)
{
//Do sth with txtBox1.Text but when I read it, it is still the same as when a loaded the page at Page_Load, So if I entered "blahblah" in the txtBox1 via browser the text I get when I debug or run is still "blah"
}
And the aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="InsertStudent.aspx.cs" Inherits="IzPT.Vb.Views.InsertStudent"
Title="VnosProfesorja" MasterPageFile="~/Shared/DefaultMaster.master" %>
<asp:Content ID="content" ContentPlaceHolderID="DefaultContent" Runat="Server">
<h1>Student</h1>
<p>
<table style="width:100%;">
<tr>
<td style="width: 139px">
Name</td>
<td>
<asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
</p>
</asp:Content>
I also tried to do this with DetailsView and bind it to a list but when I read the values in the edit mode i had the same problem.
Any ideas?
You're setting the textbox Text property to "blah" on every Page_Load. Since ViewState has already been loaded at this point, you're overwriting whatever value the user entered.
If you only want to set the Text value one time, then make sure that you put it inside the if (!IsPostBack) check.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
txtBox1.Text = "blah";
}
this._presenter.OnViewLoaded();
}
Your problem is that you are changing Value in Page_Load!
Page_Load is run before Button1_Click.
Move code from Page_Load to this
protected override void OnLoadComplete(EventArgs e)
{
txtBox1.Text = "blah";
}
Or protect your code... like this
if (!this.IsPostBack)
{
txtBox1.Text = "blah";
}
Page_Load is being called during the post back which is resetting the value in your text box. Change to
if (!this.IsPostBack)
{
txtBox1.Text = "blah";
this._presenter.OnViewInitialized();
}
Personally i would have a property in the view to set the textbox value from the presenter. In OnViewInitialized() or OnViewLoaded().

Need help asp.net other textbox

In my project i have kept source info in dropdownlist. Like source of info: in dropdownlist three items website, Newspaper and others. If user select Others item, then only other text box should be visible otherwise should be invisible. For that i have set in page load event
lblother.visible=false;
txtother.visible=false;
And in Btnsubmit event i have written the condition like.
if(dropdownlistinfo.selectedindex==2)
{
lblother.visible=true;
txtother.visible=true;
}
But in my case i m not getting my desire output. Its always invisible when i am selecting Others item from drowdownlist also. Pls somebody help me where is my mistake?
Thanks,
Sumit
I think the problem is here.
if (!IsPostBack)
{
lblother.visible = false;
txtother.visible = false;
}
This will work if you set Selected property of the default list item.
<asp:DropDownList ID="DropDownList" runat="server">
<asp:ListItem Text="Website" Selected="True"></asp:ListItem>
<asp:ListItem Text="Newspaper"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblOther" runat="server" Text="Other"></asp:Label>
<asp:TextBox ID="txtOther" runat="server"></asp:TextBox>
Hide the controls in the Page Load event.
protected void Page_Load(object sender, EventArgs e)
{
this.txtOther.Visible = false;
this.lblOther.Visible = false;
}
Then show the controls in the button click event.
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 2)
{
this.txtOther.Visible = true;
this.lblOther.Visible = true;
}
}

Resources