views:

805

answers:

2

Hi,

I got a user control containing a form. On a page that includes the user control, I need to populate the form in the user control, when the user pushes a button.

Heres how I've done it, and it doesn't work:

User Control:

public partial class editUC : System.Web.UI.UserControl
{
    public DataTable dataTable { set { UpdateForm(value); } }

    protected void Page_Load(object sender, EventArgs e) { }

    private void UpdateForm(DataTable dt) { txtControl.Text = dt.Rows..... ... }
}

Where UpdateForm binds different text boxes.

And the page containing the UC, when a button has been clicked:

EditUserControl.dataTable = dt;

Clicking the button, nothing happens. What am I missing here?

Thanks in advance.


UPDATE:

I got a bit closer. It's because I'm using a jQuery dialog for the user control. It works if I remove the jQuery.

This is how I create the dialog:

$(document).ready(function() {
    var dlg = $('.editFormDialog').dialog({
        autoOpen: false,
        height: 650,
        width: 550,
        modal: true,
        bgiframe: true,
        title: 'Rediger',
        open: function(type, data) {
            $(this).parent().appendTo("form");
        }
    });

    dlg.parent().appendTo("form");

    $('#btnEdit').live('click', function() {
        $('.editFormDialog').css('visibility', 'visible');
        $('.editFormDialog').dialog('open');
    });
});

I am appending it to the form, but it doesn't work. Any ideas?


2nd update:

It works if I remove the update panels from the page. Any ideas? :-)

A: 

User controls aren't meant to be as complex as this. They're meant to be a simple matter of pulling out some reusable portion of markup, then maybe adding a few properties, methods and events. They're not meant to do data binding. You really should rewrite tihs control as a composite server control.

That said, you'd probably get the most traction out of this by acting like it's a real server control. Binding to data would occur in the DataBinding event. In that event, you should set the DataSource property of the grid, then call DataBind on the grid.

John Saunders
Theres no grid in my user control. What do you mean?
Tommy Jakobsen
Sorry, thought it was a single grid. In this case, you'll have to databind the individual controls.
John Saunders
A: 
  1. are you using Caching on the control? If the control's contents is cached using the ASP.NET control caching mechanism, then there will be no change on the control's text.

  2. Can you add a breakpoint inside the private void UpdateForm(DataTable dt) to make sure this function gets called when you click on your button?

  3. Depending on what the user control contains you might need to do a EditUserControl.DataBind();

  4. If you are sending javascript code (jquery) as part of your update panel async postback, it's important to remember that javascript will not be evaluated by the browser. You need to find another method to run that code after your async postback.

Also, when you do an async postback on an UpdatePanel control, all the DOM elements in that control get trashed and replaced by new elements. The jQuery elements have to be re-created again.

Radu094
Also, is the user control being dyanmically loaded? If so, make sure it is getting loaded on all postbacks and that the id is kept the same everytime it is loaded. If the id changes or the control isn't reloaded, events and view state are lost on the user control.
Jeff Siver
1. No - 2. Yes. It gets called. - 3. The UpdateForm takes data out of the data table, "binding" it to text boxes. Calling DataBind() doesn't help.
Tommy Jakobsen
The UC is simply included like: <xx:yy ID="EditUserControl" runat="server" />. It's inside an UpdatePanel if that has anything to say?
Tommy Jakobsen
Well, if this is an async postback (from the button) then make sure the UpdatePanel gets "updated". ( see http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.updatemode.aspx )
Radu094
My update panel is set to update always.
Tommy Jakobsen