Tutorial :How to Pass parameter to SQlDataSource from gridview?


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?


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.


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>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »