如何将ID从转发器行传递到嵌套网格视图?

时间:2013-10-02 10:01:12

标签: asp.net vb.net gridview repeater controlparameter

问题

如果在转发器中有一个gridview,如何将主键ID传递给主键与转发器项模板行相关的gridview?

我目前正尝试使用包含PK的隐藏字段和控制参数来检测它并将其绑定到gridview。

当前错误

无法在ControlParameter'PK'中找到控件'hidPK'。

缩写代码

<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

1 个答案:

答案 0 :(得分:1)

您可以尝试在转发器中移动sqldatasource

<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>