Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Losing datagrid info on PostBack

  Asked By: Alisha    Date: Dec 09    Category: Sharepoint    Views: 1341

I'm losing info on a postback for a datagrid with dynamically-added
columns, including a template column. The strange thing is that, if
the postback is generated by BrowserDesignMode (for instance, when
trying to add a new web part to the same page), the datagrid shows
after I close the toolpart.

This is what I'm trying to do (abridged code below). I have a text
box _tbEnter and a search button _btnSearch. When user enters text
and clicks on search, I query a SPList and return the results in a
datagrid _dg.

The columns in the _dg are dynamically generated, including a
template column with check boxes, so users can subscribe to alerts on
a per-item basis.

There is a button _btnAlert that causes a post back so I can search
through checked check boxes and add alerts accordingly (the _btnAlert
only becomes visible after a search on the list is done). It is when
I click on this _btnAlert button that I seem to lose state info.

However, I noticed that I don't necessarily lose all information on
posting back the page. For instance, if instead of clicking on the
_btnAlert, I click to open the toolpart, when the toolpart opens the
results on the _dg disappear, just as if I had clicked on _btnAlert.

Nevertheless, when I close the toolpart the structure of the _dg
shows up (without the info inside). So if my search results had 3
items and I open the toolpart, first the whole _dg disappears, but
when I close the toolpart the _dg appears without data, but showing 3
rows and all the columns (empty).

This tells me that some of the info is in the viewstate. Because of
this, I tried many things, like rebinding the datasource to the _dg,
but even this didn't work.

I read about the page lifecycle, and tried to rebind at different
stages before rendering the page (like LoadViewState) but still was
not able to make it displayed.

I've been trying for a week now, so I would appreciate any help. This
is the code:



//using statements

namespace MyWebParts
{

public class AddAlerttoItem : WebPart
{

// some variables, such as
private string ErrorMessage = "";

// some web controls, such as...
protected DataGrid _dg;

#region Search List using text entered in _tbEnter

private void _btnSearch_Click(object sender, EventArgs e)
{
try
{
if(_tbEnter.Text !="")
{
SPQuery query = new SPQuery();
//get viewFields from one of the views
query.ViewFields = ReqList.Views[1].ViewFields.SchemaXml;
query.Query = "some calm query";
_results = ReqList.GetItems(query);

_dg.DataSource = _results.GetDataTable();
_dg.DataBind();

_btnAlert.Visible = true;

}
else
{
ErrorMessage = "You need to enter some text before clicking search.";
}
}
catch(SystemException se)
{
ErrorMessage = "Error retrieving data: "+ se.Message;
}
}
#endregion


#region Add Alerts Event
// once user searched results, the send alert button becomes

private void _btnAlert_Click(object sender, EventArgs e)
{
try
{
foreach(DataGridItem row in _dg.Items)
{
if((row.FindControl("addAlert") as CheckBox).Checked)
{
// add alert to each item
}
}
_btnAlert.Visible = false;
}
catch(SystemException se)
{
ErrorMessage = "Error adding alerts: "+se.Message;
}
}
#endregion

protected override void CreateChildControls()
{
try
{

_tbEnter = new TextBox();
Controls.Add(_tbEnter);

_btnSearch = new Button();
_btnSearch.Text = "Search";
_btnSearch.Click +=new EventHandler(_btnSearch_Click);
Controls.Add(_btnSearch);

_btnAlert = new Button();
_btnAlert.Text = "Add Alerts";
_btnAlert.Visible = false;
_btnAlert.Click +=new EventHandler(_btnAlert_Click);
Controls.Add(_btnAlert);

this.AddGrid();

}
catch(SystemException se)
{
ErrorMessage = "Error creating controls: "+ se.Message;
}
}

#region Create and add columns

private void AddGrid()
{
_dg = new DataGrid();
_dg.AutoGenerateColumns = false;
_dg.EnableViewState = true;
Controls.Add(_dg);

//Add check column
TemplateColumn sendAlert = new TemplateColumn();
sendAlert.HeaderText = "Send Alert";
sendAlert.ItemTemplate = new ChBoxTemplate();
_dg.Columns.Add(sendAlert);

//get fields object to obtain display name
SPFieldCollection ListFields = ReqList.Fields;

//internal fields
SPViewFieldCollection fields = ReqList.Views[1].ViewFields;

System.Collections.Specialized.StringCollection
InternalViewFldNames = fields.ToStringCollection();

BoundColumn C = null;

for(int i=0; i< InternalViewFldNames.Count; i++)
{
C = new BoundColumn();
C.HeaderText = ListFields.GetFieldByInternalName(InternalViewFldNames
[i]).Title;
C.DataField = InternalViewFldNames[i];
_dg.Columns.Add(C);
}

BoundColumn ID = new BoundColumn();
ID.DataField = "ID";
ID.Visible = false;
_dg.Columns.Add(ID);
}
#endregion

protected override void RenderWebPart(HtmlTextWriter output)
{

// code to render web part
}

#region Class to build a checkbox template
class ChBoxTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
CheckBox sendAlert = new CheckBox();
sendAlert.ID = "addAlert";
container.Controls.Add(sendAlert);
}

}
#endregion

}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Losing datagrid info on PostBack Or get search suggestion and latest updates.




Tagged: