Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Solution: How to access parent key from inside User Control on page

This is how I did it, it involves a bit of Javascript trickery.

The thing to keep in mind, is that there really is no parent/child relationship between your grids. They are all just DIVs, and COT does fancy work to keep them in sync. Because the grids are rendered using JavaScript, COT doesn't (by default) provide a mechanism for telling your UserControl(s) about which parent row was clicked, or passing it's value. So let's roll our own.

1. Using COT designer, create a User Control and have it activate via Tab on your page. It will appear next to other child Tabs.

2. Determine the ID of the parent grid (DataViewExtender). Just open the page in Visual Studio and view the source. Mine is called "viewOrderExtender".

3. Here is the entire source of my UserControl below.
The important parts are:
a.Javascript pageLoad hooks into events so that you know when the parent row was clicked, and getting the primary key.
b. Hidden control on the usercontrol (hfPurchaseOrderId in my case).
c.AsyncPostBackTrigger is required for the postback to ocurr - dont' ask me, I just swiped off the net somewhere.
d. __doPostBack forces click on the hiddren button and the partial postback to occur, giving you opportunity to reload the control and use the parent keys value.

My UserControl Source.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="LinkOrderDocuments.ascx.cs" Inherits="Controls_LinkOrderDocuments" %>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<script type="text/javascript" language="JavaScript">

function pageLoad() {

var dataView = Web.DataView.find('viewOrdersExtender');
if (dataView) {

function onOrderSelected(sender) {
// Get the Key of selected record (from Master Orders grid)
var purchaseOrderId = Web.DataView.find('viewOrdersExtender').get_selectedKey();

// Get the Hidden field control
var obj = document.getElementById('<%=hfPurchaseOrderId.ClientID %>');

// Set value of hidden field on the User Control.
obj.value = purchaseOrderId;

//Force postback on panel.
__doPostBack("<%= btnHidden.ClientID %>", "");

<asp:HiddenField ID="hfPurchaseOrderId" runat="server" />
<asp:Button ID="btnHidden" runat="server" style="display:none;"/>

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

<asp:AsyncPostBackTrigger ControlID="btnHidden" EventName="Click" />


Code Behind:

using System;
using System.Linq;
using CuteWebUI;

public partial class Controls_LinkOrderDocuments : System.Web.UI.UserControl

protected void Page_Load(object sender, EventArgs e)

if (IsPostBack)
if (!String.IsNullOrEmpty(hfPurchaseOrderId.Value))
lblTest.Text = "Parent key is: " + hfPurchaseOrderId.Value;

protected void button_Click(object sender, EventArgs e)
// Not using this.

Happy coding!

5 people like
this idea