如何从gridview向SQlDataSource传递参数?

发布于 2024-08-27 15:35:10 字数 568 浏览 8 评论 0原文

Gridview 有许多列和一个删除按钮。删除按钮是一个控件,如 TemplateField

<asp:TemplateField>
 <ItemTemplate>
  <asp:LinkButton ID="btnDelete" CommandName="Delete" Text='<%#     Eval("disabled").ToString()=="False" ? "Disabled" : "Enabled" %>'
 OnClientClick="return confirm('Are you sure you want to take this action?');"
 runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

现在关联 SQLDataSource 的删除命令的存储过程需要两个参数。一个来自 DataKeyNames (RowID),另一个我想传递的是 btnDelete 的文本(True 或 False)。

我怎样才能实现它?

Gridview has many columns and a Delete button as well. Delete button is a control as TemplateField

<asp:TemplateField>
 <ItemTemplate>
  <asp:LinkButton ID="btnDelete" CommandName="Delete" Text='<%#     Eval("disabled").ToString()=="False" ? "Disabled" : "Enabled" %>'
 OnClientClick="return confirm('Are you sure you want to take this action?');"
 runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Now associated SQLDataSource's Delete command's stored procedure is expecting two parameters. One is going from DataKeyNames (RowID), other one i wanna pass is the Text of btnDelete (True or False).

How can i achieve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2024-09-03 15:35:13

我建议在后面的代码中执行此操作。单击 btnDelete 时,我将迭代 gridview 中的每一行并检查所有数据键名。一旦我找到您要删除的那个,您将需要将其发送回您的数据库。您可以使用 linq、ado.net 等 orm,也可以使用直接的 sqlcmd。

I would recommend doing it in the code behind. On btnDelete click i would iterate through every row in your gridview and check all the datakeynames. Once i found the one you want to delete you will need to send that back to you db. You can use either an orm like linq, ado.net, or a straight sqlcmd.

铜锣湾横着走 2024-09-03 15:35:13

请参阅此代码。我实施并且工作正常

<asp:TemplateField HeaderText="Add" HeaderStyle-CssClass="grid_Title">
    <ItemTemplate>
        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("TrackID")%>' />
        <asp:SqlDataSource ID="SqlDataSource_Projects" runat="server" ConnectionString="<%$ ConnectionStrings:SentricMusicFunctionalityConnectionString2 %>"
                           SelectCommand="select* from MassTraxCatProjects where Fk_AgencyId=@CatalogAgencyId and fk_trackid=@TrackID) ">
            <SelectParameters>
                <asp:SessionParameter DbType="Int32" DefaultValue="CatalogAgencyId" Name="CatalogAgencyId"
                                      SessionField="CatalogAgencyId" />
                <asp:ControlParameter DefaultValue="TrackID" Name="TrackID" ControlID="**HiddenField1**" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:DropDownList ID="ddl_ProjectType" runat="server" DataSourceID="SqlDataSource_Projects"
                          Width="100px" DataTextField="ProjectName" DataValueField="ProjectId">
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>

Please See this Code. I implement and works fine

<asp:TemplateField HeaderText="Add" HeaderStyle-CssClass="grid_Title">
    <ItemTemplate>
        <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("TrackID")%>' />
        <asp:SqlDataSource ID="SqlDataSource_Projects" runat="server" ConnectionString="<%$ ConnectionStrings:SentricMusicFunctionalityConnectionString2 %>"
                           SelectCommand="select* from MassTraxCatProjects where Fk_AgencyId=@CatalogAgencyId and fk_trackid=@TrackID) ">
            <SelectParameters>
                <asp:SessionParameter DbType="Int32" DefaultValue="CatalogAgencyId" Name="CatalogAgencyId"
                                      SessionField="CatalogAgencyId" />
                <asp:ControlParameter DefaultValue="TrackID" Name="TrackID" ControlID="**HiddenField1**" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:DropDownList ID="ddl_ProjectType" runat="server" DataSourceID="SqlDataSource_Projects"
                          Width="100px" DataTextField="ProjectName" DataValueField="ProjectId">
        </asp:DropDownList>
    </ItemTemplate>
</asp:TemplateField>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文