How can use FindControl in the OnRowCommand of GridView? - c#

I am trying to use findcontrol TextBox in RowCommand of the gridview.
But Error Object reference not set to an instance of an object.
Help me please.
Design
<asp:GridView ID="gvMaster" runat="server" AllowPaging="true" AutoGenerateColumns="False" OnRowCommand="gvMaster_RowCommand" Width="100%">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" runat="server" CommandName="edit" ImageUrl="~/images/edit.gif" ToolTip="Insert/Edit" />
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td nowrap="nowrap">
<asp:ImageButton ID="ibtnSave" runat="server" CommandName="update" ImageUrl="~/images/icon-floppy.gif" ToolTip="Save" />
</td>
<td nowrap="nowrap">
<asp:ImageButton ID="ibtnCancel" runat="server" CommandName="cancel" ImageUrl="~/images/icon-cancel.gif" ToolTip="Cancel" />
</td>
</tr>
</table>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="30px" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Effective Date">
<ItemTemplate>
<asp:Label ID="lblEffectiveDate" runat="server" Text='<%# String.Format("{0:dd/MM/yyyy}", Eval("eff_date")) %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td nowrap="nowrap">
<asp:TextBox ID="txtEffDate2" runat="server" MaxLength="10" Text='<%# String.Format("{0:dd/MM/yyyy}", Eval("eff_date")) %>'
Width="70px" Style="text-align: center"></asp:TextBox>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code BeHind:
protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToUpper().Equals("SELECT"))
{
}
else if (e.CommandName.ToUpper().Equals("EDIT"))
{
string cmdNmEdit = e.CommandName;
object cmdSrcEdit = e.CommandSource;
GridViewRow gvMaster = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
TextBox txtEffDate2 = gvMaster.FindControl("MyTextBoxId") as TextBox;
txtEffDate2.Text = DateTime.Now.ToString("DD/MM/yyyy"); //<------ Error This Line
}
}
How can use FindControl in the OnRowCommand of GridView?
Thanks in advance. ;)

The CommandArgument contains the row-index by default. That's why this works:
protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
GridView grid = (GridView) sender;
GridViewRow row = grid.Rows[rowIndex];
// now you can use row.FindControl
}

Related

Get text box value from grid view Header

I have grid with text box for filter data on header.I have button outside the grid.I am using this gridview for filtering the grid by some columns. When I click on button I want to get value from text box and dropdownlist on codebehind.cs, below is my webform.aspx code of gridview,
<asp:Button ID="btngetLocationDate" runat="server" Text="Get Filtered Data" OnClick="getTextBoxValue"></asp:Button>
<asp:GridView ID="TrackerGrid" runat="server" BackColor="#CCCCCC" AllowPaging="True" AutoGenerateColumns="false">
<AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:TemplateField HeaderText="User ID" SortExpression="User ID">
<ItemTemplate>
<asp:Label ID="lbluserid" runat="server" Text='<%# Eval("User ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<HeaderTemplate>
Full Name:
<asp:DropDownList ID="ddlfullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="onselectDropdown">
<asp:ListItem Text="ALL" Value="ALL" Selected="True"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("Full Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<HeaderTemplate>
Mobile Number:
<asp:TextBox ID="txtMobilenumber" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%# Eval("Mobile Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address">
<HeaderTemplate>
Email Id:
<asp:TextBox ID="txtemail" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%# Eval("Email ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Longitude">
<HeaderTemplate>
<asp:Label ID="lbllng" runat="server" Text="Longitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllongi" runat="server" Text='<%# Eval("Longitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lattitude">
<HeaderTemplate>
<asp:Label ID="lbllat" runat="server" Text="Lattitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllatti" runat="server" Text='<%# Eval("Lattitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activated On">
<HeaderTemplate>
<asp:Label ID="lbldevices" runat="server" Text="Activated On"></asp:Label></br>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldevice" runat="server" Text='<%# Eval("Activated On") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location DateTime" SortExpression="Location DateTime">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Location DateTime"></asp:Label></br>
<asp:Label ID="lblStartDate" runat="server" AssociatedControlID="txtStartDate" Text="Start Date"></asp:Label>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarStartDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtStartDate" popupbuttonid="imgPopup1"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup1" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</br>
<asp:Label ID="lblEndDate" runat="server" Text="End Date" AssociatedControlID="txtEndDate"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarEndDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtEndDate" popupbuttonid="imgPopup2"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup2" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDatetime" runat="server" Text='<%# Eval("Location DateTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and I am getting texbox value in codebehind code.
protected void getTextBoxValue(object sender, EventArgs e)
{
DropDownList ddlfullname = (TrackerGrid.HeaderRow.FindControl("ddlfullName") as DropDownList);
string selectedValue = ddlfullname.SelectedItem.Text;
TextBox mobilenumber = (TrackerGrid.HeaderRow.FindControl("txtMobilenumber") as TextBox);
string mobile = mobilenumber.Text;
TextBox email = TrackerGrid.HeaderRow.FindControl("txtemail") as TextBox;
string emailid = email.Text;
TextBox staloc = TrackerGrid.HeaderRow.FindControl("txtStartDate") as TextBox;
string startlocatdate = staloc.Text;
TextBox endloc = TrackerGrid.HeaderRow.FindControl("txtEndDate") as TextBox;
string endlocadate = endloc.Text;
}
I am getting empty value when I debugging this code.I think my code is correct but how i am not getting value of textboxes, i don't understand. Please help me for solving my issue.
Thanks in advance.
You probably did not wrap the DataBind() of the Gridview inside an IspostBack check. If you do not then the values will be rested by the DataBind instead of being saved with ViewState.
protected void Page_Load(object sender, EventArgs e)
{
//not here
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
if (IsPostBack == false)
{
//but inside an ispostback check
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
}
}

Filtering gridview with textbox in header

Soo I'm trying to filter my gridview with textboxes in the header. I created a textchanged method for every textbox. Now I want to filter the gridview and bind the filtered data. For some reason the datasource is null once it hits the textchanged method. See my code below:
<asp:GridView ID="GridView_Imported" runat="server" CssClass="GridView-Upload" Width="100%" OnRowDataBound="GridView_Imported_RowDataBound" HorizontalAlign="Center" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" CssClass="checkAll" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkCtrl" runat="server" CssClass="chkCtrl" onclick="Check_Click(this)" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="filter">
<HeaderTemplate>
<asp:Label ID="lbArticleName" runat="server" Text="Article-Name:" CssClass="Article-Name-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterArticleName" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticleName_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="ArticleNameText" runat="server" Text='<%# Eval("Article-Name") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbArticle" runat="server" Text="Article:" CssClass="Article-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterArticle" runat="server" placeholder="Filter..." OnTextChanged="tbFilterArticle_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbArticleText" runat="server" Text='<%# Eval("Article") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbWarehouse" runat="server" Text="Warehouse:" CssClass="Warehouse-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterWarehouse" runat="server" placeholder="Filter..." OnTextChanged="tbFilterWarehouse_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbWarehouseText" runat="server" Text='<%# Eval("Warehouse") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbLocation" runat="server" Text="Location:" CssClass="Location-Label"></asp:Label>
<br/>
<asp:TextBox ID="tbFilterLocation" runat="server" placeholder="Filter..." OnTextChanged="tbFilterLocation_TextChanged" AutoPostBack="True"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbLocationText" runat="server" Text='<%# Eval("Location") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
</asp:GridView>
protected void tbFilterArticleName_TextChanged(object sender, EventArgs e)
{
var tb = (TextBox) sender as TextBox;
GridView_Imported.DataSource = (Session["dataSource"] as DataTable).DefaultView.RowFilter = string.Format("Article-Name LIKE '%{0}%'", tb.Text.Trim());
GridView_Imported.DataBind();
}
What I'm missing?
There are two errors in your code.
First put brackets for column name Article-Name as [Article-Name].
Second the way you assigning datasource to gridview. See the correct way below.
var dt = (Session["dataSource"] as DataTable);
dt.DefaultView.RowFilter = string.Format("[Article-Name] LIKE '%{0}%'", tb.Text.Trim());
GridView_Imported.DataSource = dt;
GridView_Imported.DataBind();
Use http://www.datatables.net/
It provides data filtering, sorting, paging etc.
Just place a html table header above your gridview with some id
<HeaderTemplate>
<table id="tblData" border="0" cellpadding="5" cellspacing="1" width="100%">
<thead>
<th align="center" width="3%">S.No.
</th>
<th align="center" width="10%">RFQ ID
</th>
</thead>
</HeaderTemplate>
Add add jquery script
$(document).ready(function () {
$('#tblData').DataTable();
});
First, in RowDataBound event of your gridview(to find your textbox):
System.Web.UI.WebControls.TextBox tbFilterArticle;
protected void GridView_Imported_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (GridView_Imported.HeaderRow != null)
{
tbFilterArticle = (System.Web.UI.WebControls.TextBox)GridView_Imported.HeaderRow.FindControl("tbSearchUser");
}
}
After that, to use it's value(anywhere you want, i used it into a button click event):
string article = tbFilterArticle.Text;

linkbutton or button onClick not fireing inside gridview ItemTemplate

hi i have a gridview in my asp.net webpage that is bounded after a user selects 3 dropdown list.
dropdown list's auto postbacks are set true , now my problem is when i put a button , imagebutton or linkbutton in my gridview ItemTemplate the onclick event dosent fire !
here is my code
<asp:GridView ID="GridView1" runat="server" Width="90%" Height="100%" OnRowCommand="GridView1_RowCommand" OnPageIndexChanging="GridView1_PageIndexChanging" style="margin:20px; vertical-align:top;" PageSize="10" AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="WFStudentID" ShowHeader="true" BorderWidth="1px">
<Columns>
<asp:TemplateField ShowHeader="true">
<ItemTemplate >
<tr>
<td>
<asp:Label ID="Label1" ForeColor="Silver" Font-Size="Medium" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("Family") %>'></asp:Label>
</td>
<td>
<asp:HyperLink ID="HyperLink1" ForeColor="Silver" Font-Size="Medium" Target="_blank" NavigateUrl='<%# Bind("WFStudentFilePath") %>' runat="server">دانلود</asp:HyperLink>
</td>
<td>
<asp:Label ID="Label7" runat="server" ForeColor="Silver" Font-Size="Medium" Text='<%# Bind("WFStudentDate") %>'></asp:Label>
</td>
<td>
<asp:Button ID="Deny" Text="deny" OnClick="Deny_Click1" runat="server" />
</td>
<td>
<asp:ImageButton ID="accept" OnClick="accept_Click" runat="server" />
</td>
<td>
<asp:TextBox ID="des" TextMode="MultiLine" Height="100px" Width="200px" runat="server"></asp:TextBox>
</td>
</tr>
<br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Deny_Click1(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
It is probably firing, but it will throw an exception (which your catch block ignores). The exception is because of this line:
LinkButton Link = (LinkButton)sender;
The sender is a Button, not a LinkButton, so the cast is not valid and an exception will be thrown.
you can check this::
http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html
verify your code like this
Buttons inside GridView do not respond to direct events. They have to go through GridView's RowCommand event implementation. I've seen that you have implemented OnRowCommand="GridView1_RowCommand". So now you need make following change to image button:
<asp:ImageButton ID="accept" runat="server" CommandName="Accept" />
Now look for "Accept" command name in your GridView1_RowCommand event handler.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Accept")
{
// Your code goes here
}
}
I hope this helps.

Bind Dropdown list in Gridview footer which is in Tab Control

I am using ASP.NET and Entity Framework and Tab control of AJAX
I have a dropdown list control in gridview which is in TAB control Am not able to bind dropdown list control.
Tab Control --> Gridview --> Dropdown list
My ASP.NET Code is
<ajax:TabContainer ID="TabContainer2" runat="server" CssClass="fancy fancy-green">
<ajax:TabPanel ID="TabPanel1" runat="server">
<HeaderTemplate>
College Toppers
</HeaderTemplate>
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<div>
<asp:GridView ID="GridView1" DataKeyNames="TL_ID" runat="server"
AutoGenerateColumns="False" CssClass="Gridview" ShowFooter="True" OnRowDataBound="GRVToppers_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Admin/Images/update.jpg" ToolTip="Update" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Admin/Images/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Admin/Images/AddNewitem.jpg" CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Admin/Images/Edit.jpg" ToolTip="Edit" Height="20px" Width="20px" /><asp:ImageButton ID="imgbtnDelete" CommandName="Delete" runat="server" ImageUrl="~/Admin/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Topper List No">
<EditItemTemplate>
<asp:Label ID="lblEditTpid" runat="server" Text='<%#Eval("TL_ID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblShowTpid" runat="server" Text='<%#Eval("TL_ID") %>' />
</ItemTemplate>
<ControlStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Department Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlEditDeptname" runat="server" Width="90" DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="true"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddladdnewDeptname" runat="server" Width="90" DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="True"></asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblShowDeptname" runat="server" Text='<%#Eval("DEPT_NAME") %>' Width="90" />
</ItemTemplate>
<ControlStyle Width="30px" />
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White"></HeaderStyle>
</asp:GridView>
</div>
</asp:Panel>
</ContentTemplate>
</ajax:TabPanel>
</ajax:TabContainer>
==================
My C# code is
protected void GRVToppers_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
var dept = from n in ecme.DEPT_MASTER
select new { n.DEPT_ID, n.DEPT_NAME };
DropDownList ddl = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddl = e.Row.FindControl("ddladdnewDeptname") as DropDownList;
int cnt = 0;
cnt = dept.Count();
ListItem[] items = new ListItem[cnt];
for (int i = 0; i < cnt; i++)
{
items[i] = new ListItem("DEPT_NAME", "DEPT_ID");
}
}
}
catch (Exception)
{ }
}
========================
You can do this very easily...
Remove
DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="True"
and
DataTextField='<%#Bind("DEPT_NAME") %>' DataValueField='<%#Bind("DEPT_ID") %>' AppendDataBoundItems="true"
from aspx code
and Make changes in
protected void GRVToppers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var dept = from n in ecme.DEPT_MASTER
select new { n.DEPT_ID, n.DEPT_NAME };
DropDownList ddl = (DropDownList)e.Row.FindControl("ddladdnewDeptname");
ddl.DataTextField = "DEPT_NAME";
ddl.DataValueField = "DEPT_ID";
ddl.DataSource = dept.ToList();
ddl.DataBind();
ddl.Items.Insert(0, "--Select Department--");
}
}
100% sure this 'll work...

Remove individual rows of a grid view only on the client side

I have a drop down list. On changing the index of the dropdownlist , I populate an asp.net gridview.
I have a requirement where the user should be able to remove individual rows of the gridview on the screen .
At the end of each row, I intend to have a remove button. On clicking the button the row should
disappear . But this should be only on the screen. There should be no changes done in the database.
I have my code below right now :
aspx
<table>
<tr>
<td>
<div>
<asp:Label ID="lblClient" runat="server" Text="Client :" CssClass="label" ForeColor="Black"></asp:Label>
<asp:DropDownList ID="ddlClient" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddlClient_SelectedIndexChanged">
<asp:ListItem Text="ALL" Value="0"></asp:ListItem>
</asp:DropDownList>
</div>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvMainLog" runat="server" Visible="true" AllowSorting="True" AutoGenerateColumns="False"AllowPaging="true">
<Columns>
<asp:BoundField DataField="Instruction" HeaderText="Instruction" />
<asp:BoundField DataField="ProviderId" HeaderText="Id" />
</Columns>
</asp:GridView>
<div>
<asp:TextBox ID="txtEditMin" runat="server"></asp:TextBox>
</div>
</td>
</tr>
</table>
aspx.cs
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
gvMainLog.DataSource = GetSetupUtility(1);
gvMainLog.DataBind();
}
In the GridView add the remove command like this
<Columns>
<asp:BoundField DataField="Instruction" HeaderText="Instruction" />
<asp:BoundField DataField="ProviderId" HeaderText="Id" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Remove"
Text="Remove" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
In the action of the remove button use the DeleteRow method
void gvMainLog_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
var id = Int32.Parse(e.CommandArgument);
gvMainLog.DeleteRow(id);
}
}
You need somthing like this:
Use TemplateField in gridview.
<script>
function deleteRow(rowId) {
$("#" + rowId).remove();
}
</script>
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div id='<%# "myRow" + Container.DataItemIndex %>'> contents <img src="deleteImageUrl" onclick='<%# "deleteRow(\"myRow" + Container.DataItemIndex+"\")" %>'/> </div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Categories