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

How to use the NeedDataSource

1 Answer 46 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Egbert
Top achievements
Rank 1
Egbert asked on 09 Oct 2013, 12:03 PM
Hi,

I'm using 2 grids on one page.
Selecting a row in the upper grid will show data in the lower grid.
This seems to work fine the way it is now.

In the lower grid I would like to use a command or button column to edit and delete items.
I've implemented these, but when clicking on of them the program will crash with the following error:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.


The solution is that you need to use the NeedDataSource event, but I don't know how.
When using this, clicking a row on the upper grid will not update the lower grid.

So how can I implement this so the edit/delete buttons functions will also work?

Thanks,
Egbert

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Tasks.aspx.cs" Inherits="Tasks" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div style="padding: 1%; width: 98%">
        <telerik:RadPanelBar ID="RadPanelBar1" runat="server" Width="100%">
            <Items>
                <telerik:RadPanelItem Text="Task details" runat="server">
                    <Items>
                        <telerik:RadPanelItem runat="server">
                            <ItemTemplate>
                                <table align="center">
                                    <tr>
                                        <td>
                                            Project Title
                                        </td>
                                        <td>
                                            Project Nr
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <telerik:RadTextBox ID="RadTextBoxProjectTitle" runat="server" Width="300px" Enabled="false">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                            <telerik:RadTextBox ID="RadTextBoxProjectNr" runat="server" Width="200px" Enabled="false">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <br />
                                        </td>
                                        <td>
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Title
                                        </td>
                                        <td>
                                            Nr
                                        </td>
                                        <td>
                                            Subsidy
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <telerik:RadTextBox ID="RadTextBoxTitle" runat="server" Width="300px">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                            <telerik:RadTextBox ID="RadTextBoxNr" runat="server" Width="200px">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                            <telerik:RadDropDownList ID="RadDropDownSubsidy" runat="server" Width="200px">
                                            </telerik:RadDropDownList>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            Description
                                        </td>
                                        <td align="right">
                                            Estimated Hours
                                            <telerik:RadMaskedTextBox ID="RadMaskedTextBox1" runat="server" Width="60px">
                                            </telerik:RadMaskedTextBox>
                                        </td>
                                        <td align="right">
                                            % Done
                                            <telerik:RadNumericTextBox ID="RadNumericTextBox1" runat="server" Width="60px">
                                            </telerik:RadNumericTextBox>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="3">
                                            <telerik:RadTextBox ID="RadTextBox1" runat="server" TextMode="MultiLine" Height="60"
                                                Width="100%">
                                            </telerik:RadTextBox>
                                        </td>
                                        <td>
                                        </td>
                                        <td>
                                        </td>
                                    </tr>
                                </table>
                            </ItemTemplate>
                        </telerik:RadPanelItem>
                    </Items>
                </telerik:RadPanelItem>
            </Items>
        </telerik:RadPanelBar>
        <br />
        <b>Projects</b>
        <telerik:RadGrid ID="RadGridProjects" runat="server" AllowPaging="True" ActiveItemStyle-Wrap="True"
            OnItemCreated="RadGrid1_ItemCreated" OnPreRender="RadGrid1_ItemPreRender" ShowStatusBar="true"
            AllowSorting="True" AllowMultiRowEdit="true" AutoGenerateColumns="false" OnSelectedIndexChanged="RadGridProjects_SelectedIndexChanged"
            OnNeedDataSource="RadGridProjects_NeedDataSource">
            <MasterTableView PageSize="5">
                <Columns>
                    <telerik:GridButtonColumn Text="Select" CommandName="Select">
                    </telerik:GridButtonColumn>
                    <%--                <telerik:GridButtonColumn Text="Deselect" CommandName="Deselect">
                </telerik:GridButtonColumn>
                    --%>
                    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn" Display="false">
                        <HeaderTemplate>
                            <asp:CheckBox ID="headerChkbox" OnCheckedChanged="ToggleSelectedState" AutoPostBack="True"
                                runat="server"></asp:CheckBox>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True"
                                runat="server"></asp:CheckBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="id" HeaderText="ID" UniqueName="id" Display="false">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="nr" HeaderText="Nr" UniqueName="nr">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="title" HeaderText="Title" UniqueName="title">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
            </ClientSettings>
        </telerik:RadGrid>
        <p>
        </p>
        <b>Tasks</b>
        <telerik:RadGrid ID="RadGridTasks" runat="server" AllowPaging="True" ActiveItemStyle-Wrap="True"
            OnItemCreated="RadGridTasks_ItemCreated" OnPreRender="RadGridTasks_ItemPreRender"
            ShowStatusBar="true" AllowSorting="True" AllowMultiRowEdit="true" AutoGenerateColumns="false"
            OnSelectedIndexChanged="RadGridTasks_SelectedIndexChanged" AllowAutomaticInserts="true"
            OnEditCommand="RadGridTasks_EditCommand" OnItemCommand="RadGridTasks_ItemCommand"
            OnNeedDataSource="RadGridTasks_NeedDataSource">
            <MasterTableView PageSize="10" NoMasterRecordsText="No data">
                <Columns>
                    <telerik:GridButtonColumn UniqueName="EditCommand" CommandName="Edit" ButtonType="PushButton"
                        Text="Edit">
                    </telerik:GridButtonColumn>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <ItemStyle CssClass="MyImageButton"></ItemStyle>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridButtonColumn Text="Select" CommandName="Select">
                    </telerik:GridButtonColumn>
                    <%--                <telerik:GridButtonColumn Text="Deselect" CommandName="Deselect">
                </telerik:GridButtonColumn>
                    --%>
                    <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn" Display="false">
                        <HeaderTemplate>
                            <asp:CheckBox ID="headerChkbox_RadGRidTasks" OnCheckedChanged="RadGridTasks_ToggleSelectedState"
                                AutoPostBack="True" runat="server"></asp:CheckBox>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox_RadGRidTasks" OnCheckedChanged="RadGridTasks_ToggleRowSelection"
                                AutoPostBack="True" runat="server"></asp:CheckBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="id" HeaderText="ID" UniqueName="idTask" Display="false">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="nr" HeaderText="Nr" UniqueName="nrTask">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="title" HeaderText="Title" UniqueName="titleTask">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings EnableRowHoverStyle="true">
            </ClientSettings>
        </telerik:RadGrid>
        <p>
        </p>
        <center>
            <telerik:RadButton ID="RadButtonAddTask" runat="server" Text="Add Task" OnClick="RadButtonAddTask_Click">
            </telerik:RadButton>
        </center>
        <telerik:RadWindow ID="RadWindowWarning" VisibleOnPageLoad="false" Title="Warning"
            runat="server" Modal="true" AutoSize="false" Width="400" Height="160" VisibleTitlebar="true"
            VisibleStatusbar="False" Behaviors="None">
            <ContentTemplate>
                <p style="text-align: center;">
                    <asp:Label runat="server" ID="labelWindow" Text="Please select a project!"></asp:Label>
                </p>
                <p style="padding: 10px; text-align: center;">
                    <telerik:RadButton ID="ButtonWindowOK" Text="OK" AutoPostBack="true" runat="server"
                        OnClick="ButtonWindowOK_Click" />
                </p>
            </ContentTemplate>
        </telerik:RadWindow>
    </div>
</asp:Content>

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 Ris.Classes;
using System.Data;
 
public partial class Tasks : System.Web.UI.Page
{
    private string userID = string.Empty;
    private string projectID = string.Empty;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Count == 0 || Session["Login"] == null || Session["Login"].ToString() == "No")
        {
            Response.Redirect("Login.aspx");
        }
 
        this.userID = Session["UserID"].ToString();
        this.projectID = Session["ProjectID"].ToString();
 
        this.ShowProjects();
 
        if (this.projectID != "0")
        {
            this.ShowTasks();
        }
    }
 
    protected void RadGridTasks_NeedDataSource(object sender, EventArgs e)
    {
        // this.ShowTasks();
    }
 
    protected void RadGridProjects_NeedDataSource(object sender, EventArgs e)
    {
        // this.ShowProjects();
    }
 
    private void ShowProjects()
    {
        DbProjects dbProject = new DbProjects();
 
        DataSet dataSet = dbProject.GetAllProjects(DbProjects.prjSelection.User, this.userID);
 
        this.RadGridProjects.DataSource = dataSet;
        this.RadGridProjects.DataBind();
    }
 
    private void ShowTasks()
    {
        this.projectID = Session["ProjectID"].ToString();
        if (this.projectID == "0")
        {
            return;
        }
 
        DbTasks dbTask = new DbTasks();
 
        DataSet dataSet = dbTask.GetAllTasks(DbTasks.tskSelection.User, this.projectID, this.userID);
 
        this.RadGridTasks.DataSource = dataSet;
        this.RadGridTasks.DataBind();
 
        this.RadGridProjects.Rebind();
    }
 
    protected void RadGridProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)RadGridProjects.SelectedItems[0];
 
        if (dataItem != null)
        {
            Session["ProjectID"] = dataItem["id"].Text;
            this.ShowTasks();
        }
    }
 
    protected void ToggleSelectedState(object sender, EventArgs e)
    {
        CheckBox headerCheckBox = (sender as CheckBox);
        foreach (GridDataItem dataItem in RadGridProjects.MasterTableView.Items)
        {
            (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
            dataItem.Selected = headerCheckBox.Checked;
        }
    }
 
    protected void RadGridTasks_ToggleSelectedState(object sender, EventArgs e)
    {
        CheckBox headerCheckBox = (sender as CheckBox);
        foreach (GridDataItem dataItem in RadGridTasks.MasterTableView.Items)
        {
            (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
            dataItem.Selected = headerCheckBox.Checked;
        }
    }
 
    protected void ToggleRowSelection(object sender, EventArgs e)
    {
        ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
    }
 
    protected void RadGridTasks_ToggleRowSelection(object sender, EventArgs e)
    {
        ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
    }
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            e.Item.PreRender += new EventHandler(RadGrid1_ItemPreRender);
        }
    }
 
    protected void RadGridTasks_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            e.Item.PreRender += new EventHandler(RadGridTasks_ItemPreRender);
        }
    }
 
    protected void RadGridTasks_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)RadGridTasks.SelectedItems[0];
 
        if (dataItem != null)
        {
            //Session["ProjectID"] = dataItem["id"].Text;
            //this.ShowTasks();
        }
    }
 
    protected void RadGrid1_ItemPreRender(object sender, EventArgs e)
    {
        try
        {
            ((sender as GridDataItem)["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox).Checked = (sender as GridDataItem).Selected;
        }
        catch
        {
        }
    }
 
    protected void RadGridTasks_ItemPreRender(object sender, EventArgs e)
    {
        try
        {
            ((sender as GridDataItem)["CheckBoxTemplateColumn"].FindControl("CheckBox1") as CheckBox).Checked = (sender as GridDataItem).Selected;
        }
        catch
        {
        }
    }
 
    protected void ButtonWindowOK_Click(object sender, EventArgs e)
    {
        this.RadWindowWarning.VisibleOnPageLoad = false;
    }
 
    protected void RadGridTasks_EditCommand(object source, GridCommandEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            //string id = item.GetDataKeyValue("id").ToString();
            //string id = item["id"].ToString();
        }
    }
 
    protected void RadGridTasks_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            int index = e.Item.ItemIndex;
            // string id = item.GetDataKeyValue("id").ToString();
            //string id = item["id"].ToString();
        }
    }
 
    protected void RadButtonAddTask_Click(object sender, EventArgs e)
    {
        if (Session["ProjectID"].ToString() == "0")
        {
            this.RadWindowWarning.VisibleOnPageLoad = true;
        }
        else
        {
            Session["EditTaskType"] = EditType.Add;
            Response.Redirect("TaskItem.aspx");
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Accepted
Eyup
Telerik team
answered on 14 Oct 2013, 11:03 AM
Hi Egbert,

Please note that using DataBind() is not recommended. Performing complex grid operations such as Inserting, Deleting, Updating, Hierarchy relations, Grouping, Paging, Sorting, Filtering, etc. require accommodating appropriate database operations.  Therefore, we suggest you to avoid Simple Databinding and strongly recommend the use of more advanced databinding methods, which automatically handle the aforementioned functions:
Declarative DataSource
Advanced Data Binding

You can check various demos for a practical implementation. In addition, you can refer to this sample for related grids using SQL datasources:
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/selectedvalue/defaultcs.aspx

Hope this helps.

Regards,
Eyup
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Egbert
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or