I've a grid view with three columns Employee name Employee details and Employee age.
I want to add a check box at each row and after selection of each check box i want to fire a insert query associated to that employee.
Can you tell me how to add this dynamic functionality to the grid view.
also if we use <%# EVAL %> i don't know how to implement it with checkbox.
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="details" HeaderText="details"
SortExpression="details" />
<asp:BoundField DataField="age" HeaderText="age" SortExpression="age" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="OK" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connApps %>"
SelectCommand="SELECT [name], [details], [age] FROM [tblA]">
</asp:SqlDataSource>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Selected item name:<br>";
foreach (GridViewRow item in GridView1.Rows)
{
CheckBox chk = (CheckBox)item.FindControl("CheckBox1");
if (chk != null)
{
if (chk.Checked)
{
Label1.Text += item.Cells[1].Text + "<br>";
}
}
}
}
Output:
Here is an example ,
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"
CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
Font-Names="Arial" GridLines="Vertical" Width="40%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkStatus" runat="server"
AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
</Columns>
<HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />
For more information , check Here !
you can use TemplateFields for the GridView:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="myCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and in code behind use below code:
ClusterName = GV.Rows(1).Cells(2).FindControl("myLabelinTheGridViewTemplateField")
Related
I have a custom GridView in asp.net Page. In PageLoad data will be bound to the grid.
protected void Page_Load(Object sender, EventArgs e)
{
InitDisplayMode(sender);
}
private void InitDisplayMode(Object sender)
{
pnlDetails.Visible = SafetyKeyId != -1;
if (!Page.IsPostBack)
{
BindValues();
}
// some other stuff
}
private void BindValues()
{
MyStrongTypedDataTable tbl = new BusinessObject.GetData();
dataGrid.DataSource = tbl;
dataGrid.DataBind();
}
Grid View Makrup
<asp:GridView ID="dataGrid" runat="server" AutoGenerateColumns="False" OnRowCreated="OnRowCreated"
AllowSorting="true" BorderWidth="0" CellPadding="2" CssClass="subitem w100p"
AllowPaging="true" PageSize="25" RowStyle-Wrap="false" HeaderStyle-Wrap="false"
OnPageIndexChanging="PageGrid" OnRowDataBound="dataGrid_RowDataBound" PagerSettings-Mode="Numeric"
PagerSettings-Position="Bottom" OnSorting="SortGrid" OnRowDeleting="RowDeleting">
<RowStyle CssClass="itemRowStyle"/>
<AlternatingRowStyle CssClass="alternatingItemRowStyle"/>
<HeaderStyle CssClass="headerRowStyle"/>
<Columns>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:HyperLink ID="lnkDetail" runat="server" meta:resourcekey="lnkDetail" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_file.png" %>'
NavigateUrl="~/BrandMgmt/BrandMgmtPublicKey.aspx?"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:HyperLink ID="lnkEdit" runat="server" meta:resourcekey="lnkEdit" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_edit.png" %>'
NavigateUrl="~/BrandMgmt/BrandMgmtPublicKey.aspx?"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle">
<ItemTemplate>
<asp:ImageButton ID="cmdDelete" Width="16" Height="16" runat="server" ImageUrl='<%# "~/App_Themes/" + Page.Theme + "/icons/16/icon_delete.png" %>'
CommandName="Delete" CommandArgument='<%# Eval("SafetyKeyId") %>' OnCommand="OnDelete"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="KeyOwnerIdentifier" HeaderText="KeyOwnerIdentifier" meta:resourcekey="ColumnKeyOwnerIdentifier"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="KeyOwnerIdentifier"/>
<asp:BoundField DataField="SafetyKeyName" HeaderText="SafetyKeyName" meta:resourcekey="ColumnSafetyKeyName"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="SafetyKeyName"/>
<asp:BoundField DataField="SafetyKeyTypeId" HeaderText="SafetyKeyTypeId" meta:resourcekey="ColumnSafetyKeyTypeId"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="SafetyKeyTypeId"/>
<asp:BoundField DataField="KeyIndex" HeaderText="KeyIndex" meta:resourcekey="ColumnKeyIndex"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="KeyIndex"/>
<asp:BoundField DataField="FromDate" HeaderText="FromDate" meta:resourcekey="ColumnFromDate"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="FromDate"/>
<asp:BoundField DataField="ToDate" HeaderText="ToDate" meta:resourcekey="ColumnToDate"
ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle" SortExpression="ToDate"/>
<asp:TemplateField ItemStyle-CssClass="itemFieldStyle" HeaderStyle-CssClass="headerFieldStyle"
meta:resourcekey="ColumnPublicKey" SortExpression="PublicKey">
<ItemTemplate>
<div style="max-width: 200px; overflow-y: scroll;">
<asp:Label runat="server" Text='<%# Eval("PublicKey") %>'/>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Grid hat 3 ImageButtons for detail view, editing and deleting per row. DetaliView Button adds additional Table with data to the Page. Editing makes this Table editable. If the grid is sorted and any of those buttons is beeing clicked, Page loads and any sorting is lost.
How can I maintain the sort setting in PostBack after clicking any of those buttons?
I'm displaying information in a Datagrid not gridView in asp.net. the datagrid contain a column called Status. the status can either be 1 or -1. if the status is 1, i'm displaying a mark icon, then if the status is -1 then displayed a cancel icon. i have done the coding and its working but the problem is, the icons are not showing in all the column depending on the condition as shown in the image below. based on the value in my databasee, the status columns are all -1 but if its showing, its skip on column and bind the next column and so on.
here is my DataGrid Markup
<asp:DataGrid ID="VehicleInfo" runat ="server" AutoGenerateColumns="False" GridLines="Horizontal" Width="939px" CssClass=" table table-striped table-bordered" AllowCustomPaging="True" AllowPaging="True" PageSize="50" OnItemDataBound="VehicleInfo_ItemDataBound" OnItemCommand="VehicleInfo_ItemCommand" >
<Columns>
<asp:TemplateColumn HeaderText="#" >
<ItemTemplate>
<%# Container.DataSetIndex + 1 %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Type" HeaderText="Veh. Type"></asp:BoundColumn>
<asp:BoundColumn DataField="Make" HeaderText="Make/Model"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Veh. Reg No">
<ItemTemplate>
<asp:Label ID="RegNo" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.RegistrationNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="EngineNumber" HeaderText="Engine No"></asp:BoundColumn>
<asp:BoundColumn DataField="ChasisNo" HeaderText="Chasis No"></asp:BoundColumn>
<asp:BoundColumn DataField="InjectionYear" HeaderText="Injection Year"></asp:BoundColumn>
<asp:BoundColumn DataField="Size" HeaderText="Veh. Size"></asp:BoundColumn>
<asp:BoundColumn DataField="ServiceTypeName" HeaderText="Service Type"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Response Status">
<ItemTemplate>
<asp:Label ID="Status" Visible="false" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.registrationstatusResponse") %>'></asp:Label>
<asp:ImageButton ID="cancel" Width="30" Height="30" ImageUrl="/images/cancel.png" Runat=server Visible="false" />
<asp:ImageButton ID="good" Width="30" Height="30" ImageUrl="/images/good.png" Runat=server Visible="false" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="verificationComment" HeaderText="Remark"></asp:BoundColumn>
</Columns>
<HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="X-Small" CssClass="" ForeColor="White" BackColor="#428BCA" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
<ItemStyle Font-Names="Verdana" Font-Size="X-Small" />
</asp:DataGrid>
and here is my code on the itemDataBound event and i Even try using a loop on the ItemDataBound event but it doesn't work. Please i need your help if there is anything im missing.
protected void VehicleInfo_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item))
{
var lab1 = (Label)e.Item.FindControl("status");
var img1 = (ImageButton)e.Item.FindControl("good");
var img2 = (ImageButton)e.Item.FindControl("cancel");
if (lab1.Text == "1")
{
img2.Visible = false;
img1.Visible = true;
}
else if (lab1.Text == "-1")
{
img2.Visible = true;
img1.Visible = false;
}
}
}
I have a GridView, In one column I have LinkButton control. I want to disable click from client side for certain condition on this column. Means for some rows it will not be possible for User to call onclick event and for some rows it is possible.
I want to achieve this from client side using javascript.
Or When User clicks on link, It will notify the User that this action can't be completed for this row.
<asp:GridView Width="100%" ShowHeader="true" ViewStateMode="Enabled" GridLines="Both" CellPadding="10" CellSpacing="5" ID="GridViewMultiplePOSAssociationId" runat="server" AllowSorting="false" AutoGenerateColumns="false" OnRowCommand="RewardGridMultiD_RowCommand"
AllowPaging="true" PageSize="8" OnRowDataBound="grdViewCustomers_OnRowDataBound" PagerSettings-Position="Top" PagerSettings-Mode="NumericFirstLast" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" DataKeyNames="POS Id">
<RowStyle CssClass="table_inner_text" BackColor="WhiteSmoke" BorderColor="CornflowerBlue" Wrap="true" ForeColor="Black" Height="30px" />
<HeaderStyle CssClass="table_head_text" />
<Columns>
<asp:TemplateField ItemStyle-Width="80px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval(" POS Id ") %>');">
<img alt="Details" id="imgdiv<%# Eval("POS Id") %>" src="images/plus.png" />
</a>
<div id="div<%# Eval(" POS Id ") %>" style="display: none;">
<asp:GridView ID="grdViewOrdersOfCustomer" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
<RowStyle CssClass="table_inner_text" BackColor="SkyBlue" BorderColor="Black" Wrap="true" ForeColor="White" Height="30px" />
<Columns>
<asp:BoundField ItemStyle-Width="150px" DataField="RULE FILE NAME" HeaderText="RULE FILE NAME" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE ID" HeaderText="RULE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="RULE TYPE ID" HeaderText="RULE TYPE ID" />
<asp:BoundField ItemStyle-Width="150px" DataField="START TIME" HeaderText="START TIME" />
<asp:BoundField ItemStyle-Width="150px" DataField="EXPIRY TIME" HeaderText="EXPIRY TIME" />
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="Row Number">
<ItemTemplate>
<asp:Label ID="LabelRowNumberId" runat="server" Text='<%#Eval("Row Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="80px" ItemStyle-HorizontalAlign="Center" HeaderText="POS Id">
<ItemTemplate>
<asp:Label ID="LabelPOSID" runat="server" Text='<%#Eval("POS Id") %>'></asp:Label>
<%-- <asp:LinkButton ID="LinkbtnPOSId" CommandArgument='<%#Eval("POS Id") %>' CommandName="ClickPOS" Text='<%#Eval("POS Id") %>' runat="server" CausesValidation="false"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="HyperLinkAssociate" CommandArgument='<%#Eval("POS Id") %>' CommandName="Associate" Text="Associate" runat="server" OnClientClick="return OnClientClickAssociateRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>/
<asp:LinkButton ID="HyperLinkReplace" CommandArgument='<%#Eval("POS Id") %>' CommandName="Replace" Text="Replace" runat="server" OnClientClick="return OnClientClickReplaceRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center" HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" ID="LabelStatusPendingPOSId" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In GridViewMultiplePOSAssociationId there is one column "Status" which has label LabelStatusPendingPOSId, LabelStatusPendingPOSId text is set Applied, Not Applied at the time of Binding. For Rows which has Status Applied, User should not be able to click on LinkButton Associate/Replace else He is allowed to click.
use this code OnRowDataBound for Your Grid
protected void grdViewCustomers_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
LinkButton LinkbtnPOSId=(LinkButton)e.Row.FindControl("LinkbtnPOSId");
Label LabelStatusPendingPOSId = (Label)e.Row.FindControl("LabelStatusPendingPOSId");
Boolean boolStatus = LabelStatusPendingPOSId.Text == "Applied" ? true : false;
//LinkbtnPOSId.Attributes.Add("onclick", "function CheckStatus('" + boolStatus.ToString() + "')");
LinkbtnPOSId.Enabled = boolStatus;
}
}
Try this..
You can loop through the Gridview and make the particular control disable...
I am writing the code in javascript pageload function...
function PageLoad(sender, args)
{
for (var i = 0; i <= RowCount; i++)
{
document.getElementById('Gridview1_HyperLinkAssociate_' + i.toString() + '').disabled = true;
}
}
Set RowCount as the number of rows of the Gridview...
When a Button in the GridView is clicked I can't seem to get the row index or the row that is to be eliminated, I need the Id and the Complete route of the archive, both of them are on the bound fields of the GridView, here is the code I have:
Gridview Code:
<asp:GridView ID="gdvData"
AllowSorting="False"
AllowPaging="True"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="False"
runat="server"
EmptyDataText="No existen archivos cargados."
Width="100%">
<AlternatingRowStyle CssClass="alternatingrowstyle" />
<Columns>
<asp:BoundField HeaderText="Id"
DataField="Id"
Visible="false" >
<HeaderStyle CssClass="left" />
<ItemStyle CssClass="left" />
</asp:BoundField>
<asp:BoundField HeaderText="RutaCompleta"
DataField="RutaCompleta"
Visible="false" >
<HeaderStyle CssClass="left" />
<ItemStyle CssClass="left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Archivo">
<ItemTemplate>
<asp:HyperLink runat="server"
CssClass="left"
Target="_blank"
NavigateUrl='<%#Eval("RutaCompleta")%>'
Text='<%#Eval("Archivo")%>'>
</asp:HyperLink>
</ItemTemplate>
<HeaderStyle CssClass="left" />
<ItemStyle HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/Images/page_delete.ico"
HeaderText="Eliminar">
<ItemTemplate>
<asp:ImageButton ID="ImgDelete"
runat="server"
CommandArgument="Delete"
ImageUrl="~/Images/page_delete.ico"
OnClick="btnEliminar_Click"
OnClientClick="return confirm('¿Esta seguro de eliminar este archivo?');"
ToolTip="Borrar Documento"/>
</ItemTemplate>
<HeaderStyle CssClass="center" />
<ItemStyle CssClass="center"/>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerstyle" />
<PagerStyle CssClass="pagerstyle" />
<PagerTemplate>
<asp:Label ID="Label1" runat="server" Text="Mostrar filas:" />
<asp:DropDownList ID="ddlPageSize"
runat="server"
AutoPostBack="true"
CssClass="CombosBox"
>
<asp:ListItem Value="10" />
<asp:ListItem Value="15" />
<asp:ListItem Value="20" />
</asp:DropDownList>
<asp:Label ID="lblDesde" runat="server" Text="Página" />
<asp:TextBox ID="txtGoToPage"
runat="server"
AutoPostBack="true"
CssClass="gotopage"
/>
<asp:Label ID="lblHasta" runat="server" Text="de " />
<asp:Label ID="lblTotalNumberOfPages" runat="server" />
<asp:Button ID="btnAnt"
runat="server"
CommandArgument="Prev"
CommandName="Page"
CssClass="previous"
ToolTip="ant. página" />
<asp:Button ID="btnProx"
runat="server"
CommandArgument="Next"
CommandName="Page"
CssClass="next"
ToolTip="prox. página" />
</PagerTemplate>
</asp:GridView>
Code Behind:
protected void btnEliminar_Click(object sender, EventArgs e)
{
try
{
Int64 intId = 0;
String strRutaCompleta = String.Empty;
GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
Label lblId = (Label)row.FindControl("lblId");
Label lblRutaCompleta = (Label)row.FindControl("lblRutaCompleta");
intId = Convert.ToInt64(lblId.Text.ToString());
strRutaCompleta = lblRutaCompleta.Text.ToString();
/*Other part of the code*/
}
catch(Exception)
{
/*Other part of the code*/
}
}
I have tried various methods found here on StackOverflow, hope you guys can help me. Thanks.
Instead of using .Parent.Parent, I recommend using the CommandArgument property of the ImageButton and set the dataitem's Id.
Here is another post on StackOverflow that give a good bit of code: ASP.NET GridView RowIndex As CommandArgument
It appears to me that all you need is the Id of the data you want to delete. So instead of trying to use the row's index, just put the Id in the button's CommandArgument
Example:
<asp:ImageButton ID="ImgDelete"
runat="server"
CommandArgument="Delete"
ImageUrl="~/Images/page_delete.ico"
OnClick="btnEliminar_Click"
CommandArgument='<%#Eval("Id")%>'
OnClientClick="return confirm('¿Esta seguro de eliminar este archivo?');"
ToolTip="Borrar Documento"/>
Code Behind:
protected void btnEliminar_Click(object sender, EventArgs e)
{
try
{
Int64 intId = 0;
ImageButton btn = (sender as ImageButton)
if(btn != null)
{
Int64 tempId;
if(Int64.TryParse(btn.CommandArgument, out tempId))
{
intId = intId;
/*Other part of the code*/
}
}
}
catch(Exception)
{
/*Other part of the code*/
}
}
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
}
}