views:

100

answers:

2

It would be a simple table on the page, which has some initial cells and another user should enter. I use DataList with ItemTemplate:

<ItemTemplate>
    <asp:TextBox ID="CellTextBox" runat="server" Text='<%# Bind("Cell") %>' />
</ItemTemplate>

In code i override DataBind() of this control:

public override void DataBind()
        {
            MyDataList.DataSource = dataTable;
            MyDataList.DataBind();
        }

Now it doesn't work. User enter some cells and click submit button, but dataTable (in the method which handels this button OnClick event) doesn't contains user's data - just only initial. What should I do?

A: 

I'm not sure I entirely understand but I think you might be having a problem because you need to check for a post back on the page load. You might want to try something like this:

public void Page_Load(Object sender, EventArgs e)
{
  if (!this.IsPostBack)
  {
    myDataList.DataSource = myManager.GetDataTable();
    myDataList.DataBind();
  }
}

That way, the data will only be bound to the control on the intial page load and not subsequent post back requests. Page_ Load will be called on each request, and that means and code in your Page_Load will execute before your button click method. Therefore you need to check the IsPostBack property on your page to see if the current request is a post back. If it is, then don't bind your data to your control because you'll lose what the user has entered.

Charlie
Sorry, i get too little inforamation. There are the details
Sevina
I doubt if controls in ItemTemplate (not EditItemTemplate) could use #Bind and could retrieve user entered data
Sevina
It looks like because of using Session/ In debug mode the Table get from Session state. But how should i hadle this?
Sevina
A: 

Yes, I check post back propety of the page:

public void Page_Load(Object sender, EventArgs e) { if (!this.IsPostBack) myControl.DataBind(); }

MyControl code:

public class Mycontrol { private DataTable myDataTable = null; public DataTable MyDataTable; { set { Context.Session["MyDataTable"] = myDataTable = value; }

        get
        {
            if (myDataTable != null)
                return myDataTable;

            if (Context.Session != null && Context.Session["MyDataTable"] != null)
            {
                myDataTable = (DataTable)Context.Session["MyDataTable"];
            }
            else
            {
                myDataTable = GetData();
                Context.Session["MyDataTable"] = myDataTable;
            }
            return myDataTable;
        }
    }

    public override void DataBind()
    {
        MyDataList.DataSource = MyDataTable;
        MyDataList.DataBind();
    }

protected void CheckButton_Click(object sender, EventArgs e)
{
// There is MyDataTable as it was in the initial state
}

}

I suposed may be TextBox in ItemTemplate can not retrieve data? May be it can retrieve data just only if it specified in the EditItemTemplate? But i think, could not use EditItemTemplate in this situation

Sevina