GridviewPager not route onpaging event - asp.net

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.

Related

ASP.NET GridView OnRowDeleted

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?

show table on gridview column click

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>

GridViewCosts.SelectedRow.Cells[1].Text dont work with itemTemplate

I have this gridView:
<div class ="gridView">
<asp:GridView ID="GridViewCosts" runat="server" ShowFooter="True" ShowHeaderWhenEmpty="True"
AutoGenerateColumns="False" OnRowDeleting="GridViewCosts_RowDeleting" Width="387px"
OnSelectedIndexChanged="GridViewCosts_SelectedIndexChanged"
OnPageIndexChanging="GridViewCosts_PageIndexChanging"
AllowPaging="True"
CssClass="mGrid"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" PageSize="5">
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Номер" />
<asp:TemplateField HeaderText="Стойност">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Value") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="100px" />
</asp:TemplateField>
And in code behind i Want :
protected void GridViewCosts_SelectedIndexChanged(object sender, EventArgs e)
{
TextBoxValue.Text = GridViewCosts.SelectedRow.Cells[1].Text;
}
this is not working when the column Value is TemplateField. If the column is BoundFIeld it's working . What should i do ?
If you use TemplateFields you have to use FindControl to get a reference to the control:
Label Label2 = (Label)GridViewCosts.SelectedRow.FindControl("Label2");
TextBoxValue.Text = Label2.Text;
Try this sample code
For Each gvr As GridViewRow In gvInvoice.Rows
Dim TctValue As TextBox= DirectCast(GridViewCosts.Cells(1).FindControl("TextBox2"), TextBox)
totamount = totamount + Convert.ToDouble(lblAmount.Text)
Next
txtTotal.Text = totamount.ToString()

Cannot find control in EditTemplate via OnRowEditing event

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");
}

How do I add a column with buttons in to a gridview in asp.net?

As my gridview is populating I want to add an extra column with some buttons in but I can't seem to figure out how, or what might be the best way. Can anyone get me started?
Use a Template Column
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_OnRowCommand">
<Columns>
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="SendMail"
Text="SendMail" CommandArgument='<%# Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName != "SendMail") return;
int id = Convert.ToInt32(e.CommandArgument);
// do something
}

Resources