Bug reports
Version 3.x > Bug reports > Problem with GridView View modes: 
User avatar
Member
Member
rafael.musco - 12/1/2008 10:50:14 AM
   
Problem with GridView
Hi,
we are having problems with GridView data when using Kentico's WebPart.

I did one webpart for test that has a GridView with sort feature. The code looks like this:

public partial class CMSWebParts_IcapWebParts_CanaisdeInformacao_CronogramaResultados : CMSAbstractWebPart

and for DataBind...

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Empresa");
dt.Columns.Add("Referencia");
dt.Columns.Add("DataDivulgacao");
dt.Columns.Add("Horario");
dt.Columns.Add("Teleconferencia");
dt.Columns.Add("Status");

DataRow dr;
dr = dt.NewRow();
dr["Empresa"] = "Petrobrás";
dr["Referencia"] = "2T09";
dr["DataDivulgacao"] = "15/5/09";
dr["Horario"] = "Depois do Pregão";
dr["Teleconferencia"] = "16/12/08 (A definir)";
dr["Status"] = "Previsto";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Empresa"] = "Acesita";
dr["Referencia"] = "1T09";
dr["DataDivulgacao"] = "15/2/09";
dr["Horario"] = "Depois do Pregão";
dr["Teleconferencia"] = "16/12/08 (A definir)";
dr["Status"] = "Previsto";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Empresa"] = "Bematech";
dr["Referencia"] = "4T08";
dr["DataDivulgacao"] = "15/12/08";
dr["Horario"] = "Depois do Pregão";
dr["Teleconferencia"] = "16/12/08 (A definir)";
dr["Status"] = "Previsto";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Empresa"] = "Cosan";
dr["Referencia"] = "3T08";
dr["DataDivulgacao"] = "15/8/08";
dr["Horario"] = "Depois do Pregão";
dr["Teleconferencia"] = "16/12/08 (A definir)";
dr["Status"] = "Previsto";
dt.Rows.Add(dr);

GrdCronograma.DataSource = dt;
GrdCronograma.DataBind();
}
}

and for sorting:

private string ConvertSortDirectionToSql()
{
if (Session["SORT"] != null)
{
switch (Session["SORT"].ToString())
{
case "ASC":
Session["SORT"] = "DESC";
return "DESC";
case "DESC":
Session["SORT"] = "ASC";
return "ASC";
default:
Session["SORT"] = "ASC";
return "ASC";
}
}
Session["SORT"] = "ASC";
return "ASC";
}

protected void GrdCronograma_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GrdCronograma.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql();

GrdCronograma.DataSource = dataView;
GrdCronograma.DataBind();
}

GrdCronograma.Columns[0].HeaderStyle.CssClass = "sort";
GrdCronograma.Columns[1].HeaderStyle.CssClass = "sort";
GrdCronograma.Columns[2].HeaderStyle.CssClass = "sort";

switch (e.SortExpression)
{
case "Empresa":
if (Session["SORT"].ToString() == "DESC")
{
GrdCronograma.Columns[0].HeaderStyle.CssClass = "asc";
}
else
{
GrdCronograma.Columns[0].HeaderStyle.CssClass = "desc";
}
break;
case "Referencia":
if (Session["SORT"].ToString() == "DESC")
{
GrdCronograma.Columns[1].HeaderStyle.CssClass = "asc";
}
else
{
GrdCronograma.Columns[1].HeaderStyle.CssClass = "desc";
}
break;
case "DataDivulgacao":
if (Session["SORT"].ToString() == "DESC")
{
GrdCronograma.Columns[2].HeaderStyle.CssClass = "asc";
}
else
{
GrdCronograma.Columns[2].HeaderStyle.CssClass = "desc";
}
break;
}
}

Well, it works outside Kentico, but inside it doesn't... At this line:

DataTable dataTable = GrdCronograma.DataSource as DataTable;

The DataTable returns NULL. It looks like there is no ViewState for the GridView DataSource.

There is any way to "Enable" this ViewState?

Thanks,
Rafael

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 12/2/2008 8:44:34 AM
   
RE:Problem with GridView
Hi Rafael,

I tested your code and it worked correctly unless anything else caused the PostBack. Could you please disable the condition:

if (!IsPostBack)

and test it?

The data has to be bound always when it reloads the page due to the PostBack.

Best regards,
Helena Grulichova

User avatar
Member
Member
rafael.musco - 12/2/2008 10:15:26 AM
   
RE:Problem with GridView
Helena,
this is just what i don't want. In this code, the DataTable was built by me, but in real code, it's a service that i don't want to "call" in every postback.

The things i figured out are:
- the GridView.DataSource is always null. Inside or outside Kentico - Ok...
- Insede Kentico, the GridView.Rows.Count is always 0 after postback only in webparts.
If i use an UserControl webpart and than call an UserControl with this code, the GridView.Rows.Count will have the number of rows of the DataTable (4).

That's were my problem is. I need this GridView.Rows with values. In many cases, i need the values of a new row that an user insert.

What i don't understand is why UserControl Webpart calling another user control works and when i create a Webparte it doesn't.

Thanks,
Rafael

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 12/3/2008 3:46:31 AM
   
RE:Problem with GridView
Hi Rafael,

for more information about User controls please see here: http://devnet.kentico.com/docs/devguide/adding_custom_code_to_the_page.htm

The User control is the standard web part which has one improtant parameter – which control it should call.

If you register your code as a new web part it has to fullfil some criterias for web parts – i.e. the format of header - contrary of the control in User control web part. Please see here for more information about developing new web parts: http://devnet.kentico.com/docs/devguide/developing_web_parts.htm

I am not sure if I got your point. Could you please describe us the detailed scenario which you would like to achieve?

In my tests:
1. The data is in the GridView if the PostBack is caused by another web part.
2. No data is in the GridView if the PostBack is caused by something in the same control (e.g. the code with GridView also contains a button).
3. It does not depend of the fact it is a new registered web part or a parameter of User control web part.

Best regards,
Helena Grulichova

User avatar
Member
Member
rafael.musco - 12/3/2008 6:34:17 AM
   
RE:Problem with GridView
Hi Helena and thanks for your patience.... :-)

Let me try to be more detailed...

I have a register form. In this form, my clients must include their bank accounts. One client can have one or more bank accounts. After insert their bank accounts, they have to check wich one is the main account.

I'm using AJAX to do it more friendly for the user. I have a form with some fields as
"bank number", "agency number", "account number", one gridview and two buttons as "insert account" and "send".

When a user fill the "bank number", "agency number", "account number" and click in "insert account" button, i get those fields information and create a new line in the GridView.
The line in the GridView has the value of the fields "bank number", "agency number", "account number" and one more collumn that is the check box to set te main account.

As i told you, i'm using an UpdatePanel to do this friendly for the user.
As far it is ok. No problems with this.

My problem is when i click in the "send" button. At this point, i have to read all the lines in the GridView and send it to a WebService. In the "send" button i'm doing the following lines...

if (grdAccounts.Rows.Count > 0)
{
foreach (GridViewRow r in grdAccounts.Rows)
{
....
}
}

I have it as an UserControl. First i tried to use it with Kentico's UserControl WebPart.
No problem. The GridView has all rows that was inserted.

To have it integrated with Kentico, i changed it to be a WebPart. As WebPart, i can insert some configuration properties that are usefull.

After doing it, the if condition is always false. Doesn't matter how many accounts i insert in the GridView, there is always 0 rows.

I don't know why, but it looks like when using WebPart, the GridView doesn't have a ViewState.

One important thing. The UserControl WebPart only worked after i changed the following code (i read it in the forum):

//public override void OnContentLoaded()
//{
// base.OnContentLoaded();
// SetupControl();
//}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
SetupControl();
}

I have already tried to put it in my WebPart either...
I have already tried to set EnableViewState in WebPart, GridView, etc...

That is my problem...

Thanks again!
Rafael

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 12/4/2008 7:47:54 AM
   
RE:Problem with GridView
Hi Rafael,

thank you for your helpful explanation!

We would recommend you to develop one web part where you would use all you need to ensure that functionality – the part with the GridView would be embedded as same as all the other parts (AJAX, ..).

Or you could try use instead of OnInit method the OnLoad method in the web part.

Best regards,
Helena Grulichova

User avatar
Member
Member
vlado - 4/1/2011 12:48:06 PM
   
RE:Problem with GridView
I have similar problem with my Repeater. My simple (test) web part has one repeater and a button (outside of repeater) which does not do anything. When I click the button the content of repeater disappears. It works OK outside of Kentico. Here is the entire code:


<asp:Repeater ID="rptSource" runat="server">
<HeaderTemplate>
<table class="pretty-table" summary="This table shows sources" style="width: 100%;">
<tr>
<th scope="col">
Source Name
</th>
<th scope="col" style="width: 350px;">
Note
</th>
<th scope="col">
Amount
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th scope="row">
<asp:Label ID="lblAccountName" runat="server" Text='<%# Eval("SourceName") %>' />
</th>
<td>
<%# Eval("Directions") %>
</td>
<td>
<asp:TextBox ID="txtShare" runat="server" Style="width: 50px;" Text='<%# Eval("Amount") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button ID="btnChangeSources" runat="server" Text="Continue" />



protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Enrollment enrollment = new Enrollment();
enrollment = CreateEnrollmentObject();
rptSource.DataSource = enrollment.Sources;
rptSource.DataBind();
}
}


User avatar
Member
Member
paolo.enrico.costa-gmail - 4/6/2011 10:59:00 AM
   
RE:Problem with GridView
Hello,

I have exactly the same problem. I have a grid that loses its content after a postback (it's a simple button with no event attached), is there a solution for this?

Paolo

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 4/7/2011 8:00:15 AM
   
RE:Problem with GridView
Hello,


could you please try to use a User cotrol web part for your control:

Adding custom code to the Portal template

instead of using web part?

The User control web part has a standard ASP.NET life cycle whilst the CMSAbstractWebPart has a different and it could lose the state.

Other possibility is to save data for controls to session variables and load them in every postback.


Best regards,
Helena Grulichova

User avatar
Certified Marketer 12
Certified Marketer 12
james-distinctionhq - 4/7/2011 9:05:16 AM
   
RE:Problem with GridView
Hi Helena,

Could you clarify how the page lifecycle has been altered within the portal engine.

We've got a similar issue where neither using the usercontrol webpart or storing in the session are suitable solutions.

Thanks

User avatar
Kentico Developer
Kentico Developer
kentico_helenag - 4/11/2011 6:05:42 AM
   
RE:Problem with GridView
Hi,


you can find an explanation here: Kentico Connection - Web parts development.

I hope it will help you.


Best regards,
Helena Grulichova