how to read the values of the clicked buttons tow? - asp.net

I have a grid-view and in one column, I added buttons with every row that it is creating a button. What I need now is, if someone clicks on that button I need to get the field of that corresponding row. I have searched for the solution but i can't find out how to do it ?
*
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="brandname" HeaderText="brandname" SortExpression="brandname" />
<asp:BoundField DataField="info" HeaderText="info" SortExpression="info" />
<asp:ButtonField DataTextField="brandname" HeaderText="brandname" ButtonType="Button"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" DeleteCommand="DELETE FROM [brand_tbl] WHERE [Id] = #original_Id AND (([brandname] = #original_brandname) OR ([brandname] IS NULL AND #original_brandname IS NULL)) AND (([info] = #original_info) OR ([info] IS NULL AND #original_info IS NULL))" InsertCommand="INSERT INTO [brand_tbl] ([brandname], [info]) VALUES (#brandname, #info)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [brand_tbl]" UpdateCommand="UPDATE [brand_tbl] SET [brandname] = #brandname, [info] = #info WHERE [Id] = #original_Id AND (([brandname] = #original_brandname) OR ([brandname] IS NULL AND #original_brandname IS NULL)) AND (([info] = #original_info) OR ([info] IS NULL AND #original_info IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_brandname" Type="String" />
<asp:Parameter Name="original_info" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="brandname" Type="String" />
<asp:Parameter Name="info" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="brandname" Type="String" />
<asp:Parameter Name="info" Type="String" />
<asp:Parameter Name="original_Id" Type="Int32" />
<asp:Parameter Name="original_brandname" Type="String" />
<asp:Parameter Name="original_info" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
*

protected void gView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView rowView= (DataRowView)e.Row.DataItem;
if (rowView["ColumnId"] != DBNull.Value)
{
var val =rowView["ColumnId"];
}
}
// you can also do like this
void GridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
//Check if it's the right CommandName...
if(e.CommandName=="Add")
{
// do code
}
}

you can define DataKeyNames on GridView, follow this:
DataKeyNames in GridView

Related

How to make new command work in gridview asp.net

I have a gridview, in the gridview task, I choose Edit Column-> Selected Fields->Command Field.select insert button to True. And the New link field appear, and I check the HTML Source,
look like necessary codes are there, but when I click on the "New" link field, no response at all, but update is ok for me, delete I have not tested yet.
Below are codes for the gridview and sqldatasource :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="agreement_no,id" Font-Size="9pt" DataSourceID="SqlDataSource1" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True"></asp:CommandField>
<asp:BoundField DataField="agreement_no" HeaderText="agreement_no" ReadOnly="True" SortExpression="agreement_no"></asp:BoundField>
<asp:TemplateField HeaderText="status" SortExpression="status">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="status" DataValueField="status" SelectedValue='<%# Bind("status") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" SelectCommand="SELECT DISTINCT [status] FROM [deal_master]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sales_person" HeaderText="sales_person" SortExpression="sales_person"></asp:BoundField>
<asp:BoundField DataField="swap_carrier" HeaderText="swap_carrier" SortExpression="swap_carrier"></asp:BoundField>
<asp:BoundField DataField="start_pos" HeaderText="start_pos" SortExpression="start_pos"></asp:BoundField>
<asp:BoundField DataField="end_pos" HeaderText="end_pos" SortExpression="end_pos"></asp:BoundField>
<asp:BoundField DataField="swap_commitment" HeaderText="swap_commitment" SortExpression="swap_commitment"></asp:BoundField>
<asp:BoundField DataField="zone" HeaderText="zone" SortExpression="zone"></asp:BoundField>
<asp:BoundField DataField="target_minutes" HeaderText="target_minutes" SortExpression="target_minutes"></asp:BoundField>
<asp:BoundField DataField="target_sell_rate" HeaderText="target_sell_rate" SortExpression="target_sell_rate"></asp:BoundField>
<asp:BoundField DataField="target_buy_rate" HeaderText="target_buy_rate" SortExpression="target_buy_rate"></asp:BoundField>
<asp:BoundField ConvertEmptyStringToNull="false" DataField="supplier_interconnect" HeaderText="supplier_interconnect" SortExpression="supplier_interconnect"></asp:BoundField>
<asp:BoundField ConvertEmptyStringToNull="false" DataField="customer_interconnect" HeaderText="customer_interconnect" SortExpression="customer_interconnect"></asp:BoundField>
<asp:BoundField DataField="target_sales" HeaderText="target_sales" SortExpression="target_sales"></asp:BoundField>
<asp:BoundField DataField="target_cost" HeaderText="target_cost" SortExpression="target_cost"></asp:BoundField>
<asp:BoundField DataField="target_profit" HeaderText="target_profit" SortExpression="target_profit"></asp:BoundField>
<asp:BoundField DataField="lcr_zone" HeaderText="lcr_zone" SortExpression="lcr_zone"></asp:BoundField>
<asp:BoundField DataField="id" HeaderText="id" SortExpression="id" ReadOnly="True"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" DeleteCommand="DELETE FROM [deal_master] WHERE [agreement_no] = #agreement_no AND [id] = #id" InsertCommand="INSERT INTO [deal_master] ([agreement_no], [status], [sales_person], [swap_carrier], [start_pos], [end_pos], [swap_commitment], [zone], [target_minutes], [target_sell_rate], [target_buy_rate], [supplier_interconnect], [customer_interconnect], [target_sales], [target_cost], [target_profit], [lcr_zone], [id]) VALUES (#agreement_no, #status, #sales_person, #swap_carrier, #start_pos, #end_pos, #swap_commitment, #zone, #target_minutes, #target_sell_rate, #target_buy_rate, #supplier_interconnect, #customer_interconnect, #target_sales, #target_cost, #target_profit, #lcr_zone, #id)" SelectCommand="SELECT [agreement_no], [status], [sales_person], [swap_carrier], [start_pos], [end_pos], [swap_commitment], [zone], [target_minutes], [target_sell_rate], [target_buy_rate], [supplier_interconnect], [customer_interconnect], [target_sales], [target_cost], [target_profit], [lcr_zone], [id] FROM [deal_master]" UpdateCommand="UPDATE [deal_master] SET [status] = #status, [sales_person] = #sales_person, [swap_carrier] = #swap_carrier, [start_pos] = #start_pos, [end_pos] = #end_pos, [swap_commitment] = #swap_commitment, [zone] = #zone, [target_minutes] = #target_minutes, [target_sell_rate] = #target_sell_rate, [target_buy_rate] = #target_buy_rate, [supplier_interconnect] = #supplier_interconnect, [customer_interconnect] = #customer_interconnect, [target_sales] = #target_sales, [target_cost] = #target_cost, [target_profit] = #target_profit, [lcr_zone] = #lcr_zone WHERE [agreement_no] = #agreement_no AND [id] = #id">
<DeleteParameters>
<asp:Parameter Name="agreement_no" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="agreement_no" Type="String" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="sales_person" Type="String" />
<asp:Parameter Name="swap_carrier" Type="String" />
<asp:Parameter Name="start_pos" Type="Decimal" />
<asp:Parameter Name="end_pos" Type="Decimal" />
<asp:Parameter Name="swap_commitment" Type="String" />
<asp:Parameter Name="zone" Type="String" />
<asp:Parameter Name="target_minutes" Type="Decimal" />
<asp:Parameter Name="target_sell_rate" Type="Decimal" />
<asp:Parameter Name="target_buy_rate" Type="Decimal" />
<asp:Parameter Name="supplier_interconnect" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="false" Name="customer_interconnect" Type="String" />
<asp:Parameter Name="target_sales" Type="Decimal" />
<asp:Parameter Name="target_cost" Type="Decimal" />
<asp:Parameter Name="target_profit" Type="Decimal" />
<asp:Parameter Name="lcr_zone" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="sales_person" Type="String" />
<asp:Parameter Name="swap_carrier" Type="String" />
<asp:Parameter Name="start_pos" Type="Decimal" />
<asp:Parameter Name="end_pos" Type="Decimal" />
<asp:Parameter Name="swap_commitment" Type="String" />
<asp:Parameter Name="zone" Type="String" />
<asp:Parameter Name="target_minutes" Type="Decimal" />
<asp:Parameter Name="target_sell_rate" Type="Decimal" />
<asp:Parameter Name="target_buy_rate" Type="Decimal" />
<asp:Parameter ConvertEmptyStringToNull="false" Name="supplier_interconnect" Type="String" />
<asp:Parameter ConvertEmptyStringToNull="false" Name="customer_interconnect" Type="String" />
<asp:Parameter Name="target_sales" Type="Decimal" />
<asp:Parameter Name="target_cost" Type="Decimal" />
<asp:Parameter Name="target_profit" Type="Decimal" />
<asp:Parameter Name="lcr_zone" Type="String" />
<asp:Parameter Name="agreement_no" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:GPGConnectionString %>" SelectCommand="SELECT DISTINCT [agreement_no] FROM [deal_master] ORDER BY [agreement_no]"></asp:SqlDataSource>
<br />
<br />
<div>
</div>
enter image description here
my question is : what is the actions or setting I missed?
I want to know what steps I missed?
My objective is : I want to click on the "New" linkfield then 1 empty row appear, then I input the value in the new created row and I can save it or cancel it
If you look at the documentation for the GridView control, it says:
Inserting records into the data source is not directly supported by the GridView control. However, it is possible to insert records by using the GridView control in conjunction with the DetailsView or FormView control. For more information, see DetailsView or FormView, respectively.
Additionally, the CommandField documentation states about the ShowInsertButton property:
This property applies only to data-bound controls that support insert operations, such as the DetailsView control.
So, you'll need to create your own form separately, or leverage a DetailsView or FormView. Or better yet: ditch Web Forms altogether if you can, as it's a dead platform, as I explain on my blog.

detailed information for each entry in gridview

So i have made a Web Form with a dropdownlist which contains different classes in school and if you select one you get all the students of the selected class in the GridView below. So what i want to make now is, that if i click on a student it should open a more detailed page about the student, but i have no clues how to do this. I get all my data of a .mdf database file.
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="schueler.aspx.cs" Inherits="ASPXUebung.schueler" %>
<asp:DropDownList ID="selectklassen" runat="server" DataSourceID="SqlDataSource1klassenselect" DataTextField="K_Bez" DataValueField="K_ID" AutoPostBack="true"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1klassenselect" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [K_Bez], [K_ID] FROM [klassen] ORDER BY [K_Bez]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="S_SCHNR" DataSourceID="SqlDataSource1schueler">
<Columns>
<asp:BoundField DataField="S_SCHNR" HeaderText="S_SCHNR" ReadOnly="True" SortExpression="S_SCHNR" />
<asp:BoundField DataField="S_Name" HeaderText="S_Name" SortExpression="S_Name" />
<asp:BoundField DataField="S_Vorname" HeaderText="S_Vorname" SortExpression="S_Vorname" />
<asp:BoundField DataField="S_Gebdat" HeaderText="S_Gebdat" SortExpression="S_Gebdat" />
<asp:BoundField DataField="S_Adresse" HeaderText="S_Adresse" SortExpression="S_Adresse" />
<asp:BoundField DataField="S_K_Klasse" HeaderText="S_K_Klasse" SortExpression="S_K_Klasse" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1schueler" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [schueler] WHERE [S_SCHNR] = #original_S_SCHNR AND (([S_Name] = #original_S_Name) OR ([S_Name] IS NULL AND #original_S_Name IS NULL)) AND (([S_Vorname] = #original_S_Vorname) OR ([S_Vorname] IS NULL AND #original_S_Vorname IS NULL)) AND (([S_Gebdat] = #original_S_Gebdat) OR ([S_Gebdat] IS NULL AND #original_S_Gebdat IS NULL)) AND (([S_Adresse] = #original_S_Adresse) OR ([S_Adresse] IS NULL AND #original_S_Adresse IS NULL)) AND (([S_K_Klasse] = #original_S_K_Klasse) OR ([S_K_Klasse] IS NULL AND #original_S_K_Klasse IS NULL))" InsertCommand="INSERT INTO [schueler] ([S_SCHNR], [S_Name], [S_Vorname], [S_Gebdat], [S_Adresse], [S_K_Klasse]) VALUES (#S_SCHNR, #S_Name, #S_Vorname, #S_Gebdat, #S_Adresse, #S_K_Klasse)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [schueler] WHERE ([S_K_Klasse] = #S_K_Klasse)" UpdateCommand="UPDATE [schueler] SET [S_Name] = #S_Name, [S_Vorname] = #S_Vorname, [S_Gebdat] = #S_Gebdat, [S_Adresse] = #S_Adresse, [S_K_Klasse] = #S_K_Klasse WHERE [S_SCHNR] = #original_S_SCHNR AND (([S_Name] = #original_S_Name) OR ([S_Name] IS NULL AND #original_S_Name IS NULL)) AND (([S_Vorname] = #original_S_Vorname) OR ([S_Vorname] IS NULL AND #original_S_Vorname IS NULL)) AND (([S_Gebdat] = #original_S_Gebdat) OR ([S_Gebdat] IS NULL AND #original_S_Gebdat IS NULL)) AND (([S_Adresse] = #original_S_Adresse) OR ([S_Adresse] IS NULL AND #original_S_Adresse IS NULL)) AND (([S_K_Klasse] = #original_S_K_Klasse) OR ([S_K_Klasse] IS NULL AND #original_S_K_Klasse IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_S_SCHNR" Type="Int16" />
<asp:Parameter Name="original_S_Name" Type="String" />
<asp:Parameter Name="original_S_Vorname" Type="String" />
<asp:Parameter Name="original_S_Gebdat" Type="DateTime" />
<asp:Parameter Name="original_S_Adresse" Type="String" />
<asp:Parameter Name="original_S_K_Klasse" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="S_SCHNR" Type="Int16" />
<asp:Parameter Name="S_Name" Type="String" />
<asp:Parameter Name="S_Vorname" Type="String" />
<asp:Parameter Name="S_Gebdat" Type="DateTime" />
<asp:Parameter Name="S_Adresse" Type="String" />
<asp:Parameter Name="S_K_Klasse" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="selectklassen" Name="S_K_Klasse" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="S_Name" Type="String" />
<asp:Parameter Name="S_Vorname" Type="String" />
<asp:Parameter Name="S_Gebdat" Type="DateTime" />
<asp:Parameter Name="S_Adresse" Type="String" />
<asp:Parameter Name="S_K_Klasse" Type="String" />
<asp:Parameter Name="original_S_SCHNR" Type="Int16" />
<asp:Parameter Name="original_S_Name" Type="String" />
<asp:Parameter Name="original_S_Vorname" Type="String" />
<asp:Parameter Name="original_S_Gebdat" Type="DateTime" />
<asp:Parameter Name="original_S_Adresse" Type="String" />
<asp:Parameter Name="original_S_K_Klasse" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
Make the student name column a Hyperlink column like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="S_SCHNR" DataSourceID="SqlDataSource1schueler">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="S_SCHNR"
DataNavigateUrlFormatString="StudentInfo.aspx?id={0}
DataTextField="S_Name" "/>
<asp:BoundField DataField="S_SCHNR" HeaderText="S_SCHNR" ReadOnly="True" SortExpression="S_SCHNR" />
<asp:BoundField DataField="S_Name" HeaderText="S_Name" SortExpression="S_Name" />
<asp:BoundField DataField="S_Vorname" HeaderText="S_Vorname" SortExpression="S_Vorname" />
<asp:BoundField DataField="S_Gebdat" HeaderText="S_Gebdat" SortExpression="S_Gebdat" />
<asp:BoundField DataField="S_Adresse" HeaderText="S_Adresse" SortExpression="S_Adresse" />
<asp:BoundField DataField="S_K_Klasse" HeaderText="S_K_Klasse" SortExpression="S_K_Klasse" />
</Columns>
</asp:GridView>
Make another web page called StudentInfo.aspx. On that page, display the data using a DetailsView control, a SqlDataSource control and the QueryString parameter. Here's an example of how to do that: http://msdn.microsoft.com/en-us/library/xt50s8kz(v=vs.100).aspx
A more convenient way to do this without creating a new page would be to populate the data into a Bootstrap modal like this. It uses UpdatePanel, which may or may not be something you want to use though.
Add the Detail Event to the Gridview.
Create a div for the Bootstrap modal with a FormView
Call the jQuery from the C# code behind to populate the data in the FormView with the ID from the GridView.

DetailsView in Modalpopup won't load on second click on edit button

I have a GridView that when you click the edit button it loads a DetailsView in Edit Mode inside of a ModalPopup. It works fine the first time you click edit. If you click edit again to make changes to a second record, it loads but the DetailsView is not in the ModalPopup.
Can anyone tell me what I"m doing wrong? My code is below.
GridView datasource:
<asp:SqlDataSource ID="sdsMembers" runat="server"
ConnectionString="<%$ ConnectionStrings:DoseRec_ABTConnectionString %>"
SelectCommand="SELECT [intMemberID], [vcharTitle], [vcharFirstName], [vcharLastName], [vcharSuffix], [vcharJobTitle], [vcharAddress1], [vcharAddress2], [vcharCity], [vcharState], [vcharZipCode], [vcharPhone], [vcharFax], [bitActive] FROM [tbl_Members]"
DeleteCommand="DELETE FROM [tbl_Members] WHERE [intMemberID] = #intMemberID"
InsertCommand="INSERT INTO [tbl_Members] ([vcharTitle], [vcharFirstName], [vcharLastName], [vcharSuffix], [vcharJobTitle], [vcharAddress1], [vcharAddress2], [vcharCity], [vcharState], [vcharZipCode], [vcharPhone], [vcharFax], [bitActive]) VALUES (#vcharTitle, #vcharFirstName, #vcharLastName, #vcharSuffix, #vcharJobTitle, #vcharAddress1, #vcharAddress2, #vcharCity, #vcharState, #vcharZipCode, #vcharPhone, #vcharFax, #bitActive)"
UpdateCommand="UPDATE [tbl_Members] SET [vcharTitle] = #vcharTitle, [vcharFirstName] = #vcharFirstName, [vcharLastName] = #vcharLastName, [vcharSuffix] = #vcharSuffix, [vcharJobTitle] = #vcharJobTitle, [vcharAddress1] = #vcharAddress1, [vcharAddress2] = #vcharAddress2, [vcharCity] = #vcharCity, [vcharState] = #vcharState, [vcharZipCode] = #vcharZipCode, [vcharPhone] = #vcharPhone, [vcharFax] = #vcharFax, [bitActive] = #bitActive WHERE [intMemberID] = #intMemberID">
<DeleteParameters>
<asp:Parameter Name="intMemberID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="vcharTitle" Type="String" />
<asp:Parameter Name="vcharFirstName" Type="String" />
<asp:Parameter Name="vcharLastName" Type="String" />
<asp:Parameter Name="vcharSuffix" Type="String" />
<asp:Parameter Name="vcharJobTitle" Type="String" />
<asp:Parameter Name="vcharAddress1" Type="String" />
<asp:Parameter Name="vcharAddress2" Type="String" />
<asp:Parameter Name="vcharCity" Type="String" />
<asp:Parameter Name="vcharState" Type="String" />
<asp:Parameter Name="vcharZipCode" Type="String" />
<asp:Parameter Name="vcharPhone" Type="String" />
<asp:Parameter Name="vcharFax" Type="String" />
<asp:Parameter Name="bitActive" Type="Boolean" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="vcharTitle" Type="String" />
<asp:Parameter Name="vcharFirstName" Type="String" />
<asp:Parameter Name="vcharLastName" Type="String" />
<asp:Parameter Name="vcharSuffix" Type="String" />
<asp:Parameter Name="vcharJobTitle" Type="String" />
<asp:Parameter Name="vcharAddress1" Type="String" />
<asp:Parameter Name="vcharAddress2" Type="String" />
<asp:Parameter Name="vcharCity" Type="String" />
<asp:Parameter Name="vcharState" Type="String" />
<asp:Parameter Name="vcharZipCode" Type="String" />
<asp:Parameter Name="vcharPhone" Type="String" />
<asp:Parameter Name="vcharFax" Type="String" />
<asp:Parameter Name="bitActive" Type="Boolean" />
<asp:Parameter Name="intMemberID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
DetailsView datasource:
<asp:SqlDataSource ID="sdsMembersDetail" runat="server"
ConnectionString="<%$ ConnectionStrings:DoseRec_ABTConnectionString %>"
SelectCommand="SELECT [intMemberID] AS ID, [vcharTitle] AS Title, [vcharFirstName] AS 'First Name', [vcharLastName] AS 'Last Name', [vcharSuffix] AS Suffix, [vcharJobTitle] AS 'Job Title', [vcharAddress1] AS Address1, [vcharAddress2] AS Address2, [vcharCity] AS City, [vcharState] AS State, [vcharZipCode] AS 'Zip Code', [vcharPhone] AS Phone, [vcharFax] AS Fax, [bitActive] AS Active FROM [tbl_Members] WHERE ([intMemberID] = #intMemberID)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="intMemberID"
QueryStringField="intMemberID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
GridView markup:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvMembers" runat="server" AutoGenerateColumns="False"
DataKeyNames="intMemberID" DataSourceID="sdsMembers" style="background-color:White;">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField ControlStyle-Width="50px" HeaderStyle-Width="60px">
<ItemTemplate>
<asp:LinkButton ID="btnViewDetails" runat="server" OnClick="BtnViewDetails_Click">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="intMemberID" HeaderText="ID"
InsertVisible="False" ReadOnly="True" SortExpression="intMemberID" />
<asp:BoundField DataField="vcharTitle" HeaderText="vcharTitle"
SortExpression="vcharTitle" />
<asp:BoundField DataField="vcharFirstName" HeaderText="First Name"
SortExpression="vcharFirstName" />
<asp:BoundField DataField="vcharLastName" HeaderText="Last Name"
SortExpression="vcharLastName" />
<asp:BoundField DataField="vcharSuffix" HeaderText="Suffix"
SortExpression="vcharSuffix" />
<asp:BoundField DataField="vcharJobTitle" HeaderText="Job Title"
SortExpression="vcharJobTitle" />
<asp:BoundField DataField="vcharAddress1" HeaderText="Address1"
SortExpression="vcharAddress1" />
<asp:BoundField DataField="vcharAddress2" HeaderText="Address2"
SortExpression="vcharAddress2" />
<asp:BoundField DataField="vcharCity" HeaderText="City"
SortExpression="vcharCity" />
<asp:BoundField DataField="vcharState" HeaderText="State"
SortExpression="vcharState" />
<asp:BoundField DataField="vcharZipCode" HeaderText="Zip Code"
SortExpression="vcharZipCode" />
<asp:BoundField DataField="vcharPhone" HeaderText="Phone"
SortExpression="vcharPhone" />
<asp:BoundField DataField="vcharFax" HeaderText="Fax"
SortExpression="vcharFax" />
<asp:CheckBoxField DataField="bitActive" HeaderText="Active"
SortExpression="bitActive" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
DetailsView marup:
<asp:Button id="btnShowPopup" runat="server" style="display:none" />
<ajaxToolKit:ModalPopupExtender
ID="mdlPopup" runat="server" TargetControlID="btnShowPopup" PopupControlID="pnlPopup"
CancelControlID="btnClose" BackgroundCssClass="modalBackground" />
<asp:Panel ID="pnlPopup" runat="server" Width="500px" style="display:none">
<asp:UpdatePanel ID="updPnlCustomerDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblMemberDetail" runat="server" Text="Member Detail" BackColor="lightblue" Width="95%" />
<asp:DetailsView ID="dvMemberDetail" runat="server" DefaultMode="Edit" Width="95%" BackColor="white" OnItemUpdating="dvMembersDetail_ItemUpdating">
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
<div align="right" style="width:95%">
<asp:LinkButton
ID="btnSave" runat="server" Text="Save"
Width="50px" onclick="btnSave_Click" />
<asp:LinkButton ID="btnClose" runat="server">Close</asp:LinkButton>
</div>
</asp:Panel>
Click event (to display the DetailsView):
protected void BtnViewDetails_Click(object sender, EventArgs e)
{
LinkButton btnDetails = sender as LinkButton;
GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
this.sdsMembersDetail.SelectParameters.Clear();
this.sdsMembersDetail.SelectParameters.Add("intMemberID", Convert.ToString(this.gvMembers.DataKeys[row.RowIndex].Value));
this.dvMemberDetail.DataSource = this.sdsMembersDetail;
this.dvMemberDetail.DataBind();
this.updPnlCustomerDetail.Update();
this.mdlPopup.Show();
}
If any other code, like the code for my save button is needed just let me know and I will post it.
Here's my Save code:
protected void btnSave_Click(object sender, EventArgs e)
{
if (this.Page.IsValid)
{
this.dvMemberDetail.UpdateItem(true);
this.dvMemberDetail.ChangeMode(DetailsViewMode.ReadOnly);
this.dvMemberDetail.Visible = false;
this.mdlPopup.Hide();
this.gvMembers.DataBind();
this.UpdatePanel1.Update();
}
}
protected void dvMembersDetail_ItemUpdating(object sender, EventArgs e)
{
using (DoseRec_ABTEntities1 dbContext = new DoseRec_ABTEntities1())
{
TextBox id = (TextBox)dvMemberDetail.Rows[0].Cells[1].Controls[0];
int intID = Convert.ToInt32(id.Text);
tbl_Members mem = (from m in dbContext.tbl_Members
where m.intMemberID == intID
select m).Single();
TextBox title = (TextBox)dvMemberDetail.Rows[1].Cells[1].Controls[0];
TextBox firstname = (TextBox)dvMemberDetail.Rows[2].Cells[1].Controls[0];
TextBox lastname = (TextBox)dvMemberDetail.Rows[3].Cells[1].Controls[0];
TextBox suffix = (TextBox)dvMemberDetail.Rows[4].Cells[1].Controls[0];
TextBox jobtitle = (TextBox)dvMemberDetail.Rows[5].Cells[1].Controls[0];
TextBox address1 = (TextBox)dvMemberDetail.Rows[6].Cells[1].Controls[0];
TextBox address2 = (TextBox)dvMemberDetail.Rows[7].Cells[1].Controls[0];
TextBox city = (TextBox)dvMemberDetail.Rows[8].Cells[1].Controls[0];
TextBox state = (TextBox)dvMemberDetail.Rows[9].Cells[1].Controls[0];
TextBox zipcode = (TextBox)dvMemberDetail.Rows[10].Cells[1].Controls[0];
TextBox phone = (TextBox)dvMemberDetail.Rows[11].Cells[1].Controls[0];
TextBox fax = (TextBox)dvMemberDetail.Rows[12].Cells[1].Controls[0];
CheckBox active = (CheckBox)dvMemberDetail.Rows[13].Cells[1].Controls[0];
mem.vcharTitle = title.Text;
mem.vcharFirstName = firstname.Text;
mem.vcharLastName = lastname.Text;
mem.vcharSuffix = suffix.Text;
mem.vcharJobTitle = jobtitle.Text;
mem.vcharAddress1 = address1.Text;
mem.vcharAddress2 = address2.Text;
mem.vcharCity = city.Text;
mem.vcharState = state.Text;
mem.vcharZipCode = zipcode.Text;
mem.vcharPhone = phone.Text;
mem.vcharFax = fax.Text;
mem.bitActive = active.Checked;
dbContext.SaveChanges();
}
}

Deleting row from Repeater

I have the following Repeater on my page:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<div id="fullcommentheader">
<span class="fullname">
<asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Name") %>'></asp:Literal></span>
<br />
<span class="fullbodytext">
<asp:Button ID="Button2" runat="server" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' Text="Delete" />
<asp:Literal ID="LitBody2" Text='<%# Eval("Message")%>' runat="server"></asp:Literal></span>
<span class="dateTime">
<asp:Literal ID="Literal4" runat="server" Text='<%# Eval("CreateDateTime") %>'></asp:Literal></span>
</div>
<br />
</ItemTemplate>
</asp:Repeater>
I have a Button2 there that I'd like to use to delete the repeater entry, how do I query the database to achieve this? I'm used to have these commands by default on gridview, so I'm unsure on how to do this manually.
My event:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete" && e.CommandArgument.ToString() != "")
{
}
}
This is my SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>"
DeleteCommand="DELETE FROM [Comment] WHERE [Id] = #Id"
InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Message], [PostId]) VALUES (#Name, #Email, #Website, #Message, #PostId)"
SelectCommand="SELECT [Name], [Email], [Website], [Message], [PostId], [Id], [CreateDateTime] FROM [Comment] WHERE ([PostId] = #PostId)"
UpdateCommand="UPDATE [Comment] SET [Name] = #Name, [Email] = #Email, [Website] = #Website, [Message] = #Message, [PostId] = #PostId, [CreateDateTime] = #CreateDateTime WHERE [Id] = #Id">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Website" Type="String" />
<asp:Parameter Name="Message" Type="String" />
<asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
<asp:Parameter Name="CreateDateTime" Type="DateTime" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Website" Type="String" />
<asp:Parameter Name="Message" Type="String" />
<asp:Parameter Name="PostId" Type="Int32" />
<asp:Parameter Name="CreateDateTime" Type="DateTime" />
<asp:Parameter Name="Id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
You will need to declare a private int _id global variable first.
Then, add the parameter dynamically in your sqldatasource deleting event:
Then in your ItemCommand 'delete', set the global _id to the CommandArgument since you passed that in. Then perform a SqlDataSource1.Delete()
_id = Convert.ToInt32(e.CommandArgument);
SqlDataSource1.Delete();
_id = 0;
//need to rebind your repeater here or you won't see the changes
protected void SqlDataSource1_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
//add the parameter
if (_id != 0)
e.Command.Parameters["#Id"].Value = _id;
}
Use the ItemCommand event for the repeater. This will trigger when the button is clicked and give you access to the command name and command argument. You can then use the command argument to delete the record from the database.
Do delete the record, you can use the SqlDataSource.Delete() method, but you will need to populate the delete parameter before calling that method or handle the Deleting event and populate the parameters there.

shopping cart total price issue

I am working on a shopping cart project for my college project in final page of my cart i want to calculate the total amount for the all the product in the cart help me in that code for that is
for cart.aspx
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="id"
DataSourceID="SqlDataSource1"
EmptyDataText="No Item in the Cart">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="pName"
HeaderText="pName"
SortExpression="pName" />
<asp:BoundField DataField="brand"
HeaderText="brand"
SortExpression="brand" />
<asp:TemplateField HeaderText="img"
SortExpression="img">
<EditItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("img") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1"
runat="server"
ImageUrl='<%# Bind("img") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="quantity"
HeaderText="quantity"
SortExpression="quantity" />
<asp:BoundField DataField="price"
HeaderText="price"
SortExpression="price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<% $ConnectionStrings:shopingConnectionString1 %>"
DeleteCommand="DELETE FROM [completeCart] WHERE [id] = #id"
InsertCommand="INSERT INTO [completeCart] ([uName], [pName], [brand], [img], [quantity], [price]) VALUES (#uName, #pName, #brand, #img, #quantity, #price)"
SelectCommand="SELECT * FROM [completeCart] WHERE ([uName] = #uName)"
UpdateCommand="UPDATE [completeCart] SET [uName] = #uName, [pName] = #pName, [brand] = #brand, [img] = #img, [quantity] = #quantity, [price] = #price WHERE [id] = #id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int64" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
</InsertParameters>
<SelectParameters>
<asp:CookieParameter CookieName="uname"
Name="uName"
Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="uName" Type="String" />
<asp:Parameter Name="pName" Type="String" />
<asp:Parameter Name="brand" Type="String" />
<asp:Parameter Name="img" Type="String" />
<asp:Parameter Name="quantity" Type="Int32" />
<asp:Parameter Name="price" Type="Int64" />
<asp:Parameter Name="id" Type="Int64" />
</UpdateParameters>
</asp:SqlDataSource>
for code behind file
protected void Page_Load(object sender, EventArgs e)
{
string s2 = System.Web.HttpContext.Current.User.Identity.Name;
Response.Cookies["uname"].Value = s2;
}
finally the result i want is the total sum of cost of product display on a label control on the same page
Remove the SqlDataSource control. Don't put SQL in aspx page! Create a class that returns cart and calculates grand total. Databind in code behind.
I am thinking of that your cart.aspx page has a grid view for displaying the cart items and prices , then you can do like this....
you can do like this ... Simply in GridView.RowDataBound Event loop gridview and find control contain price amount and sum them
decimal grdTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "price"));
grdTotal = grdTotal + rowTotal;
}
lbl.Text = grdTotal.ToString("c");
}
}
you can display total price value at the footer of price column .......

Resources