views:

58

answers:

1

Gurus, So I am trying to avoid enabling EnableViewState... Sample code as you can see has 1 repeater and 2 textboxes inside. I bind the textboxes at page init. After a postback I want to get the updated data from the client & save in a db.

The Request.Form contains the data keyed with autogenerated client ids but the repeater has 0 items after the postback. So my options seem limited to. a. Enable viewstate so I can pull the data from the repeater using Control.Find(...) b. iterate through Request.Form and find my textbox values...ugly!!

Any other suggestions? ultimatly the goal is to to render data from a datatable to some textboxes, allow the user to make changes then save these changes. I'd like to avoid viewstate if there is a clean alternative..

Thanks or the help.

ASPX:

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Foo.aspx.cs" Inherits="Ads_Foo" EnableViewState="false"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head id="head" runat="server">
        <title>Foo</title>
    </head>
    <body>
        <form id="form" runat="server">

<asp:Repeater ID="repImport" runat="server"  >
    <HeaderTemplate></HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox ID="lit1" runat="server" Text='<%# Eval("id") %>'/>
    <asp:TextBox ID="lit2" runat="server" Text='<%# Eval("data") %>'/>
    </ItemTemplate>
    </asp:Repeater>
    <asp:Literal ID="litOut" runat="server" text=""/>
    <asp:Button ID="btn" runat="server" OnClick="clicked" Text="btn" />
        </form>
    </body>
    </html>

Code Behind:

 protected void Page_Init(object sender, EventArgs e)
    { 
        if (!Page.IsPostBack) 
        {
            DataTable dt = new DataTable();

        dt.Columns.Add("cnt", typeof(int));               //item.ItemID
        dt.Columns.Add("data", typeof(string));               //item.ItemID
        DataRow row = dt.NewRow();
        row["cnt"] = 123;
        row["data"] = "Fake Item Id";
        dt.Rows.Add(row);

        DataRow row2 = dt.NewRow();
        row2["cnt"] = 999999;
        row2["data"] = "FPPPP";

        dt.Rows.Add(row2);

            repImport.DataSource = dt;
        repImport.DataBind();
        }
    }

    protected void clicked(object sender, EventArgs e)
    {

        foreach (RepeaterItem item in repImport.Items)
        {
            TextBox lit1 = (TextBox)item.FindControl("lit1");
            TextBox lit2 = (TextBox)item.FindControl("lit2");
            litOut.Text += lit1.Text;
        }
    }
A: 

You can try to always bind the repeater. Just set the initial values of the textboxes in if (!Page.IsPostBack)

korchev
wont matter. if he turns viewstate off, the server won't "see" the values when the form is posted (only in the raw http post data, not bound to server-side input elements)
RPM1984
Correct! that's the issue..
Ash