I have problem in edit grid view using asp.net C#
when I clicked on edit link in the last column it is not showing me textboxes. however it shows update and cancel options!
enter image description here
this is my table
CREATE TABLE [dbo].[TbStudent] (
[StuId] INT IDENTITY (218234581, 1) NOT NULL,
[StuFirstName] NCHAR (10) NOT NULL,
[StuLastName] NCHAR (10) NOT NULL,
PRIMARY KEY CLUSTERED ([StuId] ASC)
);
this is the code
<asp:GridView ID="GridView1" runat="server" CellPadding="3" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" AutoGenerateColumns="False" Width="549px" ForeColor="Black" CssClass="auto-style3" ShowFooter="True" AllowPaging="True" OnRowEditing="GridView1_OnRowEditing" DataKeyNames="StuId" OnRowUpdating="GridView1_RowUpdating" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnCancel" runat="server"
CausesValidation="False"
CommandName="Cancel" Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkBtnInsert" runat="server"
CommandName="Insert">Add</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="lnkBtnDelete" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("StuLastNme") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuLName" placeholder="Last Name" runat="server" CssClass="auto-style16" Width="80px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("StuFirstNme") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuFName" placeholder="First Name" runat="server" CssClass="auto-style16" Width="80px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("StuId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuID" placeholder="Student Id" runat="server" CssClass="auto-style16" Width="80px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#E7E7FF" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF" />
<SelectedRowStyle BackColor="#E7E7FF" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#E7E7FF" />
<SortedAscendingHeaderStyle BackColor="#E7E7FF" />
<SortedDescendingCellStyle BackColor="#E7E7FF" />
<SortedDescendingHeaderStyle BackColor="#E7E7FF" />
</asp:GridView>
code behind
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Manage : System.Web.UI.Page
{
String myconnectionString = ConfigurationManager.ConnectionStrings["RigesterConnectionString1"].ConnectionString;
SqlConnection con;
SqlDataAdapter adapter;
DataSet ds;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowData();
}
}
private void insertStudent()//this function for insert new Student
{
}
protected void ShowData()
{
con = new SqlConnection(myconnectionString);
cmd = new SqlCommand("select * from TbStudent", con);
con.Open();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
}
else
{
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();
}
}
protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
You only have one EditItemTemplate element for the first TemplateField, you need an EditItemTemplate element for each TemplateField that you want to be editable, like this:
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" runat="server"
CausesValidation="True"
CommandName="Update" Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="lnkBtnCancel" runat="server"
CausesValidation="False"
CommandName="Cancel" Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkBtnInsert" runat="server"
CommandName="Insert">Add</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server"
CausesValidation="False"
CommandName="Edit" Text="Edit">
</asp:LinkButton>
<asp:LinkButton ID="lnkBtnDelete" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server"
Text='<%# Bind("StuLastNme") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuLName" placeholder="Last Name"
runat="server"
CssClass="auto-style16" Width="80px">
</asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
Put your textbox here...
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server"
Text='<%# Bind("StuFirstNme") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuFName" placeholder="First Name"
runat="server"
CssClass="auto-style16" Width="80px">
</asp:TextBox>
</FooterTemplate>
<EditTemplate>
Put your textbox here...
</EditTemplate>
</asp:TemplateField>
</Columns>
Related
I am trying to hide the Edit button when a student logs in and show it when the admin logs in. By default the edit button is visible. I want to hide the edit button on Page_Load. I have tried wrapping it in a div but it does not work for some reason. Any solutions??
GRIDVIEW CODE FOR .ASPX FILE:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1224px" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("SrNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Profile">
<ItemTemplate>
<asp:Label ID="lbl_Profile" runat="server" Text='<%#Eval("Profile") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Profile" runat="server" Text='<%#Eval("Profile") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CTC">
<ItemTemplate>
<asp:Label ID="lbl_CTC" runat="server" Text='<%#Eval("CTC") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_CTC" runat="server" Text='<%#Eval("CTC") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="InterOrFT">
<ItemTemplate>
<asp:Label ID="lbl_InternOrFT" runat="server" Text='<%#Eval("InternOrFT") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_InternOrFT" runat="server" Text='<%#Eval("InternOrFT") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="lbl_Location" runat="server" Text='<%#Eval("Location") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Location" runat="server" Text='<%#Eval("Location") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
CODE FOR .ASPX.CS FILE:
protected void Page_Load(object sender, EventArgs e)
{
if(Session["user"] == null)
{
Response.Redirect("~/login.aspx");
}
else
{
if (Session["user"].ToString() != "admin")
{
addForm.Visible = false;
}
}
linkProfile.Text = Session["user"].ToString();
if (!IsPostBack)
{
GVBind();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("insert into tblUpcoming values(#name, #profile, #ctc, #internFT, #location)", con);
cmd.Parameters.AddWithValue("#name", txtName.Text);
cmd.Parameters.AddWithValue("#profile", txtProfile.Text);
cmd.Parameters.AddWithValue("#ctc", txtCTC.Text);
cmd.Parameters.AddWithValue("#internFT", txtInternFT.Text);
cmd.Parameters.AddWithValue("#location", txtLocation.Text);
cmd.ExecuteNonQuery();
con.Close();
GVBind();
clear();
}
void GVBind()
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select compID as SrNo, name as Name, profile as Profile, internFT as InternOrFT, ctc as CTC, location as Location from tblUpcoming", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Read the session value directly at the aspx-file
<asp:Button ID="btn_Edit" runat="server" Text="Edit"
CommandName="Edit" Visible='<% Session["user"] == "admin" %>' />
also see ASP.NET "special" tags
can anyone tell me what i am missing why my row editing event is not firing.
I am trying to give edit facility in same grid as well as Add new data.
I am able to add new data (new row) but unable to edit existing row for update.
my row editing event is not firing.
<asp:GridView ID="grdHierarchy" runat="server" AllowSorting="True" AutoGenerateColumns="False" CssClass="tableGridBorder" GridLines="None" ShowFooter="True" ShowHeaderWhenEmpty="True" Width="100%" OnRowCommand="grdHierarchy_RowCommand" OnRowDataBound="grdHierarchy_RowDataBound"
OnRowEditing="grdHierarchy_RowEditing">
<AlternatingRowStyle CssClass="AlternateRow" />
<Columns>
<asp:TemplateField HeaderText="Sr.#">
<ItemTemplate>
<asp:Label ID="lblSrNo" runat="server" Text="<%# Container.DataItemIndex + 1 %>"></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="textAlignCenter" />
<HeaderStyle CssClass="textAlignCenter" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RequiredFieldValidator ID="rfvHierarchyName" runat="server" SetFocusOnError="True" ControlToValidate="txtHierarchyName" ErrorMessage="Enter Reference." ValidationGroup="grpHierarchy">
<asp:TextBox ID="txtHierarchyName" CssClass="textBoxSmall" Style="visibility: visible" runat="server" Text='<%# Bind("Name") %>' MaxLength="50"></asp:TextBox>
</asp:RequiredFieldValidator>
<span style="color: Red;">*</span>
<asp:RegularExpressionValidator ID="regexValiator3" runat="server" ControlToValidate="txtAPReference" ErrorMessage='Invalid Reference.' Display="None" ValidationGroup="grpHierarchy" ValidationExpression='([^<>\"\^])*'>
</asp:RegularExpressionValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:RequiredFieldValidator ID="rfvHierarchyName" runat="server" SetFocusOnError="True" ControlToValidate="txtHierarchyName" ErrorMessage="Enter Reference." ValidationGroup="grpfHierarchy">
<asp:TextBox ID="txtHierarchyName" CssClass="textBoxSmall" Style="visibility: visible" runat="server" Text='<%# Bind("Name") %>' MaxLength="50"></asp:TextBox>
</asp:RequiredFieldValidator>
<span style="color: Red;">*</span>
<asp:RegularExpressionValidator ID="regexValiator4" Display="None" runat="server" ControlToValidate="txtHierarchyName" ErrorMessage='Invalid Reference.' ValidationGroup="grpfHierarchy" ValidationExpression='([^<>\"\^])*'>
</asp:RegularExpressionValidator>
</FooterTemplate>
<ItemStyle CssClass="textAlignLeft" />
<HeaderStyle CssClass="textAlignLeft" />
<FooterStyle CssClass="textAlignLeft" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LevelName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Level.Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RequiredFieldValidator ID="rfvLevelName" runat="server" SetFocusOnError="True" ControlToValidate="ddlLevelName" ErrorMessage="Select Level Number." Width="100px" CssClass="redborderlarge" ValidationGroup="grpHierarchy" InitialValue="--Select--">
<asp:DropDownList ID="ddlLevelName" Width="200px" Style="visibility: visible" runat="server">
</asp:DropDownList>
</asp:RequiredFieldValidator><span style="color: Red; margin-top: 4px;">*</span>
</EditItemTemplate>
<FooterTemplate>
<asp:RequiredFieldValidator ID="rfvCustomerName" runat="server" SetFocusOnError="True" ControlToValidate="ddlLevelName" ErrorMessage="Select Customer." ValidationGroup="grpfHierarchy" InitialValue="--Select--">
<asp:DropDownList ID="ddlLevelName" Width="200px" Style="visibility: visible" runat="server">
</asp:DropDownList>
</asp:RequiredFieldValidator><span style="color: Red;">*</span>
</FooterTemplate>
<ItemStyle CssClass="textAlignLeft" />
<HeaderStyle CssClass="textAlignLeft" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="btnHierarchyEdit" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnHierarchyUpdate" runat="server" CommandName="Select" Text="Update" CausesValidation="true" ValidationGroup="grpfHierarchy" OnClick="btnHierarchyUpdate_Click" OnClientClick="return ToggleCursor2(1,'grpAP',true);"></asp:LinkButton>
<asp:LinkButton ID="btnHierarchyCancel" runat="server" CausesValidation="false" CommandName="Select" Text="Cancel" OnClick="btnHierarchyCancel_Click"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnHierarchyAdd" runat="server" CausesValidation="true" ValidationGroup="grpfHierarchy" CommandName="New" Text="Add" OnClientClick="return true;"></asp:LinkButton>
</FooterTemplate>
<ItemStyle CssClass="textAlignCenter" />
<HeaderStyle CssClass="textAlignCenter" />
<FooterStyle CssClass="textAlignCenter" />
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="Header" />
<RowStyle CssClass="Row" />
<FooterStyle CssClass="Header borderBttm" />
</asp:GridView>
and my code behind code is this event is not firing.where I am wrong
protected void grdHierarchy_RowEditing(object sender, GridViewEditEventArgs e)
{
grdHierarchy.EditIndex = e.NewEditIndex;
grdHierarchy.ShowFooter = false;
}
protected void grdHierarchy_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("New"))
{
Hierarchy Hierarchy = new Hierarchy();
TextBox txtHierarchyName = (TextBox)grdHierarchy.FooterRow.FindControl("txtHierarchyName");
DropDownList ddlLevelName = (DropDownList)grdHierarchy.FooterRow.FindControl("ddlLevelName");
string HierarchyName = txtHierarchyName.Text.Trim();
bool IsNameExist = Hierarchy.IsNameExist(Session, HierarchyName);
if (IsNameExist == false)
{
Hierarchy.Id = 0;
Hierarchy.Name = HierarchyName;
Hierarchy.LevelId = Convert.ToInt64(ddlLevelName.SelectedValue);
Hierarchy.CreatedDtTm = DateTime.Now;
Hierarchy.ModifiedDtTm = DateTime.Now;
Hierarchy.Save(Session);
fillHierarchyGrid();
}
else
{
cGlobalUI.showPopupMsg(Page, "Hierarchy Name is already Exist", cGlobalUI.MessageType.mtInformation);
}
}
}
Struggling to edit a Gridview Row. Once I hit edit the gridview disappears, I guess because I only bind the grid on !IsPostBack - although the tutorial tells me to do it this way.
I have tried rebinding on postback which lets me edit the gridview but keeps the default values - not the new values.
Any help appreciated.
if (!IsPostBack)
{
if (Session["Order_ID"] != null)
{
showgrid(int.Parse(Session["Order_ID"].ToString()));
}
}
public void showgrid(int Order_ID)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbConn"].ToString());
string sqlstring = "Removed - is working";
SqlCommand cmd = new SqlCommand(sqlstring, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gv_ExistingAds.DataSource = ds;
gv_ExistingAds.DataBind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
gv_ExistingAds.EditIndex = e.NewEditIndex;
showgrid(int.Parse(Session["Order_ID"].ToString()));
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lb = (Label)gv_ExistingAds.Rows[e.RowIndex].FindControl("Label3");
TextBox tx1 = (TextBox)gv_ExistingAds.Rows[e.RowIndex].FindControl("TextBox3");
string tx11 = tx1.Text;
gv_ExistingAds.EditIndex = -1;
showgrid(int.Parse(Session["Order_ID"].ToString()));
}
HTML
<asp:GridView runat="server" AutoGenerateColumns="false" ShowFooter="True" ShowHeaderWhenEmpty="false" EmptyDataText="This order has no ads..."
ID="gv_ExistingAds" CssClass="table table-striped table-bordered table-hover"
Visible="true" DataKeyNames="Orders_Editions_ID, Cancelled, Order_ID" EnableViewState="true" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField HeaderText="Publication Title" HtmlEncode="false" DataField="Publication_Title" />
<asp:BoundField HeaderText="Insertion Date" HtmlEncode="false" DataField="Insertion_Date" />
<asp:BoundField HeaderText="Advert Type" HtmlEncode="false" DataField="Description" />
<asp:BoundField DataField="Ad_Size" HeaderText="Size" ItemStyle-Width="150" />
<asp:BoundField DataField="Position" HeaderText="Position" ItemStyle-Width="150" />
<asp:BoundField DataField="Revenue" HeaderText="Revenue" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Note" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Note") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("Note") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemStyle Width="165px" />
<ItemTemplate>
<%--<asp:LinkButton ID="btn_UpdateAdvert" ControlStyle-CssClass="btn btn-primary btn-xs" CommandName="Update_Advert" CommandArgument='<%#Eval("Orders_Editions_ID")%>' runat="server" Text="Update" CausesValidation="false" />--%>
<asp:LinkButton ID="btn_CancelAdvert" ControlStyle-CssClass="btn btn-warning btn-xs" CommandName="Cancel_Advert" CommandArgument='<%#Eval("Orders_Editions_ID")%>' runat="server" Text="Cancel" CausesValidation="false" />
<asp:LinkButton ID="btn_DeleteAdvert" ControlStyle-CssClass="btn btn-danger btn-xs" CommandName="Delete_Advert" CommandArgument='<%#Eval("Orders_Editions_ID")%>' runat="server" Text="Delete" CausesValidation="false" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#FF6699" />
</asp:GridView>
when i have a gridview, after the user press the delete button, i will like to know that the particular row data in this case, how i get the particular row data?
Sample gridview:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" DataKeyNames="addCartID"
BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None" OnRowDeleted="Delete_Event">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:TemplateField HeaderText="addCartID" InsertVisible="False"
SortExpression="addCartID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("addCartID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("addCartID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="productID" SortExpression="productID">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("productID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("productID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="custID" SortExpression="custID">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("custID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("custID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="seatOrderID" SortExpression="seatOrderID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("seatOrderID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("seatOrderID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
Here is the c# code i wanna get the delete value, after user click delete i wish to update something based on the seatOrderID deleted, how do i get it? tq~~
protected void updateBack()
{
Response.Write(I want to get the deleted seatOrderID here!!!!!);
}
Using this code, you can fetch the ID in the RowDeleting event:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow gvr = (GridViewRow)GridView1.Rows[e.RowIndex];
string id = String.Empty;
Label lbl = (gvr.FindControl("Label1") as Label)
if (lbl != null)
{
id = lbl.Text;
}
}
You can fetch any control in the row, using the FindControl() method.
I wish to attach a hovermenu pop up to rows of a gridview which is dynamically populated from a datasource. I tried to set TargetcontrolId property of hovermenuextender control to row unique Id on occurence of rowdatabound event. But rather than appearing on the right side of each row, pop up is appearing on the right side of header row. Can somebody help me resolve this problem? I used below code for the same
CodeBehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HoverMenuExtender hoverMenu =(HoverMenuExtender)e.Row.FindControl("hme2");
if (hoverMenu != null)
{
hoverMenu.TargetControlID = hoverMenu.Parent.Parent.UniqueID;
}
}
}
}
Aspx page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" ShowFooter="false" ShowHeader="false"
OnRowEditing="GridView1_RowEditing" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating"
GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText ="Talk Title">
<ItemTemplate>
<asp:Label Font-Bold="true" ID="lbltalktitle" runat="server" Text='<%#Eval("Talk_Title") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Brand">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%# Eval("Brand") %>' /></td>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Franchise">
<ItemTemplate>
<asp:Label ID="lblFranchise" runat="server" Text='<%# Eval("Franchise") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="programmetype">
<ItemTemplate>
<asp:Label ID="lblprg" runat="server" Text='<%# Eval("programmetype") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="programmetype">
<ItemTemplate>
<asp:Label ID="lblsaledforce" runat="server" Text='<%# Eval("salesforce") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Start_Date">
<ItemTemplate>
<asp:Label ID="lblstartdate" runat="server" Text='<%# Eval("Start_Date") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="End_Date">
<ItemTemplate>
<asp:Label ID="lblenddate" runat="server" Text='<%# Eval("End_Date") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<cc1:HoverMenuExtender ID="hme2" runat="server" TargetControlID="lblenddate"
PopupControlID="PopupMenuX" HoverCssClass="popupHover" PopupPosition="Right"/>
<asp:Panel CssClass="popupMenu" ID="PopupMenuX" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit" /><br />
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete" />
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle ForeColor="Black" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
AjaxControlToolkit.HoverMenuExtender ajxhovermenu = (AjaxControlToolkit.HoverMenuExtender)e.Row.FindControl("ahm_1");
e.Row.ID = e.Row.RowIndex.ToString();
ajxhovermenu.TargetControlID = e.Row.ID;
}
}
Give TargetControlID of the HoverMenuExtender in RowCreated event of GridView and set it as the RowID