This is a migrated thread and some comments may be shown as answers.

Bind RadGrid on button Click

5 Answers 959 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Reena
Top achievements
Rank 1
Reena asked on 22 Jul 2010, 02:43 PM
Hi,
I am trying to bind the radgrid on button click but it doesnt work neither I get any error. The same code works perfectly fine when I move the binding on Page load.
Why is it so??

protected

 

void Button1_Click(object sender, EventArgs e)

 

{

 

RAASBLL Refresh = new RAASBLL();
//Calls the method which has the query in it

 

 

DataTable dtPlazas = Refresh.AllPlazas();

 

RadGrid2.MasterTableView.DataSource = dtPlazas.DefaultView;

RadGrid2.DataSource = dtPlazas.DefaultView;

RadGrid2.Databind ();

}

 

<

 

telerik:RadGrid ID="RadGrid2" runat="server" AllowFilteringByColumn="True"

 

 

autogeneratecolumns="False" datakeynames="PLAZA" AllowPaging="true" >

 

 

 

 

 

<MasterTableView IsFilterItemExpanded="false" CommandItemDisplay="Top">

 

 

<CommandItemSettings ExportToPdfText="Export to Pdf">

 

 

 

 

 

</CommandItemSettings>

 

 

 

 

 

<RowIndicatorColumn>

 

 

 

 

 

<HeaderStyle Width="20px"></HeaderStyle>

 

 

 

 

 

</RowIndicatorColumn>

 

 

 

 

 

<ExpandCollapseColumn>

 

 

 

 

 

<HeaderStyle Width="20px"></HeaderStyle>

 

 

 

 

 

</ExpandCollapseColumn>

 

 

 

 

 

<Columns>

 

 

<telerik:GridBoundColumn DataField="PLAZA" HeaderText="Plaza" DataType="System.Decimal" SortExpression="PLAZA" UniqueName="PLAZA" />

 

 

 

 

 

<telerik:GridBoundColumn DataField="PLAZA_NAme" HeaderText="Plaza Name" DataType="System.String" SortExpression="PLAZA_NAME" UniqueName="PLAZA_NAME" />

 

 

 

 

 

</Columns>

 

 

 

 

 

</MasterTableView>

 

 

 

 

 

</telerik:RadGrid>

 

<

 

asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Refresh" />

 

 

 


Please help.

Thanks,
Reena

5 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 22 Jul 2010, 03:29 PM
Hi Reena,

Could you please specify what happens after you click the button? No data is displayed in the grid or the grid disappear after particular actions?

Additionally, you can refer to this article for more information on RadGrid data-binding.

Regards,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Reena
Top achievements
Rank 1
answered on 22 Jul 2010, 03:34 PM
Since I am not binding the RadGrid at design time, RadGrid doesnot appear on the page. Technically after clicking on the Button and setting the RadGrid DataSource property, the radgrid should show data, but it does not.
Eveything works fine if I set
RadGrid1.DataSource=dtPlazas;
RadGrid1.DataBind();

on PageLoad but same doesn't work on Button Click

Thanks,
Reena
0
Iana Tsolova
Telerik team
answered on 22 Jul 2010, 03:45 PM
Hi Reena,

I followed your scenario and prepared a sample project however I was not able to replicate the issue in question. Could you please check it out and let me know what differs in your case?

Regards,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Reena
Top achievements
Rank 1
answered on 22 Jul 2010, 04:18 PM
Hi,
Your example worked fine for me. I am attaching a sample of my Page, you will have to modify the database query.
If you see this code doesnt work for me, dont know where the problem is?
But if I move the code under Button1_click on Page_Load (have commented that code on Page_Load) everything works fine. Can you check and tell me where is the problem.
I could not attach the sampel project zip file so am pasting the code below:

CS File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
  
public partial class Members_ETC_Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //    RadGrid1.DataSource = GetData();
    }
    protected void HeaderContextMenu_PreRender(object sender, EventArgs e)
    {
        RadMenuItem item = new RadMenuItem("Show filter");
        item.Value = "FilterBuilder";
        item.Attributes["ColumnName"] = string.Empty;
        item.Attributes["TableID"] = string.Empty;
        item.PostBack = false;
        item.Font.Bold = true;
        RadGrid1.HeaderContextMenu.Items.Add(item);
    }
  
    protected void ApplyButton_Click(object sender, EventArgs e)
    {
        RadFilter1.FireApplyCommand();
        RadGrid1.DataSource = GetData() ;
        RadGrid1.Rebind();
    }
  
    protected string GetFilterIcon()
    {
        //RadGrid1.Skin = RadSkinManager1.Skin ;
        //RadGrid1.Rebind();
  
        return RadAjaxLoadingPanel.GetWebResourceUrl(Page, string.Format("Telerik.Web.UI.Skins.{0}.Grid.Filter.gif", "Vista"));
    }
  
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid1.DataSource = GetData();
    }
  
    private DataTable GetData()
    {
        using (OracleConnection oraconn = new OracleConnection(ConfigurationManager.ConnectionStrings["RaasConnString"].ToString()))
        {
            oraconn.Open();
            using (OracleCommand oracommand = oraconn.CreateCommand())
            {
                oracommand.CommandText = "Select Plaza, Plaza_Name from t_ref_plaza";
                DataSet ds = new DataSet();
                OracleDataAdapter da = new OracleDataAdapter(oracommand);
                da.Fill(ds);
                return ds.Tables[0];
            }
        }
    }
}


aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Members_ETC_Test" %>
  
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
    <telerik:RadScriptBlock runat="server" ID="RadScriptBlock1">
  
    <script type="text/javascript">
        function openFilterBuilderDialog()
        {
            $find('<%=RadWindow1.ClientID %>').show();
        }
  
        function hideFilterBuilderDialog()
        {
            $find('<%=RadWindow1.ClientID %>').close();
        }
  
        function OnHeaderMenuItemClicked(sender, args)
        {
            if (args.get_item().get_value() == "FilterBuilder")
            {
                openFilterBuilderDialog();
            }
        }
  
        function onPanelBarItemClicked(sender, args)
        {
            if (args.get_item().get_commandName() == "OpenRadFilter")
            {
                openFilterBuilderDialog();
            }
        }
    </script>
  
</telerik:RadScriptBlock>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
<telerik:RadAjaxManager runat="server" ID="RadAjaxManager1" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadFilter1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadFilter1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="ApplyButton">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1" />
        <telerik:RadWindow ID="RadWindow1" runat="server" Behaviors="Move,Close,Resize" Title="Filter Builder"
            Modal="true" Width="500" Height="350">
            <ContentTemplate>
                <telerik:RadFilter runat="server" ID="RadFilter1" FilterContainerID="RadGrid1" ShowApplyButton="false" style="margin:10px 0 0 10px"/>
                <asp:Panel ID="FilterButtonPanel" runat="server" style="margin:10px 0 0 10px;font-size:medium">
                    <asp:LinkButton runat="server" ID="ApplyButton" OnClick="ApplyButton_Click" Font-Names="Verdana" Font-Size="Small"
                    Text="Apply Expressions" OnClientClick="hideFilterBuilderDialog()"/>
                </asp:Panel>
            </ContentTemplate>
        </telerik:RadWindow>
        <telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" 
            AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True" 
            EnableHeaderContextMenu="True">
            <MasterTableView IsFilterItemExpanded="false" CommandItemDisplay="Top" 
                DataKeyNames="PLAZA">
                <CommandItemTemplate>
                    <telerik:RadToolBar runat="server" ID="RadToolBar1" OnClientButtonClicked="onPanelBarItemClicked">
                        <Items>
                            <telerik:RadToolBarButton Text="Show filter" CommandName="OpenRadFilter" ImageUrl="<%#GetFilterIcon() %>"
                                ImagePosition="Right"/>
                        </Items>
                    </telerik:RadToolBar>
                </CommandItemTemplate>
                <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                <Columns>
                    <telerik:GridBoundColumn DataField="PLAZA" HeaderText="PLAZA" 
                        DataType="System.Decimal" ReadOnly="True" SortExpression="PLAZA" 
                        UniqueName="PLAZA" />
                    <telerik:GridBoundColumn DataField="PLAZA_NAME" HeaderText="PLAZA_NAME" 
                        SortExpression="PLAZA_NAME" UniqueName="PLAZA_NAME" />
                </Columns>
            </MasterTableView>
            <HeaderContextMenu OnPreRender="HeaderContextMenu_PreRender" OnClientItemClicked="OnHeaderMenuItemClicked">
            </HeaderContextMenu>
        </telerik:RadGrid>       
          
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </div>
    </form>
</body>
</html>


Thanks,
Reena

0
Iana Tsolova
Telerik team
answered on 28 Jul 2010, 08:47 AM
Hi Reena,

When the RadGrid data source is assigned later than Page_Load, you need to explicitly call the DataBind() method. Therefore can you try modifying the Button1_Click handler as below as see if it makes any difference?

protected void Button1_Click(object sender, EventArgs e) 
    RadGrid1.DataSource = GetData(); 
    RadGrid1.DataBind();
}


Kind regards,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Reena
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Reena
Top achievements
Rank 1
Share this question
or