How to refresh gridview after insertion in asp.net c# - c#

I am using gridview in a page of website that I am builing with asp.net so I am using sqldatasource component to fill gridview.now I insert new data into the table and page is refreshed but new data is not displayed in the gridview how to solve this problem

Bind your gridview again after inserting new data. It will take time but you have to fetch the data from database and bind it again to gridview.
Gridview.DataSource = yourDataSource;
Gridview.DataBind();

Bind the Grid in page_Init()
It bind the data from your database Every page init

Documentation on inserting using SqlDataSource - SqlDataSource.Insert Method()
And here's a complete, wroking example:
Code behind:
public partial class SqlDataSourceExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
SqlDataSource1.Insert();
}
}
.ASPX:
<form id="form1" runat="server">
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Surname] FROM [Person]"
InsertCommand="INSERT INTO [Person](Name,Surname)VALUES(#name,#surname)">
<InsertParameters>
<asp:ControlParameter ControlID="txtName" Name="name" />
<asp:ControlParameter ControlID="txtSurname" Name="surname" />
</InsertParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" />
</Columns>
</asp:GridView>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Label ID="lblSurname" runat="server" Text="Surname"></asp:Label>
<asp:TextBox ID="txtSurname" runat="server"></asp:TextBox><br />
<asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</form>

Related

How to take ID of a row in gridview when you press a button in a row

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="IDProject" DataSourceID="SqlDataSource1" GridLines="Vertical" style="margin-top:2rem" Width="1056px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField SortExpression="ProjectLeader" HeaderText="Project Leader">
<ItemTemplate>
<asp:Label Text='<%# Bind("ProjectLeader") %>' ID="lvProjectLeader" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" SortExpression="ProjectName" />
<asp:TemplateField HeaderText="Start date:" SortExpression="StartDate">
<ItemTemplate>
<asp:Label Text='<%# Bind("StartDate") %>' ID="lbStartDate" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Client Name" SortExpression="Client name">
<ItemTemplate>
<asp:Label Text='<%# Bind("Client name") %>' ID="lbClientName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employees">
<ItemTemplate>
<asp:Button Text="Open" ID="EmployeesOpen" OnClick="EmployeesOpen_Click" runat="server" CssClass="btn btn-outline-primary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="Insert into Project values(#ProjectName, #ClientID,#ProjectLeader,#StartDate)"
SelectCommand="select IDProject, ProjectLeader, ProjectName, StartDate, ClientID, Client.Name from Project inner join Client on IDClient = ClientID"
UpdateCommand="Update Project set ProjectName = #ProjectName where IDProject = #IDProject">
<InsertParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="ClientID" />
<asp:Parameter Name="ProjectLeader" />
<asp:Parameter Name="StartDate" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProjectName" />
<asp:Parameter Name="IDProject" />
</UpdateParameters>
</asp:SqlDataSource>
I made a gridview table for Projects which has values ProjectName, ClientID, ProjectLeader, StartDate and all the employees that work on that Project. Since i cant fit all the employees that work on a single project inside a small table i made a button inside that column that redirects you to another page where you can see table with all the employees. The employees are in another table in sql and i made third table which connects Projects and employees and it only contains ProjectID and EmployeeID. I have a button inside every row but i cant find a way how to make in code behind so that it takes the id of that project and prints on another page only employees that are working on that project.
public partial class Project : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void EmployeesOpen_Click(object sender, EventArgs e)
{
Response.Redirect("Employees_On_Project.aspx");
}
}
You will find what you are looking for here : https://forums.asp.net/t/1957409.aspx?Gridview+get+SelectedRow+in+button+click+event

How to change the data in a asp gridlist on click with UserControls?

I'm trying to change the data of my gridlist on my homepage when you click on a button.. Tried many things nothing works. When I press the button to change gridview data nothing happpens and few secons later I get a error
How it looks: http://prntscr.com/7fa9qh
The Error : http://prntscr.com/7faam0
I have left some code out to make it not all to big, like I only have 2 buttons in code but there are more
list.aspx.cs :
protected void Page_Load(object sender, EventArgs e) {
GvTop2000 usercontrol = (GvTop2000)Page.LoadControl("~/Pages /Usercontrols/ListGv/GvTop2000.ascx");
pnlGVList.ContentTemplateContainer.Controls.Add(usercontrol);
}
protected void btnTop2000_Click(object sender, EventArgs e) {
pnlGVList.ContentTemplateContainer.Controls.Clear();
GvTop2000 usercontrol = (GvTop2000)Page.LoadControl("~/Pages/Usercontrols/ListGv/GvTop2000.ascx");
pnlGVList.ContentTemplateContainer.Controls.Add(usercontrol);
pnlGVList.Update();
}
protected void btnNieuw_Click(object sender, EventArgs e) {
pnlGVList.ContentTemplateContainer.Controls.Clear();
GvNieuw usercontrol = (GvNieuw)Page.LoadControl("~/Pages/Usercontrols/ListGv/GvNieuw.ascx");
pnlGVList.ContentTemplateContainer.Controls.Add(usercontrol);
pnlGVList.Update();
}
list.aspx :
<asp:Content ID="ContentHolderDD" runat="server" ContentPlaceHolderID="ContentHolderDD">
<asp:ScriptManagerProxy ID="smProxy" runat="server" />
<%-- Year selector --%>
<asp:DropDownList ID="ddlJaar" CssClass="ddl" runat="server" DataSourceID="DSYears" DataTextField="top2000jaar" DataValueField="top2000jaar" AutoPostBack="True" />
<asp:SqlDataSource runat="server" ID="DSYears" ConnectionString='<%$ ConnectionStrings:TOP2000_IAO4A_GROEP2ConnectionString %>' SelectCommand="sp_get_year" SelectCommandType="StoredProcedure" />
</asp:Content>
<asp:Content ID="ContentPlaceholderButtons" runat="server" ContentPlaceHolderID="ContentPlaceholderButtons">
gvTop2000 usercontrol:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="GvTop2000.ascx.cs" Inherits="Top2000.Pages.Usercontrols.ListGv.GvTop2000" %>
<asp:GridView runat="server" ID="gridList" CellPadding="10" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataSourceID="DSList" AllowPaging="True" PageSize="100" CssClass="GridListings">
<Columns>
<asp:BoundField DataField="positie" HeaderText="Positie" SortExpression="positie"></asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="artiestid" DataNavigateUrlFormatString="Artists.aspx?id={0}" DataTextField="naam" HeaderText="Artiest" />
<asp:HyperLinkField DataNavigateUrlFields="songid" DataNavigateUrlFormatString="Song.aspx?id={0}" DataTextField="titel" HeaderText="Titel" />
<asp:BoundField DataField="jaar" HeaderText="Jaar" SortExpression="jaar"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" ID="DSList" ConnectionString='<%$ ConnectionStrings:TOP2000_IAO4A_GROEP2ConnectionString %>' SelectCommand="sp_top2000_year" SelectCommandType="StoredProcedure" >
<SelectParameters>
<asp:ControlParameter PropertyName="SelectedValue" DefaultValue="" Name="YEAR" Type="Int32" ControlID="ContentHolderDD$ddlJaar" />
</SelectParameters>
</asp:SqlDataSource>
<asp:UpdatePanel runat="server" ID="pnlCButtons">
<ContentTemplate>
<asp:Button ID="btnTop2000" runat="server" Text="Top2000" OnClick="btnTop2000_Click" />
<asp:Button ID="btnNieuw" runat="server" Text="Nieuw" OnClick="btnNieuw_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
<asp:Content id="ContentPlaceHolder1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<%-- GridView --%>
<asp:UpdatePanel ID="pnlGVList" runat="server">
<ContentTemplate>
<asp:PlaceHolder runat="server" ID="phGv"/>
</ContentTemplate>
</asp:UpdatePanel>
gvNieuw usercontrol :
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="GvNieuw.ascx.cs" Inherits="Top2000.Pages.Usercontrols.ListGv.GvNieuw" %>
<asp:GridView runat="server" ID="gridList" CellPadding="10" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataSourceID="sqlDS" AllowPaging="True" PageSize="100" CssClass="GridListings" DataKeyNames="songid,top2000jaar,songid1,artiestid1">
<Columns>
<asp:BoundField DataField="songid" HeaderText="songid" SortExpression="songid" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="top2000jaar" HeaderText="top2000jaar" SortExpression="top2000jaar" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="positie" HeaderText="positie" SortExpression="positie" />
<asp:BoundField DataField="songid1" HeaderText="songid1" InsertVisible="False" ReadOnly="True" SortExpression="songid1" />
<asp:BoundField DataField="artiestid" HeaderText="artiestid" SortExpression="artiestid" />
<asp:BoundField DataField="titel" HeaderText="titel" SortExpression="titel" />
<asp:BoundField DataField="jaar" HeaderText="jaar" SortExpression="jaar" />
<asp:BoundField DataField="artiestid1" HeaderText="artiestid1" InsertVisible="False" ReadOnly="True" SortExpression="artiestid1" />
<asp:BoundField DataField="naam" HeaderText="naam" SortExpression="naam" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:TOP2000_IAO4A_GROEP2ConnectionString %>" SelectCommand="sp_new_songs_year" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="ContentHolderDD$ddlJaar" Name="top2000Year" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

Generate a table based on a selection from a dynamic drop down?

so I am learning ASP.NET currently and actually today is my second day.
So right now I am wanting to create a page where I have first a dynamically generated dropdown that selects just the first name from my database and table. This I have working
Next I have a gridview table that shows all the data from the database, currently I have hard coded a name so the page will not crash (there are 100000 rows of data)
Lastly when I post my code you will see a checkbox, that is just so when I click it the gridview will show up. I am just trying that out, because that was part of a lesson I learned today.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Test of First Database pull</h1>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" Text="Show Panel" OnCheckedChanged="checkbox1_CheckedChanged" />
<br />
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="FirstName" DataValueField="FirstName" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ASP.NETConnectionString1 %>" ProviderName="<%$ ConnectionStrings:ASP.NETConnectionString1.ProviderName %>" SelectCommand="SELECT DISTINCT [FirstName] FROM [SampleData] ORDER BY 'FirstName' "></asp:SqlDataSource>
<br />
<br />
<asp:Panel ID="Panel1" runat="server" Visible="False" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
<Columns>
<asp:BoundField DataField="SSN" HeaderText="SSN" SortExpression="SSN" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="HomeStreet" HeaderText="HomeStreet" SortExpression="HomeStreet" />
<asp:BoundField DataField="HomeCity" HeaderText="HomeCity" SortExpression="HomeCity" />
<asp:BoundField DataField="HomeState" HeaderText="HomeState" SortExpression="HomeState" />
<asp:BoundField DataField="HomeZip" HeaderText="HomeZip" SortExpression="HomeZip" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="CompanyStreet" HeaderText="CompanyStreet" SortExpression="CompanyStreet" />
<asp:BoundField DataField="CompanyCity" HeaderText="CompanyCity" SortExpression="CompanyCity" />
<asp:BoundField DataField="CompanyState" HeaderText="CompanyState" SortExpression="CompanyState" />
<asp:BoundField DataField="CompanyZip" HeaderText="CompanyZip" SortExpression="CompanyZip" />
<asp:BoundField DataField="HomePhone" HeaderText="HomePhone" SortExpression="HomePhone" />
<asp:BoundField DataField="CompanyPhone" HeaderText="CompanyPhone" SortExpression="CompanyPhone" />
<asp:BoundField DataField="CellPhone" HeaderText="CellPhone" SortExpression="CellPhone" />
<asp:BoundField DataField="DateOfBirth" HeaderText="DateOfBirth" SortExpression="DateOfBirth" />
<asp:BoundField DataField="DateOfHire" HeaderText="DateOfHire" SortExpression="DateOfHire" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASP.NETConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ASP.NETConnectionString1.ProviderName %>"
SelectCommand="SELECT [SSN], [LastName], [FirstName], [HomeStreet], [HomeCity], [HomeState], [HomeZip], [CompanyName],
[CompanyStreet], [CompanyCity], [CompanyState], [CompanyZip], [HomePhone], [CompanyPhone], [CellPhone],
[DateOfBirth], [DateOfHire] FROM [SampleData] WHERE [FirstName] = 'Bob' "></asp:SqlDataSource>
</asp:Panel>
</div>
</form>
</body>
</html>
Here is my C# part where i call the session
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Demos_GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string name = DropDownList1.SelectedValue;
Session["FName"] = name;
}
protected void checkbox1_CheckedChanged(object sender, EventArgs e)
{
Panel1.Visible = CheckBox1.Checked;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
As you see I called the session "I believe" I tried using this
<%:Session["user"]%>
within that sql query but it was saying I could not have the <%%> within the asp sql calling.
So if anyone could help me out that would be amazing.
Perhaps you should add the name of the user as a SqlParameter?
SqlDataSource1.SelectParameters.Add(new SqlParameter("#Name", name));
// Of course don't forget to edit your query: WHERE [FirstName] = #Name
Something as the above should work (note I haven't tested it).
Edit:
You could shorten this process even more by configuring your SqlDataSource to set the #Name parameter's value from a Control or Session.

Gridview not updating value

I am trying to make my gridview update a SQL record but it is not updating the values.
Here is the aspx code:
<asp:GridView ID="GridView1" runat="server" GridLines="None" AutoGenerateColumns="false"
onrowcancelingedit="GridView1_RowCancelling"
OnRowDeleting ="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
DataKeyNames="RID"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:TemplateField Visible="false" HeaderText="RID">
<ItemTemplate>
<asp:Label runat="server" ID="RID" Text='<%#Bind("RID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Short Description">
<ItemTemplate>
<asp:Label runat="server" ID="short_lbl" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="SHORT_DESCRIPTION" Text='<%#Bind("SHORT_DESCRIPTION") %>' />
<asp:RequiredFieldValidator runat="server" ID="valShort" ControlToValidate="SHORT_DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server" ID="desc_label" Text='<%#Bind("DESCRIPTION") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="DESCRIPTION" Text='<%#Bind("DESCRIPTION") %>' />
<asp:RequiredFieldValidator runat="server" ID="valLast" ControlToValidate="DESCRIPTION" ValidationGroup="var1" ErrorMessage="*" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" />
<br />
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the update function itself.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();
Label RID = GridView1.Rows[e.RowIndex].FindControl("RID") as Label;
TextBox SHORT_DESCRIPTION = GridView1.Rows[e.RowIndex].FindControl("SHORT_DESCRIPTION") as TextBox;
TextBox DESCRIPTION = GridView1.Rows[e.RowIndex].FindControl("DESCRIPTION") as TextBox;
String UpdateQuery = string.Format("UPDATE TAXONOMIES SET SHORT_DESCRIPTION='{0}', DESCRIPTION='{1}' WHERE RID = {2}", SHORT_DESCRIPTION.Text, DESCRIPTION.Text, Convert.ToInt32(RID.Text));
GridView1.EditIndex = -1;
BindGridData(UpdateQuery);
}
If I put a breakpoint and look at the value, I can see that is is not the newly typed in value. I'm not sure what is going on.
I think the problem is you are binding you grid on the page-load event.
try binding it in if (!IsPostBack)
Example
private void Page_Load()
{
if (!IsPostBack)
{
//bind your grid here.
}
}
use this code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="regGroupID" HeaderText="regGroupID"
SortExpression="regGroupID" />
<asp:BoundField DataField="amountReceived" HeaderText="amountReceived"
SortExpression="amountReceived" />
<asp:BoundField DataField="other" HeaderText="other" SortExpression="other" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Table]"
UpdateCommand="UPDATE [Table] SET [regGroupID] = #regGroupID, [amountReceived] = #amountReceived, [other] = #other WHERE [id] = #id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
<asp:Parameter Name="other" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
here I set id as the primary key
table structure is
Try changing the Eval to Bind. Eval is read-only so you can't actually update the value.

Nested GridView - How to trigger the Child GridView Button Click event

I need to show a Master/Child data in a page and I have used multiple GridViews to achieve the same. So, I have created two GridViews (Parent & Child) and now I want to fire the Button click event (i.e. btnLock) from the child gridview control and do some DB operations. So, I dont know how to achieve this.
Please help.
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:GridView Width="100%" AllowPaging="True" ID="gvCustomers" AutoGenerateColumns="False"
DataSourceID="sqlDsCustomers" runat="server" ShowHeader="False" OnRowCreated="gvCustomers_RowCreated">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="group" id='<%#String.Format("customer{0}",Container.DataItemIndex) %>' onclick='showhide(<%#String.Format("\"#customer{0}\"",Container.DataItemIndex) %>,<%#String.Format("\"#order{0}\"",Container.DataItemIndex) %>)'>
<asp:Image ID="imgCollapsible" CssClass="first" ImageUrl="~/Assets/img/plus.png"
Style="margin-right: 5px;" runat="server" /><span class="header">
<%#Eval("CustomerID")%>
:
<%#Eval("CompanyName")%>
(<%#Eval("TotalOrders")%>
Orders) </span>
</div>
<asp:SqlDataSource ID="sqlDsOrders" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT [OrderID], [OrderDate], [RequiredDate], [Freight], [ShippedDate] FROM [Orders] WHERE ([CustomerID] = #CustomerID)">
<SelectParameters>
<asp:Parameter Name="CustomerID" Type="String" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>
<div id='<%#String.Format("order{0}",Container.DataItemIndex) %>' class="order">
<asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
runat="server" ShowHeader="true" EnableViewState="false">
<RowStyle CssClass="row" />
<AlternatingRowStyle CssClass="altrow" />
<Columns>
<asp:TemplateField ItemStyle-CssClass="rownum">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Order ID" DataField="OrderID" ItemStyle-Width="80px" />
<asp:BoundField HeaderText="Date Ordered" DataField="OrderDate" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="100px" />
<asp:BoundField HeaderText="Date Required" DataField="RequiredDate" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="110px" />
<asp:BoundField HeaderText="Freight" DataField="Freight" DataFormatString="{0:c}"
ItemStyle-Width="50px" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField HeaderText="Date Shipped" DataField="ShippedDate" DataFormatString="{0:MM/dd/yyyy}"
ItemStyle-Width="100px" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnLock" Text="Lock" CommandName="Lock" CommandArgument=<%# Eval("OrderID") %> runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Thanks
Use the RowCommand:
<asp:GridView AutoGenerateColumns="false" CssClass="grid" ID="gvOrders" DataSourceID="sqlDsOrders"
runat="server" ShowHeader="true" EnableViewState="false"
onrowcommand="gvOrders_RowCommand"
>
........
</asp:GridView >
protected void gvOrders_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Lock")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
//dowork
}
}

Categories

Resources