English 中文(简体)
How Do I Get a Dynamic Control s Value after Postback?
原标题:

I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created every time, even after postback. From there I moved the function to bind the ListView outside of the if (!Page.IsPostBack) conditional. Now I get the dynamic controls values but the static controls I have are set to their defaults. Here is a sample of what I am trying to accomplish:

For brevity, I left some obvious things out of this example.

<asp:ListView runat="server" ID="MyList" OnItemDataBound="MyList_ItemDataBound">
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
    </LayoutTemplate>

    <ItemTemplate>
        <asp:PlaceHolder runat="server" ID="ProductPlaceHolder">
            <asp:TextBox runat="server" ID="StaticField" Text="DefaultText" />
            <asp:PlaceHolder ID="DynamicItems" runat="server" />
        </asp:PlaceHolder>           
    </ItemTemplate>
</asp:ListView>

and here is the codebehind:

protected void MyList_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e) {
    PlaceHolder DynamicItems = (PlaceHolder)e.Item.FindControl("DynamicItems");
    DynamicItems.Controls.Add(textbox);
}

So, like I said, if I only databind when Page != PostBack then I cant find my dynamic controls on postback. If I bind every time the page loads then my static fields get set to their default text.

最佳回答

Try moving the data binding of the ListView into the OnInit() event.

问题回答

Very similar question (instead of populating a ListView the guy is generating a set of buttons). Briefly, you ll find that you have to store the items in the list in your Viestate - than fish it out on Postback and re-populate the list.

Note that this solutions implies dropping data-binding (which you might not wanna do for others reasons).

Hope it helps.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签