How to Pass parameter to SQlDataSource from gridview? - c#

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>

Related

One button for CheckBox items in ItemTemplate

How can i put only one button for ChceckBox?
here is what i get:
enter image here
As you can see i have 10 button for each checkbox value but i need group them by "week" "Day" and in this case put only one for each of them. That "day weak" label come from sql and is dynamic value.
Here is code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PS_TestConnectionString %>" SelectCommand="SELECT * FROM [Prize] ">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("Prize") %>' OnCheckedChanged="CheckBox1_CheckedChanged1" AutoPostBack="True" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /><br /> <asp:Button ID="Button1" runat="server" Text="Receive the prize" Visible="True" CommandName="foo" /></ItemTemplate>
I need this witch CheckBox in itemtemplate becouse i have to put image in
I solved this problem by adding a new GridView to ItemTemplate

Adding Blank Rows to GridView bound to SqlDataSource

I am creating a GridView that will initially have a blank row (consisting of 2 DropDownLists and one TextBox). There is a button in the footer that will allow a user to add another row after they have filled in the first. I am binding this to a SqlDataSource so I am able to use the InsertCommand to call a stored procedure to Insert the user's data into a database table. However, I am not sure how to go about adding an empty row. Right now, the headers will not show up until a postback call is executed. I have seen tutorials that use a DataTable to add an empty row, but I could not get this to work with the bound DropDownLists. Is there a way to add a row to the SqlDataSource from code behind? The following is the relevant part of my ASP page:
<table>
<tr>
<td colspan="2" style="width:100%">
<div>
<asp:UpdatePanel ID="upUpdateChecklist" runat="server">
<ContentTemplate>
<asp:GridView ID="gvUpdate" runat="server" AutoGenerateColumns="False"
CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
ShowHeaderWhenEmpty="true"
Visible="true" CellSpacing="2"
CellPadding="2" ShowFooter="true"
DataSourceID="dsUpdate">
<Columns>
<asp:TemplateField HeaderText="Division/Context">
<ItemTemplate>
<asp:DropDownList ID="ddDivision" runat="server" DataTextField="Division" DataValueField="ID" DataSourceID="dsDivision"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application">
<ItemTemplate>
<asp:DropDownList ID="ddApplication" runat="server" DataTextField="Application" DataValueField="ID" DataSourceID="dsApplication" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:TextBox ID="tbTask" runat="server" ReadOnly="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAdd" Text="Add New Row" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsDivision" SelectCommand="SELECT ID, Division FROM Automation.dbo.Division ORDER BY Application, Division"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"
SelectCommand="">
<InsertParameters>
<asp:Parameter Name="Division" DbType="Int32" />
<asp:Parameter Name="Application" DbType="Int32" />
<asp:Parameter Name="Task" DbType="String" />
</InsertParameters>
</asp:SqlDataSource>
I am not completely sure I understand how do you intend to achieve what you want but if you want to generate an empty row, change the select command of the SQL data source to do a union with an empty dummy row.
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications UNION select -1 as ID, '' as ApplicationName "

How To change Command Text and ImageButton of ItemTemplate in TemplateField

I have a column that has an ImageButton. my database field has bit data type. I want when my record has true value in that column show True.jpg and my command become MakeFalse and when it has false value show False.jpg and my command become MakeTrue. How I can do this?Is it possible to do it with one TemplateField?
thanks
You could include two ImageButtons in a TemplateField and evaluate Visible from your bit_field
<asp:TemplateField HeaderText="YourField">
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="True.jpg" Visible='<%# (bool)Eval("bit_field") %>' />
<asp:ImageButton runat="server" ImageUrl="False.jpg" Visible='<%# !(bool)Eval("bit_field") %>' />
</ItemTemplate>
</asp:TemplateField>
I'm not sure how you'd want your Command to tie in.
This is the remaining part of the above Brissles code
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text='<%#(bool)Eval("bit_field")? "Make False":"Make True" %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

How to make a checkbox checked in a gridview using DataBinder.Eval

I am trying to make a checkbox checked if the value is 1 or 0 basically in my database i have
a field called Active (bit, not null) and i can pass the value to the gridview.. but now i am trying to make it checked if the bit is 1 or not checked if the bit is 0 but its not working.. it just shows unchecked but the bit is 1.
<ItemTemplate>
<asp:CheckBox ID="ItemCheck" runat="server"
Enabled='<%# (DataBinder.Eval(Container.DataItem, "Active")) %>' />
</ItemTemplate>
Any help would be much appreciated
Give this a shot:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Convert.ToBoolean(Eval("Active"))%>' .. />
You can probably do it this way too:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#((bool)Eval("Active"))%>' .. />
You can use a CheckBoxField which will do this for you automatically and is a default subcontrol of a GridView
<asp:GridView ......>
<Columns>
<asp:CheckBoxField DataField="Active" SortExpression="Active" />
</Columns>
</asp:GridView>
This is all a matter of style but I prefer to use a RadioButtonList since it's usually more intuitive for a user
<asp:TemplateField ....>
<ItemTemplate>
<asp:RadioButtonList ID="rblActive" runat="server"
SelectedValue='<%# Bind("Active") %>'
RepeatDirection="Horizontal">
<asp:ListItem Value="1">Enabled</asp:ListItem>
<asp:ListItem Value="0">Disabled</asp:ListItem>
</asp:RadioButtonList>
<ItemTemplate>
</asp:TemplateField>

Conformation window on delete in gridview?

how to make a conformation on boundfield in gridview in delete ....event
As far as I understand you want to add confirmation on deleting in your grid, correct?
Here is a simple example of grid:
<asp:GridView runat="server"
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton runat="server"
CommandName="Delete"
OnClientClick='return confirm("Are you sure?");'
Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
<!-- your bound fields here -->
</Columns>
</asp:GridView>
The trick is in adding client-side confirm to the OnClientClick property of the delete button.
Can you do something like this?
<asp:linkbutton id="btnDelete" runat="server" commandname="Delete" onclientclick="return confirm('Are you sure you want to delete this item?');"
text="Delete" />

Categories

Resources