سؤال

I have a got Gridview, that gets its data by:

internal void updateGrid()
{
    List<String> data = dt.getAlldata(UserID(), ListID());
    gridViewer.DataSource = data;
    gridViewer.DataBind();
}

After a button click the database is updated and the new data is shown. Everything works fine.

Now I have to replace the string with an Usercontrol:

internal void updateGrid()
{
    List<String> data = dt.getAlldata(UserID(), ListID());
    gridViewer.DataSource = data;
    gridViewer.DataBind();

    for (int i = 0; i < gridViewer..Rows.Count; i++)
    {
        UCControl dum = (UCControl)LoadControl("~/UCControl.ascx");
        dum.SetData(gridViewer.Rows[i].Cells[0].Text, false);
        gridViewer.Rows[i].Cells[0].Controls.Clear();
        gridViewer.Rows[i].Cells[0].Controls.Add(dum);
    }
}

After first Page_Load(), everything is shown correctly. But when I click my buttons, the Usercontrols do not repaint. The data is set correctly inside, but it does not update.

Because this Control reacts by javascript on UserInputs on the client side, every Usercontrol has got his own Id that is set by

this.ID = "UCControl" + data[0];

data[0] is unique, but known during the whole process.

Can somebody tell me why the UserControl does not repaint? Or better: How do I tell the Usercontrols to update?

هل كانت مفيدة؟

المحلول

From your code I would say that once the data is bound then you can't go in immediately after and start to change it. You would need to hook into one of the databinding events.

If you were using a list viem ItemDataBound would be a good candidate to hook into. But the GridView has a more limited set of events and doesn't offer that level of control - so you are a bit stuck on that score.

In my experience using dynamic controls in asp.net (LoadControl) is just a bit fraught. I think a better option would be to publicly expose a property in your user control and bind to that.

This question gives a good description of how to achieve this

asp.net user controls binds inside gridview

Hope that helps

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top