When do I have to bind?

Hyuga

Freshman
Joined
Jul 21, 2005
Messages
30
My scenario: an aspx page with a custom control on it, containing a Datagrid. I have to assign some control properties programatically, and I do it in the Page_Load event from the aspx page, then I DataBind the control. In the Page_Load of the control, I Bind the control only when Not IsPostBack. I don't know it it is correct, but it shows what I expect. If I don't bind the control after assigning the properties, the control is not shown.

Now, I want to edit some fields of the Datagrid. I Click on the edit button and the textboxes from the EditItemTemplate appeared. Great.

But now, I edit the values and click on the Update Button. When I try to retrieve the values from the textboxes, they are not loaded. Instead of that, the labels of the ItemTemplate part has been loaded again.

I Databind the Datagrid again on the Update Command event handler, but always after trying to recover the values from the textboxes. But I think that some of the bindings I'm done on both the aspx and the ascx are doing the change.

Anyone has any idea? If the explanation is not clear, I'll try to be more precise.
 
kahlua001 said:
Do you have the correct Command attribute set for your Update button?

the command attribute was set to "update", and in the DataGrid settings, the OnUpdateCommand property is set to my handler.

In fact, the program flow is reaching my handler function, and the error occurs inside of it, when I'm trying to recover the text from the textbox.
 
Another Clue: As I expected, when the page reach my OnUpdateCommand handler, the Datagrid.EditItemIndex value is set, uncorrectly, to -1, and the item.ItemIndex from the DataGridCommandEventArg is set, as it should, to the index edited. I can solve my problem using the FindControl function directly over the Datagrid, instead of using it over the DataGridCommandEventArg, but this doesn't seem very "clean" for me.

Tomorrow I will write the parts of the code involved in this mess, hoping that it will help.
 
Well, the important parts of the code. Let's assume that my page is called page.aspx, and my control is called control.ascx

In page.aspx, in the Page_Load:
Code:
control.var1 = X
control.var1 = Y
control.var1 = Z

inside the HTML:
Code:
<dgH:dgHoraio id="control" EnableViewState="false" runat="server"></dgH:dgHorario>

In control.ascx, in the Page_Load:
Code:
If Not Me.IsPostBack Then
     BindGrid()
End If


NOTE: The fist time the page load, everything showed great. Now, when I click the edit button, the Datagrid dissapeared. Before, I use a control.BindGrid() in the Page_Load() event from page.aspx and the DG is showed always, but the I've the problem of the texboxes not loading after the update command.

¿Any idea?
 
Thanks kahlua!

This is the solution. I've added EnableViewState="false" to the control and to the Datagrid, and this is causing the problem.

I've added this because when facing with my first problems in handling events from the controls I've read somewhere that I should use it. I assume that I've missunderstood the explanation :D

Now it is working as expected ;)
 
Back
Top