I have CodeBehind
protected void gvAll_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
KB.DeleteConstant(e.Keys[e.RowIndex]);
gvMaster.DataBind();
}
protected void gvMaster_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
Session["MasterBound"] = false;
gvMaster.DataBind();
}
and aspx
onrowdeleted="gvMaster_RowDeleted"
onrowdeleting="gvAll_RowDeleting">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"/>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"/>
</ItemTemplate>
Can someone explain me why onitemdeleted event does not raising when I click Delete?
Related
I have a problem with asp.net gridviewpager. When i click pager any page it routes to OnRowCommand event. I am waiting to route OnPageIndexChanging
I have define my gridview like this.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id"
GridLines="None" PageSize="4" AllowPaging="True" EmptyDataText="No record found"
OnPageIndexChanging="OnPaging" OnRowDeleting="GridView1_RowDeleting" OnRowCommand="RowCommand"
CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Baslik" HeaderText="Baslik" />
<asp:BoundField DataField="KisaAciklama" HeaderText="Kısa Acıklama" />
<asp:TemplateField HeaderText="Güncelle">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("Id") %>' CommandName="VIEW">Güncelle</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sil">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkDelete" CommandArgument='<%#Eval("Id") %>'
CommandName="DELETE">Sil</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%----%>
</Columns>
</asp:GridView>
Also my back-end code is
protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnkView = (LinkButton)e.CommandSource;
string Id = lnkView.CommandArgument;
if (e.CommandName == "VIEW")
{
Response.Redirect("/Views/AdminPages/Kayit.aspx?cmd=Update&id="+Id);
}
else if (e.CommandName == "DELETE")
{
kayitService.DeleteKayit(Convert.ToInt32(Id));
}
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
Please try this
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
onpageindexchanging="gvPerson_PageIndexChanging"
onrowcancelingedit="gvPerson_RowCancelingEdit"
onrowdatabound="gvPerson_RowDataBound" onrowdeleting="gvPerson_RowDeleting"
onrowediting="gvPerson_RowEditing" onrowupdating="gvPerson_RowUpdating"
onsorting="gvPerson_Sorting">
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Baslik" HeaderText="Baslik" />
<asp:BoundField DataField="KisaAciklama" HeaderText="Kısa Acıklama" />
<asp:TemplateField HeaderText="Güncelle">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkView" CommandArgument='<%#Eval("Id") %>' CommandName="VIEW">Güncelle</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sil">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkDelete" CommandArgument='<%#Eval("Id") %>'
CommandName="DELETE">Sil</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<%----%>
</Columns>
Back-end code may be :
protected void gvPerson_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// Set the index of the new display page.
Gridview1.PageIndex = e.NewPageIndex;
// Rebind the GridView control to
// show data in the new page.
BindGridView();
}
According to my opinion the code which I mentioned above is the most appropriate code for Paging.
I have a gridview which have some columns. I have made name column as a hyperlink.
I have a table named- 'tblAdd'. On Page load event I made it invisible. I want that when I click column hyperlink, table display.
How can I do this using asp.net ?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Name"
DataSourceID="SqlDataSource1" OnCheckedChanged="sellectAll"
>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="sellectAll" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name"
SortExpression="Name" >
<ItemTemplate>
<asp:HyperLink ID="linkName" runat="server" Text='<%#Bind("Name") %>' OnClick="displayTutorial_Click" NavigateUrl='#'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
My Default.aspx.cs-
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
GridView1.Columns[2].Visible = false;
//GridView1.DataBind();
if (!Page.IsPostBack)
{
fillLanguageGrid();
tblAdd.Visible = false;
}
}
Do it using Grid View row command event.
Do something like this-
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//To get the Selected link text field on textbox
if (e.CommandName == "displayLink")
{
txtEditName.Text=((LinkButton)e.CommandSource).Text;
}
}
On default.aspx-
<asp:TemplateField HeaderText="Name" SortExpression="Name" >
<ItemTemplate>
<asp:LinkButton ID="linkName" runat="server" Text='<%#Bind("Name")%>' OnClick="linkBtn_Click" CommandName="displayLink"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Button btnAddrecord = (Button)sender;
GridViewRow gvr =(GridViewRow)btnAddrecord.NamingContainer;
if (btnAddrecord.CommandName == "onAddMaterial")
Define the button in your grid view markup and assign a CommandName value to the button, like this:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Add Record">
<ItemTemplate>
<asp:Button ID="btnAddRecord"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
CommandName="AddRecord" runat="server" Text="Add" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now in your code-behind, you can handle the OnRowCommand event and AddRecord command, like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddRecord")
{
// Get index of row passed as command argument
int index = Convert.ToInt32(e.CommandArgument.ToString());
// Your logic here
}
}
1.Give a command name for button..
2.Then inside gridview
if e.commandname="yourcommandname"
{
//your code..
}
<!--We use onrowcommand for getting the selected row -->
<asp:GridView runat="server" ID="gvTest" AutoGenerateColumns="False" OnRowCommand="gvTest_OnRowCommand" >
<Columns>
<asp:TemplateField HeaderText="BookId" >
<ItemTemplate>
<asp:Label runat="server" ID="lblBookId" Text='<%# Bind("[BookId]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtBookId" Text='<%# Bind("[BookId]") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options">
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" CommandArgument="<%# Container.DisplayIndex %>" CommandName="IsDelete" Text="Delete"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
//Code Behind
protected void gvTest_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//getting rowindex which we have selected by using CommandArgument
int rowindex = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "IsDelete")
{
int bkid = gvTest.Rows[rowindex].Cells[0].FindControl("BookId");
//call a method to delete book using the bkid
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
I have a GridView like so:
<asp:GridView runat="server" ID="grdPractices" PageSize="10" AutoGenerateColumns="False" DataKeyNames="Id"
CssClass="linkGrid" AllowSorting="True" OnSorting="grdPractices_OnSorting" OnRowDataBound="grdPractices_OnRowDataBound"
OnRowEditing="grdPractices_OnRowEditing" OnRowCancelingEdit="grdPractices_OnRowCancelingEdit" OnRowUpdating="grdPractices_OnRowUpdating">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" meta:resourcekey="PracticeName" ReadOnly="True" SortExpression="Name" ItemStyle-Width="400px" />
<asp:BoundField DataField="Code" HeaderText="Code" meta:resourcekey="Code" ReadOnly="True" SortExpression="Code" ItemStyle-Width="200px" />
<asp:TemplateField meta:resourcekey="SiteName" ItemStyle-Width="200px" SortExpression="SiteName">
<ItemTemplate>
<asp:Literal runat="server" Text='<%# Eval("SiteName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="lstSites" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField meta:resourcekey="NumOfUsers" ItemStyle-Width="200px" SortExpression="NumOfUsers">
<ItemTemplate>
<asp:LinkButton runat="server" OnCommand="OnLinkButtonCommand" CommandName="ViewUsers" Text='<%# Eval("NumOfUsers") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="False" ShowCancelButton="True"
ShowInsertButton="False" ShowEditButton="True" EditText="Edit"
CancelText="Cancel" UpdateText="Update" meta:resourcekey="Edit" ItemStyle-Width="200px" />
</Columns>
</asp:GridView>
and here is the code for the OnRowEditing event:
protected void grdPractices_OnRowEditing(object sender, GridViewEditEventArgs e)
{
this.grdPractices.EditIndex = e.NewEditIndex;
var sitesDropDown = this.grdPractices.Rows[e.NewEditIndex].Controls[0].FindControl("lstSites") as DropDownList;
if (sitesDropDown == null)
{
return;
}
}
My problem is that I cannot get a hold of the lstSites control, which lives in the EditTemplate. I've tried using the following:
this.grdPractices.Rows[e.NewEditIndex].Controls[0].FindControl("lstSites")
as DropDownList;
this.grdPractices.Rows[e.NewEditIndex].FindControl("lstSites") as
DropDownList;
this.grdPractices.Rows[e.NewEditIndex].FindControlRecursive("lstSites")
as DropDownList;
The result is always the same, a NULL is returned.
How on earth are you supposed to get a control in a row when in the OnRowEditing event?
OK, what I was not doing, after this line:
this.grdPractices.EditIndex = e.NewEditIndex;
was then rebinding the grid's data. So after rebinding the data, and then calling:
var sitesDropDown = this.grdPractices.Rows[e.NewEditIndex].FindControlRecursive("lstSites") as DropDownList;
I am now able to interact with the siteDropDown variable since it now contains a reference to the lstSites control.
Try to use GridViewRow as following...
protected void grd_RowEditing(object sender, GridViewEditEventArgs e)
{
GridViewRow selectRow = grd.Rows(e.NewEditIndex);
DropDownList sitesDropDown =(DropDownList)selectRow.Cells[2].FindControl("lstSites");
}
Basically I have a GridView:
<asp:GridView ID="gvServices" runat="server" CellPadding="4"
ForeColor="#333333" GridLines="None" AllowSorting="True"
AutoGenerateColumns="False" OnRowCommand="gvServices_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
And inside I have 2 TemplateFields:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStart" runat="server" Text="Start" CommandName="StartService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStop" runat="server" Text="Stop" CommandName="StopService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
Finally I have the method thats supposed to fire when clicking on either of the Buttons on the gridView, problem is that when I click either of them the event does not get called at all
public void gvServices_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "StartService")
{
StartServiceItem(gvServices.Rows[index].Cells[0].Text, locations[index]);
}
if (e.CommandName == "StopService")
{
StopServiceItem(gvServices.Rows[index].Cells[0].Text, locations[index]);
}
loadGridView();
}
RowCommand on GridView does fire from a button within a TemplateField. Check out the answer to this thread:
Linkbutton inside a gridview not firing
You need to listen to the buttons RowCommand. GridView.RowCommand is used when you have an asp:ButtonField on each row.
Aspx code:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStart" runat="server" Text="Start" OnCommand="GridButtons_Command" CommandName="StartService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnStop" runat="server" Text="Stop" OnCommand="GridButtons_Command" CommandName="StopService" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
</ItemTemplate>
</asp:TemplateField>
Code Behind:
public void GridButtons_Command(object sender, CommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "StartService")
{
StartServiceItem(gvServices.Rows[index].Cells[0].Text, locations[index]);
}
if (e.CommandName == "StopService")
{
StopServiceItem(gvServices.Rows[index].Cells[0].Text, locations[index]);
}
loadGridView();
}