Hide edit button on Page_Load - c#

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

Related

Edit Gridview Row ASP.net

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>

Text boxes in edit mode of grid view are not visible?

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>

Binding Shopping Cart Check box Session onto another Gridview

I am new to asp.net and I am trying to make a shopping cart. I have a shop set up that has a list of school supplies and a check box next to each one with a "Add to Cart" button on the bottom. I want to store only the checked items into a session and call it to another gridview on my Shopping Cart page. For some reason my gridview on my Shopping cart page is not appearing.
Shop.aspx
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Price" DataFormatString="{0:c2}" HeaderText="Price" SortExpression="Price" />
<asp:TemplateField HeaderText="Add To Cart">
<ItemTemplate>
<asp:CheckBox ID="cbAdd" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Table] ORDER BY [Id], [Name], [Price]"></asp:SqlDataSource>
</div>
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add To Cart" OnClick="btnAdd_Click" />
Shop.aspx.cs
protected void Page_Load(object sender, EventArgs e)//bind list to gridview
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();//create new datatable
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("ID"), new DataColumn("Name"), new DataColumn("Country") });
foreach (GridViewRow row in gvProducts.Rows)//for each row in the gridview
{
CheckBox ckRow = (CheckBox)row.FindControl("ckAdd");
if (ckRow !=null && ckRow.Checked)
{
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Price");
Session["ShoppingCart"] = dt;//store datatable as session called ShoppingCart
}
Response.Redirect("~/ShoppingCart.aspx");
}
ShoppingCart.aspx
<h1>Shopping Cart</h1>
<asp:GridView ID="gvProductsList" runat="server" AutoGenerateColumns="true" ViewStateMode="Enabled">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:Textbox ID="txtQuantity" runat="server" Text='<%# Bind("Count")%>'></asp:Textbox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Total">
<ItemTemplate>
<asp:Label ID="lblProductTotal" runat="server" Text='<%# Convert.ToInt32(Eval("Quantity"))*Convert.ToInt32(Eval("Price"))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Order Total: "></asp:Label>
</FooterTemplate>
</asp:TemplateField>
ShoppingCart.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = (DataTable)Session["ShoppingCart"];
gvProductsList.DataSource = dt;
gvProductsList.DataBind();
}
}
Try this:-
List<GridViewRow> gridcheckedrows = gvProducts.Rows.Cast<GridViewRow>().
Where(x => (x.FindControl("cbAdd") as CheckBox)
.Checked == true).ToList();
I used below custom data to bind the grid gvProducts
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Price");
DataRow dr = dt.NewRow();
dr[0] = "1";
dr[1] = "Sharique";
dr[2] = "120";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "2";
dr[1] = "Ansari";
dr[2] = "100";
dt.Rows.Add(dr);
gvProducts.DataSource = dt;
gvProducts.DataBind();
}
}
use this to bind you grid gvProductsList
List<GridViewRow> gridcheckedrows = gvProducts.Rows.Cast<GridViewRow>().Where(x => (x.FindControl("cbAdd") as CheckBox).Checked == true).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Price");
dt.Columns.Add("Count");
dt.Columns.Add("Quantity");
foreach (GridViewRow gvr in gridcheckedrows)
{
DataRow dr = dt.NewRow();
dr["Id"] = gvr.Cells[0].Text;
dr["Name"] = gvr.Cells[1].Text;
dr["Price"] = gvr.Cells[2].Text;
dr["Count"] = "1";
dr["Quantity"] = "2";
dt.Rows.Add(dr);
}
gvProductsList.DataSource = dt;
gvProductsList.DataBind();
and for grid gvProductsList use below html
<asp:GridView ID="gvProductsList" runat="server">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Count")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Total">
<ItemTemplate>
<asp:Label ID="lblProductTotal" runat="server" Text='<%# ((Convert.ToInt32(Eval("Quantity")))*(Convert.ToInt32(Eval("Price"))))%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server" Text="Order Total: "></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Hope this will help you.

how to get the delete row value if I wanna to delete it in gridview (web application)

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.

How to bind hovermenuextender control to a row with multiple columns in a dynamic gridview?

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

Categories