Thursday, March 29, 2012

Passing values between contentplaceholders

I have on the same page two ojbectdatasources (ods1, ods2) located in distinct contentplaceholders (CPL1, CPL2). Both are tied to gridviews (grv1, grv2). Objectdatasource ods2 uses grv1 datakey 'ProdCatID' as select parameter. If both ods and grv's are located within the same contentplaceholder, selecting a grv1 value causes ods2 to fire correctly. When the ods and the grv's aremoved to distinct contentplaceholders, firing fails.

Based on the procedure outlined in post: http://forums.asp.net/p/677362/677362.aspx#677362, I tried to substitute 'FormParameter' for 'SelectParameter' to no avail.

Is my code declaration wrong or are there restrictions on using a gridview as select source? (FormField also fails when ods1/grv1 is moved back to the same contentplaceholder.)

TIA for any hints.

1. SelectParameter definition for ods2 when the ods are located in different contentplaceholders with value of 'ctl00$CPL1$grv1' as found in the page html code (does not work):

<SelectParameters> <asp:FormParameter formfield="ctl00$CPL1$grv1" name="ProdCatID" type="Int32"> </asp:FormParameter> </SelectParameters>
2. SelectParameter definition when both ods are located within the same contentplaceholder (works ok):
<asp:ControlParameter controlid="grv1" name="ProdCatID" propertyname="SelectedValue" type="Int32"> </asp:ControlParameter>

Hi Msch,

Based on my understanding, you want to retrieve the data base on the GridView's DataKey value. The GridViews and DataSource controls are in different contentplaceholders. If I have misunderstood you, please feel free to let me know.

If we don't want to place them in the same contentplaceholder, we can bind GridView2 base on the DataKey value of GridView1 in the codebehind. For example:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">Content1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"DataKeyNames="ProductID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" /> <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> <asp:CommandField ButtonType="Button" ShowSelectButton="True" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetforumConnectionString%>" SelectCommand="SELECT [ProductID], [ProductName] FROM [Production]"></asp:SqlDataSource> <br /></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">Content2 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"> <Columns> <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" /> <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" /> <asp:BoundField DataField="SupplierName" HeaderText="SupplierName" SortExpression="SupplierName" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:aspnetforumConnectionString%>" SelectCommand="SELECT * FROM [Production] WHERE ([ProductID] = @.ProductID)" OnSelecting="SqlDataSource2_Selecting"> <SelectParameters> <asp:Parameter Name="ProductID" Type="String" /> </SelectParameters> </asp:SqlDataSource></asp:Content>

CodeBehind:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {//Executing Select method to bound the GridView2.GridView2.DataSource = SqlDataSource2.Select(DataSourceSelectArguments.Empty); GridView2.DataBind(); }protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e) {//Get the DataKey of GridView1 and then assign to the SqlDataSource's selectparameter.e.Command.Parameters[0].Value = GridView1.DataKeys[GridView1.SelectedIndex][0].ToString(); }


I hope this helps.

0 comments:

Post a Comment