By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

DataBinding lost of Postback

P: 1
I have a stupid problem but not able to find solution :(

I have repeater that create user control (WebUserControl1) . I pass parameter (InterationString) to user control that is "Container.DataItem" of repeater.

When Usercontrol postback page (autopostback=true on dropdownlist), UserControl lose parameter value. InterationString is correctly set on first loading but on postback, IterationString property is null.

Thank for your help.

Here is the code :

******************************** Web Page ************************************

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="repeater1" DataSource="<%# GetSource %>">
<ItemTemplate>
<uc1:WebUserControl1 id="WebUserControl1_1" InterationString='<%# Container.DataItem %>' runat="server">
</uc1:WebUserControl1>
<br /><br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>



public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
this.DataBind();
}

public string[] GetSource
{
get { return new string[] { "Iteration 1","Iteration 2" }; }
}
}



******************************** User Control ************************************

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>

<%= InterationString %> => <asp:DropDownList runat=server ID="ddlAssets" AutoPostBack="true" OnSelectedIndexChanged="ddlAssets_OnSelectedIndexC hanged">
<asp:ListItem Value="1" Text="Test 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Test 2"></asp:ListItem>
</asp:DropDownList>


public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

public string InterationString;


protected void ddlAssets_OnSelectedIndexChanged(object sender, EventArgs args)
{
Response.Write("Event catched.");
}
}
Jul 12 '07 #1
Share this Question
Share on Google+
1 Reply


nateraaaa
Expert 100+
P: 663
I have a stupid problem but not able to find solution :(

I have repeater that create user control (WebUserControl1) . I pass parameter (InterationString) to user control that is "Container.DataItem" of repeater.

When Usercontrol postback page (autopostback=true on dropdownlist), UserControl lose parameter value. InterationString is correctly set on first loading but on postback, IterationString property is null.

Thank for your help.

Here is the code :

******************************** Web Page ************************************

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc1" %>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater runat="server" ID="repeater1" DataSource="<%# GetSource %>">
<ItemTemplate>
<uc1:WebUserControl1 id="WebUserControl1_1" InterationString='<%# Container.DataItem %>' runat="server">
</uc1:WebUserControl1>
<br /><br />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>



public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
this.DataBind();
}

public string[] GetSource
{
get { return new string[] { "Iteration 1","Iteration 2" }; }
}
}



******************************** User Control ************************************

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>

<%= InterationString %> => <asp:DropDownList runat=server ID="ddlAssets" AutoPostBack="true" OnSelectedIndexChanged="ddlAssets_OnSelectedIndexC hanged">
<asp:ListItem Value="1" Text="Test 1"></asp:ListItem>
<asp:ListItem Value="2" Text="Test 2"></asp:ListItem>
</asp:DropDownList>


public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}

public string InterationString;


protected void ddlAssets_OnSelectedIndexChanged(object sender, EventArgs args)
{
Response.Write("Event catched.");
}
}
Try storing the value of your InterationString in a Session variable. This should eliminate your problem of losing the InterationString value on a postback.

Nathan
Jul 12 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.