i have a rad grid with the following code
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="Delete" Text="Remove »"
CommandArgument='<%# Eval("ApartmentId") %>'
CommandName="RemoveItem" CssClass="Button" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
and .CS code as follows
protected void radGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Page")
{
}
else
{
if (e.CommandName == "RemoveItem")
{
Apartments apartAdmin = new Apartment();
bool deleted = apartAdmin.Delete(int.Parse(e.CommandArgument.ToString());
if (deleted)
{
radGrid.Rebind();
}
}
}
}
My problem is that when I debug it, say I add the breakpoint to this event, it is never fired, is like if it does not see the event for some reason... Can anyone see what the problem may be? This is the mark up of the grid at the top
<telerik:RadGrid ID="radGrid" ShowFooter="true" ShowHeader="true" CaptionAlign="Left"
runat="server" ForeColor="Black" CellPadding="4" AutoGenerateColumns="False"
CssClass="Grid" Width="100%" GridLines="None" OnRowCommand="radGrid_RowCommand"
OnNeedDataSource="radGrid_NeedDataSource" AllowPaging="True" AllowSorting="true">
<MasterTableView DataKeyNames="ApartmentID,ApartmentTypeID">
<CommandItemSettings ShowRefreshButton="true" ShowAddNewRecordButton="false" />
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="Delete" Text="Remove »"
CommandArgument='<%# Eval("ApartmentId") %>'
CommandName="RemoveItem" CssClass="Button" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<NoRecordsTemplate>
No related items found</NoRecordsTemplate>
</MasterTableView>
<FooterStyle CssClass="FooterStyle" />
<ItemStyle CssClass="RowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<AlternatingItemStyle CssClass="AlternatingRowStyle" />
<PagerStyle CssClass="PagerStyle" FirstPageText="First" LastPageText="Last" Mode="NextPrevAndNumeric"
AlwaysVisible="true" />
</telerik:RadGrid>
You have a linkbutton inside an itemtemplate, add a handler for OnClick on the linkbutton itself and the event will surely be fired.
For example:
protected void LinkButton1_Click(Object sender, EventArgs
{
LinkButton button = sender as LinkButton;
Apartments apartAdmin = new Apartment();
bool deleted = apartAdmin.Delete(int.Parse(button.CommandArgument.ToString());
if (deleted)
{
radGrid.Rebind();
}
}
Related
I am fairly new to Asp.Net. I have been trying to Edit a column value in a gridview but upon checking one of the two checkboxes (Yes or No), it does not update anything.
Sharing Gridview code below :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#000000" BorderStyle="Solid"
BorderWidth="2px" CellPadding="6" DataKeyNames="film_data" DataSourceID="SqlDataSource" ForeColor="White"
GridLines="Vertical" onrowdatabound="GridView1_RowDataBound" AllowSorting="True">
<AlternatingRowStyle BackColor="#CCCCC" />
<Columns>
<asp:CommandField ShowEditButton="True">
<ControlStyle ForeColor="Blue" />
</asp:CommandField>
<asp:BoundField DataField="film_title" HeaderText="MOVIE TITLE" ReadOnly="True" SortExpression="film_title" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="film_release" HeaderText="RELEASE DATE" ReadOnly="True" SortExpression="release_date" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:CheckboxField DataField="Yes" HeaderText="YES" SortExpression="Yes" />
<asp:CheckboxField DataField="No" HeaderText="NO" SortExpression="No" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle />
</asp:GridView>
This is the GridView1_RowDataBound snippet from .cs file.
public void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Checking the RowType of the Row
if (e.Row.RowType == DataControlRowType.DataRow)
{
....
What am I missing here ? Upon clicking Edit, and the Yes or No checkboxes. nothing gets updated. There is no error either. Kindly assist.
In Gridview change the commandfield and checkbox control like this in your code
<asp:TemplateField HeaderText="Yes/No">
<ItemTemplate>
<asp:Literal ID="ltrchkboxVal" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "chkboxval").ToString() %>'></asp:Literal>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnchkboxval" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "chkboxval").ToString() %>'/>
<asp:CheckboxList ID="chkyesboxval" runat="server" Width="40">
<asp:ListItem> Yes</asp:ListItem>
<asp:ListItem> No</asp:ListItem>
</asp:CheckboxList >
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" CancelText="Cancel" ControlStyle-CssClass="small"
EditText="Optimise" HeaderText="Optimise" ShowEditButton="true" UpdateText="Apply" ItemStyle-Width="30px">
</asp:CommandField>
IN c# code
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
DivMessage.Visible = false;
GridView1.EditIndex = e.NewEditIndex;
//Bind gridview
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string yesnoval = (GridView1.Rows[e.RowIndex].FindControl("chkyesboxval") as CheckBoxList).SelectedItem.Value;
// update new values and Bind gridview
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Bind gridview
}
Add events for the gridview control
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
I am using gridview to display of data from database, in each row of the gridview I am having delete and edit link button. How can I get value of “NAME” and "DESCRIPTION" when I click on Delete or Edit Button in gridview.
The following is my code.
List.aspx
<div align="center" style="margin-top:50px">
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" CellPadding="5" runat="server">
<Columns>
<asp:BoundField HeaderText="Report Name" DataField="NAME" />
<asp:BoundField HeaderText="Report Description" DataField="DESCRIPTION" />
<asp:BoundField HeaderText="Report Group" DataField="REPORT_GROUP" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" OnClick="Btn_Delete_Click" />
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
List.aspx.cs
protected void Btn_Delete_Click(object sender, EventArgs e)
{
}
use command argument like this
<ItemTemplate>
<asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandArguman='<%# Eval("Name")+","+Eval("DESCRIPTION") %>' OnClick="Btn_Delete_Click" />
<asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandArguman='<%# Eval("Name")+","+Eval("DESCRIPTION") %>' OnClick="btnEdit_Click" />
</ItemTemplate>
in onclick
protected void Btn_Delete_Click(object sender, EventArgs e)
{
string strName=((LinkButton)sender).CommandArgument.Split(',')[0];
string strDescription=((LinkButton)sender).CommandArgument.Split(',')[1];
}
protected void btnEdit_Click_Click(object sender, EventArgs e)
{
string strName=((LinkButton)sender).CommandArgument.Split(',')[0];
string strDescription=((LinkButton)sender).CommandArgument.Split(',')[1];
}
I have a Multi View with two views in it. View1 has a Grid View and the grid-view allow paging by ten records.
The problem is i have to press page number tow times to go to that page.
The first click nothing happen the second click the grid view goes to the page what is the issue
<asp:GridView ID="gridusers" AutoGenerateColumns="false" runat="server" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" PageSize="10" CssClass="table table-bordered text-nowrap" OnSelectedIndexChanged="gridusers_SelectedIndexChanged" OnRowDeleting="gridusers_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn-primary btn-xs" CausesValidation="False" CommandName="Select" Text="" ><i class="glyphicon glyphicon-pencil"></i></asp:LinkButton>
</ItemTemplate>
<controlstyle cssclass="btn btn-primary" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" OnClientClick="return confirm('Are you sure you want to delete this record ?');" CssClass="btn btn-primary btn-xs" CommandName="Delete" Text="Delete"><i class="glyphicon glyphicon-trash"></i></asp:LinkButton>
</ItemTemplate>
<controlstyle cssclass="btn btn-danger" />
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="Customer ID" SortExpression="Customer ID" />
<asp:BoundField DataField="Name" HeaderText="Customer Name" SortExpression="Name" />
<asp:BoundField DataField="Contact Person" HeaderText="Contact Person" SortExpression="Contact Person" />
<asp:BoundField DataField="P.O.Box" HeaderText="P.O.Box" SortExpression="P.O.Box" />
<asp:BoundField DataField="Address" HtmlEncodeFormatString="false" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Mobile No" HeaderText="Mobile No" SortExpression="Mobile No" />
</Columns>
<SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" />
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
CustomerClass c = new CustomerClass();
if (!this.IsPostBack)
{
gridusers.DataSource = c.getcst();
gridusers.DataBind();
}
}
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridusers.PageIndex = e.NewPageIndex;
this.c.getcst();
}
You are not rebinding the grid once the page number changes.
Your code is this:
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridusers.PageIndex = e.NewPageIndex;
this.c.getcst();
}
You need to do this instead:
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridusers.PageIndex = e.NewPageIndex;
gridusers.DataSource = c.getcst();
gridusers.DataBind();
}
recently I asked a similar question, which you can find in the link below
Using Dropdownlist in a gridview Asp.Net?
I have a gridview that looks like this...
<asp:GridView ID="grdvEventosVendedor" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DatakeyNames="idCita"
EmptyDataText="No Hay Eventos Para Este Vendedor" ForeColor="#333333"
GridLines="None" AllowSorting="True"
onpageindexchanging="grdvEventosVendedor_PageIndexChanging"
onrowcommand="grdvEventosVendedor_RowCommand"
onsorting="grdvEventosVendedor_Sorting" CellSpacing="1"
onrowdatabound="grdvEventosVendedor_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775"/>
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnEdicEvento" runat="server"
CommandArgument='<%# Eval("idCita")%>' CommandName="Edicion"
Height="32px" ImageUrl="~/img/pencil_32.png" Width="32px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="35px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnDelete" runat="server"
CommandName="Borrar"
ImageUrl="~/img/1385_Disable_16x16_72.png"
onclientclick="return confirm('¿Desea eliminar el registro?');"
CommandArgument='<%# Eval("idCita")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Cliente" HeaderText="Cliente" InsertVisible="False" ReadOnly="True" SortExpression="Cliente" ItemStyle-Width="50px" />
<asp:BoundField DataField="Empresa" HeaderText="Empresa" InsertVisible="False" ReadOnly="True" SortExpression="Empresa" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Telefono" HeaderText="Telefono" InsertVisible="False" ReadOnly="True" SortExpression="Telefono" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Nextel" HeaderText="Nextel" InsertVisible="False" ReadOnly="True" SortExpression="Nextel" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Tipo" HeaderText="Tipo" InsertVisible="False" ReadOnly="True" SortExpression="Tipo" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Descripcion" HeaderText="Descripcion" InsertVisible="False" ReadOnly="True" SortExpression="Descripcion" ItemStyle-Width="100px"/>
<asp:TemplateField HeaderText="Fecha" SortExpression="Fecha" ItemStyle-Width="50px">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Fecha", "{0:dd/MM/yyyy}")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="tbxFecha" runat="server" Text='<%#Bind("Fecha","{0:dd/MM/yyyy}") %>' ValidationGroup="gpEdicionAgenda">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="HoraInicio" HeaderText="Hora" InsertVisible="False" ReadOnly="True" SortExpression="HoraInicio" ItemStyle-Width="50px"/>
<asp:BoundField DataField="Lugar" HeaderText="Lugar" InsertVisible="False" ReadOnly="True" SortExpression="Lugar" ItemStyle-Width="50px"/>
<%--<asp:BoundField DataField="Estado" HeaderText="Estado" InsertVisible="False" ReadOnly="True" SortExpression="Estado" ItemStyle-Width="50px"/>--%>
<asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
<ItemTemplate>
<asp:DropDownList ID="dpdListEstado" runat="server" OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>Pendiente</asp:ListItem>
<asp:ListItem>Atendido</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CRM" ItemStyle-Width="25px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnCRM" runat="server"
CommandArgument='<%# Eval("IdCliente")%>' CommandName="CRM"
ImageUrl="~/img/activar.png" Width="16px" Height="16px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VM" ItemStyle-Width="25px">
<ItemTemplate>
<asp:ImageButton ID="imgBtnVerMas" runat="server"
CommandArgument='<%# Eval("IdCliente")%>' CommandName="VerMas"
ImageUrl="~/img/search.png" Width="16px" Height="16px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" Font-Size="Small" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Font-Size="Larger" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" Font-Size="Small" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
As mentioned in the link, I had to manually add some code, like OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True" and also add this on the cs file...
protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
{
}
I have tried it and it did work, whenever I change the value of a ddl, it does enter to the void int the cs file...
however now I have a question...
I need the Id from the record that it's stored in a specific row (idCita, as in the buttons I have which use a CommandArgument)... that way can use another class I have which would trigger a query that would edit a field called Estado....
However, I noticed that when I use CommandArguments, I use another void called...
protected void grdvEventosVendedor_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intIdCita = int.Parse(e.CommandArgument.ToString());
if (e.CommandName == "Edicion")
{
//Some Code
}
else if (e.CommandName == "Borrar")
{
//More Code
}
else if (e.CommandName == "CRM")
{
//Even More Code
}
else if (e.CommandName == "VerMas")
{
//....
}
}
with this I can call both e.CommandName which tells me which button I pressed, and e.CommandArgument which is the value for idCita... however I noticed that the ddl void that I previously showed you doesnt have a GridViewCommandEventArgs e, instead it has a EventArgs e...
Is there a way that I can get the value of idCita from the gridview, all the way to the ddl void??
What do I need to add in the gridview code and in the cs file??
Thanks
Refactor you're code something like this - it should get you most of the way there, additionally look at converting the else if statements to a more readable switch case statement...
protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);
MeaningfulNameHere(int.Parse(grdvEventosVendedor.DataKeys[gvr.RowIndex]),"Estado");
}
protected void grdvEventosVendedor_RowCommand(object sender, GridViewCommandEventArgs e)
{
MeaningfulNameHere(int.Parse(e.CommandArgument.ToString()),e.CommandName);
}
private void MeaningfulNameHere(int id, string commandName)
{
if (commandName == "Edicion")
{
//Some Code
}
else if (commandName == "Borrar")
{
//More Code
}
else if (commandName == "CRM")
{
//Even More Code
}
else if (commandName == "VerMas")
{
//....
}
else if (commandName == "Estado")
{
}
}
I have a gridview like this :
<asp:GridView ID="gvProducts" runat="server" AutoGenerateEditButton="True" AutoGenerateColumns="False"
OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating" CellPadding="4"
ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblPID" runat="server" Text="Product ID"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblPName" runat="server" Text="Product Name"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
and here is the code behind page
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
{
gvProducts.EditIndex = e.NewEditIndex;
}
protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int i = e.RowIndex;
object control = gvProducts.Rows[i].FindControl("txtProdID");
//i want to access the new value from the object "control" but i m getting the previous value only
}
try this
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtProdID = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdID");
TextBox txtProdName = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdName");
//Call update method
Product.Update(txtProdId,txtProdName);
gvProducts.EditIndex = -1;
//Refresh the gridviwe
BindGrid();
}
You need to check the e.NewValues dictionary for updated data.
I've a sample below, the GridView Template is bound to CategoryName.
OnRowUpdating is fired when the 'Edit' button is clicked.
In the RowUpdating event handler, it fetches the CategoryName data to which the textbox is bound to.
Note: Instead of using TextBox in normal mode use Labels.
<asp:GridView ID='GridView1' runat='server' DataKeyNames='CategoryID' OnRowUpdating='HandleOnGridViewRowUpdating'
DataSourceID='ObjectDataSource1' AutoGenerateColumns='false'>
<Columns>
<asp:CommandField ShowEditButton="true" />
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID='CategoryName' Text='Category' runat='server'></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID='CategoryNameTextbox' Text='<%# Bind("CategoryName") %>' runat='server'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind:
public void HandleOnGridViewRowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (e.NewValues["CategoryName"] != null)
{
String newCategoryName = e.NewValues["CategoryName"].ToString();
// process the data;
}
}