API Questions on Kentico API.
Version 6.x > API > UniGrid on Actions View modes: 
User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 7/22/2013 7:45:52 AM
   
UniGrid on Actions
Hi,

I have created one page and placed one UniGrid on this page.

I have created Action event on page load as below :

protected void Page_Load(object sender, EventArgs e)
{
customgrid.OnAction += customgrid_OnAction;
}

I have one Upload button on the same page. Now when i click on my one Unigrid action button, It works fine. After this event, i click on Upload button at that time it fired grid's previous click action event instead of Upload button event.

How can i resolve this issue?


Thanks,

Atul Patel

User avatar
Member
Member
kentico_sandroj - 7/22/2013 3:07:38 PM
   
RE:UniGrid on Actions
Hello,

Could you please provide the full code so that we can take a look?

Thanks,
Sandro

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 7/25/2013 8:18:42 AM
   
RE:UniGrid on Actions
Hi sandroj,

Please see below code for your review.


Design Code:

<cms:UniGrid ID="customGrid" runat="server" GridName="custom_UniGrid.xml" OrderBy="ItemID" ShowActionsMenu="true" />


C# Code :


/// <summary>
/// On Load Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
try
{


// Assigns a handler for the OnAction event

customGrid.OnAction += customGrid_OnAction;
}
catch (Exception ex)
{

}
}

/// <summary>
/// Handles the UniGrid's OnAction event.
/// </summary>
protected void customGrid_OnAction(string actionName, object actionArgument)
{
try
{

//Defines the code used to implement the edit action
if (actionName == "edit")
{
-- Edit code here.
}
else if (actionName == "export")
{
-- Export code here.
}
else if (actionName == "delete")
{
-- Delete code here.
}
}
catch (Exception ex)
{

}
}

User avatar
Member
Member
kentico_sandroj - 7/25/2013 5:06:13 PM
   
RE:UniGrid on Actions
Hello,

Thanks for posting the code but it seems incomplete - the upload button you mentioned is not there?

The issue is likely in the code and not in the settings so we would need the full code in order to determine what the cause is.

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 7/26/2013 2:53:27 AM
   
RE:UniGrid on Actions
Hi,

I have written lot of code in Upload button event. So i have written steps below :

protected void btnUpload_Click(object sender, EventArgs e)
{
-- I have simply added code for importing excel into database.
-- Read the excel and generate datatable from this excel.
-- Validate each fields from generated datatable and based on this I am updating existing record and inserting new records.
-- finally print the message if successfully uploaded or not.
}

I have not written any code that call UniGrid action event in my upload button event code.

Thats it.


Thanks,

Atul Patel.

User avatar
Member
Member
kentico_sandroj - 7/26/2013 3:24:31 PM
   
RE:UniGrid on Actions
Hi Atul,

I have configured the following basic example and the code is fired correctly. I have tested this example in v6.0.58:


<%@ Register src="~/CMSAdminControls/UI/UniGrid/UniGrid.ascx" tagname="UniGrid" tagprefix="cms" %>
<%@ Register Namespace="CMS.UIControls.UniGridConfig" TagPrefix="ug" Assembly="CMS.UIControls" %>

<asp:Content ID="Content1" ContentPlaceHolderID="plcMain" Runat="Server">
<cms:UniGrid ID="customGrid" runat="server" ObjectType="cms.user" Columns="UserID, UserName" OrderBy="UserName">
<GridActions>
<ug:Action Name="edit" Caption="$General.Edit$" Icon="Edit.png" />
</GridActions>
<GridColumns>
<ug:Column Source="UserName" Caption="$general.username$" Width="100%" />
</GridColumns>
</cms:UniGrid>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</asp:Content>

-----------------------------

using CMS.UIControls;

public partial class CMSTemplates_CorporateSite_GridTest : TemplatePage
{
protected void Page_Load(object sender, EventArgs e)
{
customGrid.OnAction += customGrid_OnAction;
}

protected void customGrid_OnAction(string actionName, object actionArgument)
{
try
{

if (actionName == "edit")
{
Label1.Text += "edited";

}
else if (actionName == "export")
{
Label1.Text += "exported";
}
else if (actionName == "delete")
{
Label1.Text += "deleted";
}
}
catch (Exception ex)
{
Label1.Text += ex.ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text += "uploaded";
}
}

Please try this basic example and let me know if the correct event is fired. If there is an issue with the basic example, this means a bug is likely affecting the functionality. If the basic example works fine, it means that the issue is in the code somewhere. Without your full code I am not able to give you any specifics but I would be willing to review the code if you are able to post here or e-mail it to support@kentico.com.

Regards,
Sandro

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 7/26/2013 11:46:38 PM
   
RE:UniGrid on Actions
Hi Sandro,

Thanks for your reply.

I will try your code and let you know.


Thanks,
Atul Patel

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 8/1/2013 7:01:35 AM
   
RE:UniGrid on Actions
Hi Sandro,

I have tried your code and it is working fine.

But in my case, it is not working. Here i have pasted my sample code. Please have a look at it and let me know if anything wrong in my code.

Design Code


<%@ Register Src="~/CMSAdminControls/UI/UniGrid/UniGrid.ascx" TagName="UniGrid" TagPrefix="cms" %>
<%@ Register Namespace="CMS.UIControls.UniGridConfig" TagPrefix="ug" Assembly="CMS.UIControls" %>

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Grid.ascx.cs" Inherits="CMSWebParts_Feedback_Grid" %>
<br />
<br />
<asp:Button ID="btn" runat="server" Text="Upload" OnClick="btn_Click" />
       
<asp:LinkButton ID="lnkExportAll" runat="server" OnClick="btnExportall_Click">

<asp:Label ID="lblExportAll" runat="server" Text="Export All"></asp:Label>
</asp:LinkButton>
<br />
<br />
<br />
<br />
<cms:UniGrid ID="customGrid" runat="server" ObjectType="cms.user" Columns="UserID, UserName" OrderBy="UserName">
<GridActions>
<ug:Action Name="edit" Caption="$General.Edit$" Icon="Edit.png" />
<ug:Action Name="export" Caption="Export" Icon="Export.png" />
</GridActions>
<GridColumns>
<ug:Column Source="UserName" Caption="$general.username$" Width="100%" />
</GridColumns>
</cms:UniGrid>

<asp:Label ID="Label1" runat="server" Text=""></asp:Label>





C# Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.SiteProvider;
using CMS.GlobalHelper;
using CMS.CMSHelper;
using CMS.SettingsProvider;
using System.Data;
using CMS.FormControls;
using CMS.Ecommerce;
using CMS.PortalControls;
using CMS.UIControls;
using CMS.DataEngine;
using CMS.EventLog;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;

public partial class CMSWebParts_Feedback_Grid : CMS.PortalControls.CMSAbstractWebPart
{

protected void Page_Load(object sender, EventArgs e)
{
customGrid.OnAction += customGrid_OnAction;
}

protected void customGrid_OnAction(string actionName, object actionArgument)
{
if (actionName == "export")
{
try
{
GeneralConnection cn1 = ConnectionHelper.GetConnection();
DataSet ds = null;
QueryDataParameters parameters1 = new QueryDataParameters();

ds = cn1.ExecuteQuery("CMS.User.selectall", null);
HttpResponse response = HttpContext.Current.Response;
string filename = "SampleTest.xls";
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

// create a string writer
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
GridView dg = new GridView();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
catch (Exception ex)
{

}
}
}
protected void btnExportall_Click(object sender, EventArgs e)
{
try
{

GeneralConnection cn1 = ConnectionHelper.GetConnection();
DataSet ds = null;
QueryDataParameters parameters1 = new QueryDataParameters();

ds = cn1.ExecuteQuery("CMS.User.selectall", null);
HttpResponse response = HttpContext.Current.Response;
string filename = "SampleTest.xls";
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

// create a string writer
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
GridView dg = new GridView();
dg.DataSource = ds.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
catch (Exception ex)
{

}
}
protected void btn_Click(object sender, EventArgs e)
{
// No code is written.
}
}




To generate the error, Follow the following steps :

1. First click on export from grid action buttons. ( o/p : Excel is generated with Data.)
2. Now click on Upload button. (error o/p : Excel is generated with data even there is no any code written in Upload button event.)


Thanks,
Atul patel

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 8/5/2013 3:32:03 AM
   
RE:UniGrid on Actions
hi,

can anyone help me to resolve this issue?

Thanks,

Atul Patel

User avatar
Member
Member
kentico_sandroj - 8/5/2013 8:24:26 PM
   
RE:UniGrid on Actions
Hi Atul,

At this point I know that the issue is in the following button:
<asp:Button ID="btn" runat="server" Text="Upload" OnClick="btn_Click" />

It seems that if you replace this button with a LinkButton it works as expected.

I am not sure what causes the issue but the original button was executing the btnExportall_Click event every time, not the latest-used event as we assumed.

I will investigate a bit more to figure out what causes this but please let me know if this is an acceptable workaround.

Regards,
Sandro

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 8/6/2013 2:50:26 AM
   
RE:UniGrid on Actions
Hi Sandro,

Thanks for your reply.

I have checked my code and placed redirection to other page on btnExportall_Click event and it is not called every time as per your post.

Yes It is working fine when we use linkbutton instead of Button.

Thanks a lot to give solution for this issue. But still i have question that why it is not working with Button?


Thanks,

Atul Patel.

User avatar
Member
Member
kentico_sandroj - 8/6/2013 2:27:30 PM
   
RE:UniGrid on Actions
Hi Atul,

Thank you for your reply.

We found that the issue is in the asp Button - it behaves as a submit button by default and that has to be disabled in order to run the appropriate event:

<asp:Button ID="LinkButton1" runat="server" Text="Upload" OnClick="btn_Click" UseSubmitBehavior="false" />

Please try adding this property and let us know if the issue persists.

Thanks,
Sandro

User avatar
Certified Developer 8
Certified Developer 8
Atul Patel - 8/7/2013 3:50:42 AM
   
RE:UniGrid on Actions
Hi Sandro,

Thanks for your reply.

Yes, it is working fine.


Thanks,
Atul Patel