开发者

Pulling data from Repeater with EnableViewState=false ... How/Alternative?

开发者 https://www.devze.com 2023-01-16 22:22 出处:网络
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 upda

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">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <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;
        }
    }


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


Atanas is more or less correct. The repeater needs to be rebound on every request in the absense of viewstate. If you're worred about your datasource changing between posts, you'll need to store your datatable or smaller datastructure somewhere (viewstate, session) and rebind to that.

0

精彩评论

暂无评论...
验证码 换一张
取 消