从Gridview获取SQLDataSource更新参数

时间:2011-03-28 15:34:10

标签: asp.net vb.net webforms

我正在尝试从gridview获取更新的参数,但它是一个我不想显示的ID列。如果我在boundfield中显示数据,它可以正常工作但如果我将可见性设置为false,则不再将参数发送到更新存储过程。似乎没有可以放入gridview的隐藏字段列。

我试图通过后面的代码设置参数,但我不确定如何访问我希望以下代码不起作用的数据(它将参数设置为空

Protected Sub grvFacilityDisciplineBillingRate_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles grvFacilityDisciplineBillingRate.RowUpdating
    Dim row As GridViewRow = grvFacilityDisciplineBillingRate.Rows(e.RowIndex)
    sqlDisciplineBillingRate.UpdateParameters("Facility_ID").DefaultValue = CInt(row.DataItem("Facility_ID"))
    sqlDisciplineBillingRate.UpdateParameters("Discipline_ID").DefaultValue = CInt(row.DataItem("Discipline_ID"))
End Sub

这是显示两个ID列的前端,这不是我想要的

    <asp:SqlDataSource ID="sqlDisciplineBillingRate" runat="server" DataSourceMode="DataSet" SelectCommandType="StoredProcedure" SelectCommand="SP_Facility_DisciplineBillingRates" UpdateCommandType="StoredProcedure" UpdateCommand="SP_DiscplineBillingRate_Update" ConnectionString="<%$ ConnectionStrings:Trustaff_ESig2 %>">
        <SelectParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Facility_ID" DbType="Int32" />
            <asp:Parameter Name="Discipline_ID" DbType="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="grvFacilityDisciplineBillingRate" runat="server" DataSourceID="sqlDisciplineBillingRate" DataKeyNames="DisciplineBillingRate_ID"  AutoGenerateColumns="false" AllowSorting="true" AllowPaging="false" AutoGenerateEditButton="true"   CssClass="gridview">
        <EmptyDataTemplate>
            There were no discipline billing rates for this facility.
        </EmptyDataTemplate>
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Discipline" SortExpression="Name" ReadOnly="true" />
            <asp:BoundField DataField="BillingRate" HeaderText="Billing Rate" SortExpression="BillingRate" />
            <asp:BoundField DataField="Facility_ID" HeaderText="Facility_ID" SortExpression="Facility_ID" InsertVisible="false" />
            <asp:BoundField DataField="Discipline_ID" HeaderText="Discipline_ID" SortExpression="Discipline_ID" />
        </Columns>
    </asp:GridView>

2 个答案:

答案 0 :(得分:4)

您只需设置DataKeyNames,例如DataKeyNames =“Facility_ID,Discipline_ID”和SqlDataSource将在您使用GV的更新和删除功能时为您解决。

答案 1 :(得分:1)

尝试使用模板字段并使用标签控件并通过设置visible =“false”隐藏它们。

    <asp:TemplateField HeaderText="College">
       <ItemTemplate>
         <asp:Label ID="lbl_Title" runat="server" text='<%# Bind("Title") %>' visible="false"></asp:Label>
       </ItemTemplate>
   </asp:TemplateField>

使用以下代码访问代码隐藏中的值。

If gvrow.RowType = DataControlRowType.DataRow Then
     Dim label1 As Label = DirectCast(gvrow.FindControl("lbl_Title"), Label)
'Access text of label using label1.text and cast to int or string
End If

如果您不希望使用ID列,则可以使用模板化字段。并仍使用datakeynames属性

访问它