1

QUESTION

If you have a gridview inside a repeater, how do you pass a primary key ID to the gridview where the primary key relates to the repeaters item template row?

I am currently attempting to do this by using a hiddenfield that contains the PK and a control parameter to detect it and bind it to the gridview.

CURRENT ERROR

Could not find control 'hidPK' in ControlParameter 'PK'.

ABBREVIATED CODE

<asp:Repeater ID="rpt" RunAt="Server">
  <ItemTemplate>
    <asp:HiddenField ID="hidPK" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "PK") %>'/>

    <asp:GridView ID="GV" DataSourceID="sqlSource"></asp:GridView>

  </ItemTemplate>
</asp:Repeater>

<asp:SqlDataSource ID="sqlSource" RunAt="Server" SelectCommand="spPopulateGridview" SelectCommandType="StoredProcedure">
  <SelectParameters>
    <asp:ControlParameter Type="Int32" Name="PK" DefaultValue="0" ControlID="hidPK"/>
  </SelectParameters>
</asp:SqlDataSource>

EDITS

Protected Sub rpt_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem  Then
        Dim gv As GridView = TryCast(e.Item.FindControl("GV"), GridView)    
        gv.DataSource = ds
        gv.DataBind()
    End If
End Sub
DreamTeK
  • 32,537
  • 27
  • 112
  • 171

1 Answers1

1

What you can try is moving the sqldatasource in the repeater

<asp:Repeater ID="rpt" RunAt="Server">
  <ItemTemplate>
    <asp:HiddenField ID="hidPK" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "PK") %>'/>

    <asp:GridView DataSourceID="sqlSource"></asp:GridView>
    <asp:SqlDataSource ID="sqlSource" RunAt="Server" SelectCommand="spPopulateGridview" SelectCommandType="StoredProcedure">
      <SelectParameters>
         <asp:ControlParameter Type="Int32" Name="PK" DefaultValue="0" ControlID="hidPK"/>
      </SelectParameters>
    </asp:SqlDataSource>
  </ItemTemplate>
</asp:Repeater>
Hans Derks
  • 1,027
  • 7
  • 7
  • Thanks Hans, I did consider this as a reason for the error however moving it into the repeater causes the data source to be considered undeclared. "Error: 'sqlSource' is not declared". I suspect because it can no longer find the sqldatasource inside the repeater. Any Ideas? – DreamTeK Oct 02 '13 at 11:50
  • How did you add it, within the ItemTemplate? I tried it with testdata and it seemed to work – Hans Derks Oct 02 '13 at 12:27
  • Yes as above. With sqlSource declared in code behind on page load. Would you agree this is the way to get the unique value anyway? Can PKID not be bound to repeater control directly? – DreamTeK Oct 02 '13 at 13:15
  • Do you need the sqlSource in codebehind? There are now multiple sqlSources and if you need the sqlSource you must use FindControl in ItemDataBound event. something like: GridView x= e.Item.FindControl("gridviewid") as GridView; SqlDataSource y= x.DataSource as SqlDataSource; – Hans Derks Oct 02 '13 at 13:31
  • Appreciate your help on this Hans. This is what I am trying to do at the moment. See my edits above. I cant get the correct syntax. – DreamTeK Oct 02 '13 at 13:50
  • I added code to get the sqlDatasource but I think this is not necessary because the grid is already populated with data (at least with my testdata) – Hans Derks Oct 03 '13 at 04:51
  • I just took your advise and bound the datasource of the page instead of code behind. Problem solved. Many thanks for the time you invested. – DreamTeK Oct 03 '13 at 08:10