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

[Solved] Item Not Being Selected

2 Answers 121 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pradeep
Top achievements
Rank 1
Pradeep asked on 09 Apr 2013, 07:47 PM
Hello,

I have an Edit button located on my MasterPage and a RadGrid located in my Default.aspx page. I am trying to get the Session ID of the row selected in the Default.aspx page and pull in those values into my Edit page.
When I select a row in Default.aspx and then click Edit, the page refreshes and the row is deselected.
What am I doing wrong here?

Here is my code behind for the MasterPage Edit button:
protected void lEditItem_Click(object sender, EventArgs e)
{
    GetSelected();
    if (Session["CriteriaID"] != null)
    {
        Session["AccessMode"] = "";
        Response.Redirect("~/EditCriteria.aspx");
    }
}
 
private void GetSelected()
{
    RadGrid control = (RadGrid)PageContent.FindControl("criteriaList");
    foreach (GridDataItem item in control.MasterTableView.Items)
    {
        if (item.Selected)
        {
            String temp = item.GetDataKeyValue("ID").ToString();
            Session["CriteriaID"] = new Guid(item.GetDataKeyValue("ID").ToString());
 
        }
    }
}

The code for the RadGrid in Default.aspx is
<asp:Content ID="Content2" ContentPlaceHolderID="PageContent" runat="server">
    <div class="PageBlock">
            <div class="SecHeader">Criteria List</div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:CriteriaCS %>"
         
                SelectCommand="SELECT [AssetOwner], [CriteriaName], [CriteriaDescription], [CriteriaType], [ID] FROM [Study]">
    </asp:SqlDataSource>
    <telerik:RadGrid ID="criteriaList" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1"
        GridLines="None" AllowSorting="True" AutoGenerateColumns="False">
        <ClientSettings EnablePostBackOnRowClick="True">
            <Selecting AllowRowSelect="True" />
            <ClientEvents OnRowSelected="RowSelected"></ClientEvents>
        </ClientSettings>
        <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="ID">
            <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn Visible="True"
                FilterControlAltText="Filter ExpandColumn column" Created="True">
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridBoundColumn DataField="AssetOwner" FilterControlAltText="Filter AssetOwner column"
                    HeaderText="AssetOwner" SortExpression="AssetOwner" UniqueName="AssetOwner">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CriteriaName" FilterControlAltText="Filter CriteriaName column"
                    HeaderText="CriteriaName" SortExpression="CriteriaName" UniqueName="CriteriaName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CriteriaDescription" FilterControlAltText="Filter CriteriaDescription column"
                    HeaderText="CriteriaDescription" SortExpression="CriteriaDescription" UniqueName="CriteriaDescription">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CriteriaType" FilterControlAltText="Filter CriteriaType column"
                    HeaderText="CriteriaType" SortExpression="CriteriaType" UniqueName="CriteriaType">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ID" DataType="System.Guid"
                    FilterControlAltText="Filter ID column" HeaderText="ID" ReadOnly="True"
                    SortExpression="ID" UniqueName="ID" Visible="False">
                </telerik:GridBoundColumn>
            </Columns>
            <EditFormSettings>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                </EditColumn>
            </EditFormSettings>
            <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
        </MasterTableView>
        <PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>
    </div>
</asp:Content>



Thanks!!!

2 Answers, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 12 Apr 2013, 01:02 PM
Hello Pradeep,

It is preferable to extract the id when the grid row is clicked. You can achieve this by handling the OnItemCommand of the grid. This is also demonstrate in the code snippet below:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "RowClick")
        {
            Session["CriteriaID"] = (e.Item as GridDataItem).GetDataKeyValue("ID").ToString();
        }
    }

Later when the button in the master page is clicked you can extract the value directly from the session variable.
protected void lEditItem_Click(object sender, EventArgs e)
    {
        if (Session["CriteriaID"] != null)
        {
            string id = Session["CriteriaID"].ToString();
        }
    }


Regards,
Angel Petrov
the Telerik team
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 their blog feed now.
0
Pradeep
Top achievements
Rank 1
answered on 13 Apr 2013, 03:49 PM
Thanks for the reply!

Actually, I had created an OnClick event for the button and deleted it from the *.cs file, but I didn't remove the OnClick ID from the .aspx file. So, when clicking, a non-existing event was occurring, which disrupted the GetSelected() process.

Thank you again for taking the time to reply!

-Pradeep K.
Tags
Grid
Asked by
Pradeep
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Pradeep
Top achievements
Rank 1
Share this question
or