my aspx code is:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="Item_id"
DataSourceID="SqlDataSource2" ShowFooter="True"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Item_id" InsertVisible="False"
SortExpression="Item_id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Item_id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Item_id") %>'></asp:Label>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="BtnInsert" runat="server" Text="Insert"
Font-Bold="true" BackColor="#0099CC" ForeColor="white" Height="30px" Width="100px" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category_name" SortExpression="Category_name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Category_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Category_name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbC" runat="server" Height="20px" Width="120px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="item_name" SortExpression="item_name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("item_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("item_name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbN" runat="server" Height="20px" Width="120px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="unit_price" SortExpression="unit_price">
<ItemTemplate>
<asp:TextBox ID="TbPrice" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbP" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="QuantitySelected"
SortExpression="QuantitySelected">
<ItemTemplate>
<asp:Textbox ID="TbQt" runat="server" Text='<%# Bind("QuantitySelected") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server"
Text='<%# Bind("QuantitySelected") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbQ" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Total" SortExpression="Total">
<ItemTemplate>
<asp:Textbox ID="TbTl" runat="server" Text='<%# Bind("Total") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Total") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbT" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount" SortExpression="Discount">
<ItemTemplate>
<asp:Textbox ID="TbD" runat="server" Text='<%# Bind("Discount") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Discount") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbD" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Grand_total" SortExpression="Grand_total">
<ItemTemplate>
<asp:Textbox ID="TbGtl" runat="server" Text='<%# Bind("Grand_total") %>'></asp:Textbox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Grand_total") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TbG" runat="server" Height="20px" Width="100px" Font-Bold="true"></asp:TextBox>
</FooterTemplate>
<ItemStyle Width ="100px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
my code behind is:
public partial class ItemPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
protected void btn_Click(object sender, EventArgs e)
{
SqlDataSource2.InsertParameters["Category_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbC")).Text;
SqlDataSource2.InsertParameters["item_name"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbN")).Text;
SqlDataSource2.InsertParameters["unit_price"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbP")).Text;
SqlDataSource2.InsertParameters["QuantitySelected"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbQ")).Text;
SqlDataSource2.InsertParameters["Total"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbT")).Text;
SqlDataSource2.InsertParameters["Discount"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbD")).Text;
SqlDataSource2.InsertParameters["Grand_total"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("TbG")).Text;
}
double price = 0.0f;
double quantity = 0.0f;
double total = 0.0f;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox tb11 = (TextBox)e.Row.FindControl("Tb2");
TextBox tb12 = (TextBox)e.Row.FindControl("Tb3");
TextBox tb13 = (TextBox)e.Row.FindControl("TbPrice");
TextBox tb14 = (TextBox)e.Row.FindControl("TbQt");
TextBox tb15 = (TextBox)e.Row.FindControl("TbTl");
TextBox tb16 = (TextBox)e.Row.FindControl("TbDs");
TextBox tb17 = (TextBox)e.Row.FindControl("TbGtl");
price = Convert.ToDouble(((TextBox)e.Row.FindControl("TbPrice")).Text);
quantity = Convert.ToDouble(((TextBox)e.Row.FindControl("TbQt")).Text);
total = price * quantity;
//total += total;
tb15.Text = total.ToString();
}
else
{
if (e.Row.RowType == DataControlRowType.Footer)
{
TextBox tb1 = (TextBox)e.Row.FindControl("TbC");
TextBox tb2 = (TextBox)e.Row.FindControl("TbN");
TextBox tb3 = (TextBox)e.Row.FindControl("TbP");
TextBox tb4 = (TextBox)e.Row.FindControl("TbQ");
TextBox tb5 = (TextBox)e.Row.FindControl("TbT");
TextBox tb6 = (TextBox)e.Row.FindControl("TbD");
TextBox tb7 = (TextBox)e.Row.FindControl("TbG");
tb5.Text = total.ToString();
}
}
}
}
}
i hv posted my edit code again. the problem is when i run the page i am getting blank textboxes in my footer. only textbox in "total" column has vales and that too is some vague value.. i have to put values in "price" "quantity" and then click "insert" button to add values into grid along with calculated value in "total" column.. i am still stuck!! plz help!!
you also need this:
if (e.Row.RowType == DataControlRowType.DataRow)
where you do the summation.
Note: the variables need to be declared outside the event handler or they will be zeroed on each iteration.
EDIT- zero out the summation variable when e.Row.RowType == DataControlRowType.Header
EDIT Do something like this:
double grand_total = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
grand_total = 0;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
double price = Convert.ToDouble(((TextBox)e.Row.FindControl("TbP")).Text);
double quantity = Convert.ToDouble(((TextBox)e.Row.FindControl("TbQ")).Text);
double total = price * quantity ;
((TextBox)e.Row.FindControl("TbT")).Text = total.ToString();
grand_total += total;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
((TextBox)e.Row.FindControl("TbT")).Text = grand_total.ToString() ;
}
}
Related
There is a radiobuttonlist in a gridview. I want to do something with the selectedvalue of which radiobuttonlist's selectedindexchanged.
But it's giving the old value always. For example the first item is selected when page load. If I click the second item, its still giving the first item's value.
My codes, in aspx file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="cezaDS" >
<Columns>
<asp:TemplateField HeaderText="Öğrenci Bilgileri" SortExpression="SINIF">
<ItemTemplate>
<asp:Label ID="lbSiraNo" Visible="false" runat="server" Text='<%# Eval("SIRA_NO") %>'></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ADI_SOYADI") %>'></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text='<%# Bind("OKUL_NO") %>'></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text='<%# Bind("BOLUM") %>'></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text='<%# Bind("SINIF") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ceza Türü ve Aldığı Zaman" SortExpression="DONEM">
<ItemTemplate>
Ceza Türü: <b><asp:Label ID="Label7" runat="server" Text='<%# Bind("CEZA_TURU") %>'></asp:Label></b><br />
Cezayı Aldığı Sınıf: <b><asp:Label ID="Label6" runat="server" Text='<%# Bind("CEZA_ALDIGI_SINIF") %>'></asp:Label></b><br />
Cezayı Aldığı Öğretim Yılı: <b><asp:Label ID="Label5" runat="server" Text='<%# Bind("DONEM") %>'></asp:Label></b>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ILGILI_MADDE" HeaderText="İlgili Madde" SortExpression="ILGILI_MADDE" />
<asp:TemplateField HeaderText="Kararınız">
<ItemTemplate>
<asp:RadioButtonList ID="rdList" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="rdList_SelectedIndexChanged">
<asp:ListItem Value="Kaldırılsın">Kaldırılsın</asp:ListItem>
<asp:ListItem Value="Kaldırılmasın">Kaldırılmasın</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In cs file:
protected void rdList_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)(((RadioButtonList)sender).NamingContainer);
Label cezaSiraNo = (Label)(row.FindControl("lbSiraNo"));
RadioButtonList clickedList= (RadioButtonList)sender;
string x = clickedList.SelectedValue;
//...
Here x variable is taking the wrong value.
For some reason my gridview is not showing up. On a debug I get rows and columns from the database when I check Results. I'm using a view to get the fields I need. I tried setting AutoGenerateColumns to true, but no luck. Am I missing something?
My aspx page:
<asp:Content ID="ContentMain" ContentPlaceHolderID="cphMain" runat="Server">
<asp:Panel ClientIDMode="Static" ID="meritData" runat="server" Visible="false" CssClass="dataTable">
<h5>Faculty Teaching Assignments</h5>
<p id="pMeritMsg" runat="server" class="userMsg"></p>
<asp:GridView ID="gridFTA" runat="server" AutoGenerateColumns="false"
ShowHeaderWhenEmpty="true"
ShowFooter="true"
DataKeyNames="FacultyId">
<Columns>
<asp:TemplateField HeaderText="Faculty Name">
<ItemTemplate>
<asp:Label ID="txtFacultyName" runat="server" Text='<%#Bind("FacultyName") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblFacultyName" runat="server" width="40px" Text='<%#Bind("FacultyName") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inFacultyName" width="140px" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number Of Courses">
<ItemTemplate>
<asp:Label ID="lblNumberOfCourses" runat="server" Text='<%#Bind("NumberOfCourses") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNumberOfCourses" width="70px" runat="server" Text='<%#Bind("NumberOfCourses") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inNumberOfCourses" width="120px" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Adjustment">
<ItemTemplate>
<asp:Label ID="lblAdjustment" runat="server" Text='<%#Bind("Adjustment") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblAdjustment" runat="server" visible = "false" Text='<%#Bind("Adjustment") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inAdjustment" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reason">
<ItemTemplate>
<asp:Label ID="lblReason" runat="server" Text='<%#Bind("Reason") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblReason" runat="server" visible = "false" Text='<%#Bind("Reason") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inReason" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:Label ID="lblCourse" runat="server" Text='<%#Bind("CourseNumber") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblCourse" runat="server" visible = "false" Text='<%#Bind("CourseNumber") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inCourse" width="50" runat="server" >
</asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="ButtonEdit" runat="server" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" CommandName="AddNew" Text="Add New Faculty Teaching Assignment" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
</asp:Content>
My code behind:
protected clsData moData = new clsData();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpContext.Current.Session["FTA"] = "";
BindData("");
}
}
protected void BindData(String FTA)
{
DataTable Results = moData.GetFTA(FTA);
gridFTA.DataSource = Results;
gridFTA.DataBind();
}
Code from the Database
public DataTable GetFTA(
string FTA)
{
// Prepare connection
SqlConnection conn = new SqlConnection(msConnString);
// Prepare command
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
if (FTA == "")
{ cmd.CommandText = "Select * from View_FTA order by FacultyName"; }
DataTable dt = new DataTable();
try
{
conn.Open();
dt.Load(cmd.ExecuteReader());
}
catch (Exception ex)
{
logException(ex);
}
finally
{
conn.Close();
}
return dt;
}
Your asp:Panel has Visible set to false. This makes the ASP.NET engine to not to render the control (and its contents) at all.
I am attempting to hide a DetailsView Row conditionally from code behind. I'd like to be able to call SomeMethod based on some condition and have it to hide Row 12. Does anyone know how I can go about accomplishing this?
I have used the debugger to confirm that Row[12] is the correct row and that the method is being executed.
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Rows[12].visible= false;
//Alternative methods I attempted that also did not work.
//DetailsViewRow row = DetailsView1.DataItem as DetailsViewRow;
//row[12].Visible = false;
//DetailsViewRow row = DetailsView1.Rows[12];
//row.visible = false;
}
This is my aspx.
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="ObjectDataSource2">
...
<asp:TemplateField HeaderText="Item"
SortExpression="Item">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Item") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Item") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...
You have to use Fields collection of DetailsView. Your method should look like:
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Fields[12].Visible= false;
}
To test create add a DetailsView to page, add a button:
<asp:DetailsView ID="DetailsView1" AutoGenerateRows="false" runat="server" Height="50px" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Category") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<br />
<asp:Button ID="btnHide" runat="server" Text="Hide" OnClick="SomeMethod" />
And add the following code in code behind:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DetailsView1.DataSource = GetProduct();
DetailsView1.DataBind();
}
}
private List<Product> GetProduct()
{
return new List<Product>()
{
new Product{Id=1, Name="Paper", Category="Stationery"},
new Product{Id=2, Name="Pen", Category="Stationery"},
new Product{Id=3, Name="Pencil", Category="Stationery"}
};
}
protected void SomeMethod(object sender, EventArgs e)
{
DetailsView1.Fields[0].Visible = false;
}
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
}
And here's the result:
I have a gridview, in which two link buttons are there.
Here it is:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" PageSize="5" OnRowCommand="gv_RowCommand"
AllowPaging="true" DataKeyNames="ID" CssClass="mGrid" BackColor="White" BorderColor="Silver" BorderStyle="Double"
BorderWidth="1px" CellPadding="4" >
<RowStyle BackColor="White" Width="150%" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false" >
<ItemTemplate>
<asp:Label ID="lblID" Text='<%#Bind("ID") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Version">
<ItemTemplate>
<asp:Label ID="lblversion" Text='<%#Bind("version") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image ID">
<ItemTemplate>
<asp:Label ID="lblimageid" runat="server" Text='<%#Bind("image_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Getty ID">
<ItemTemplate>
<asp:Label ID="lblgettyid" runat="server" Text='<%#Bind("getty_id") %>'></asp:Label>
</ItemTemplate>
<%--<EditItemTemplate>
<asp:TextBox ID="txtgettyid" Text='<%#Bind("getty_id") %>' runat="server"></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pool Letter">
<ItemTemplate>
<asp:Label ID="lblpoolletter" runat="server" Text='<%#Bind("pool_letter") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Use">
<ItemTemplate>
<asp:Label ID="lbltouse" runat="server" Text='<%#Bind("to_use") %>'></asp:Label>
</ItemTemplate>
<%-- <EditItemTemplate>
<asp:TextBox ID="txttouse" Text='<%#Bind("to_use") %>' runat="server"></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Clue">
<ItemTemplate>
<asp:Label ID="lblclue" runat="server" Text='<%#Bind("clue") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Range">
<ItemTemplate>
<asp:Label ID="lblrange" runat="server" Text='<%#Bind("range") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="last_updated">
<ItemTemplate>
<asp:Label ID="lbllastupdated" runat="server" Text='<%#Bind("last_updated") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<%#Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First">
<ItemTemplate>
<asp:Label ID="lblfirst" runat="server" Text='<%#Bind("first") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle">
<ItemTemplate>
<asp:Label ID="lblmiddle" runat="server" Text='<%#Bind("middle") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last">
<ItemTemplate>
<asp:Label ID="lbllast" runat="server" Text='<%#Bind("last") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemStyle Wrap="false" Width="100px" />
<ItemTemplate>
<asp:LinkButton ID="lnkbtnedit" Text="Edit" Visible="true" style="color:#006699;" CommandName="Edit" runat="server"></asp:LinkButton>
<asp:LinkButton ID="lnkbtndel" Text="Delete" Visible="true" style="color:#006699;" CommandName="Delete" runat="server"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#006699" Font-Bold="True" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#CCFFFF" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
con = new SqlConnection(conString);
con.Open();
int v = version + 1 ;
string updatedetails = "update tblfameface set status ='Deleted',version=" + v + " where ID=" + ID + " ";
SqlCommand cmd = new SqlCommand(updatedetails, con);
int temp = cmd.ExecuteNonQuery();
con.Close();
//int rows = GridView1.CurrentCell.RowIndex;
//GridView1.Rows.RemoveAt(rows);
bindDB();
LinkButton btndel = (LinkButton)GridView1.Rows[row.RowIndex].FindControl("lnkbtndel");
btndel.Visible = false;
}
On clicking Edit link,it is working as desired and
When I click on Delete link, I want that row to be hidden.
which event should be fired and how can it be handled?
how can I achieve this??
This is Aspx:
<Columns>
<asp:TemplateField HeaderText="Hide"> <ItemTemplate> <asp:LinkButton ID="lnkHide" runat="server" Text="Hide" OnClick="LnkHide_Click" /> </ItemTemplate> </asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
This is Code behind:
protected void LnkHide_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
clickedRow.Visible = false;
}
Try this code
private void button1_Click(object sender, EventArgs e)
{
int row = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows.RemoveAt(row);
}
Try this code........
i have a simple task of updating row, on a click of update linkbutton, as
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<% #Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<% #Eval("Mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Eval("LName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Eval("LName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="action">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">LinkButton</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
at CS page
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox t1 = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
string s = t1.Text.ToString();
Label3.Text = s;
}
I tried working on this several times but i don't get any answer. what possible code i am missing or there is any error in a code.Thanks for any assistance.
Your LinkButton needs to have a CommandName applied to it in the EditTemplate:
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update">Update</asp:LinkButton>