GridView not showing up in ASP.NET? - asp.net

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.

Related

Get current GridView

I've got a page with 3 GridViews.
<div id="Export" runat="server" class="headeropt noprint">
<asp:LinkButton runat="server" ID="buttonexport" OnClick="buttonexport_Click" OnClientClick="Javascript:Noshow();" CssClass="btn btn-default">
<asp:Image runat="server" ImageUrl="/images/Excel-16.gif" />
<asp:Label runat="server" Text="Export" />
</asp:LinkButton>
</div>
<div class="col-md-12">
<asp:GridView ID="grid1" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid1_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid1_RowEditing" datakeynames="grid1">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("name") %>' Text='<%# Eval("name") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("company") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid2" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid2_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" OnRowEditing="grid2_RowEditing" datakeynames="grid2">
<Columns>
<asp:TemplateField HeaderText="Product Family">
<ItemTemplate>
<asp:LinkButton ID="lnkprdfamily" runat="server" CausesValidation="false" CommandName="Edit" CommandArgument='<%# Eval("prdfamily") %>' Text='<%# Eval("prdfamily") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle CssClass="alinha-direita" />
<FooterStyle CssClass="alinha-direita" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="grid3" runat="server" CellPadding="0" CssClass="table" CellSpacing="0" OnRowDataBound="grid3_RowDataBound" AutoGenerateColumns="false" GridLines="Vertical" BorderStyle="Solid" ShowFooter="true" datakeynames="grid3">
<Columns >
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("product") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
as you can see, the first grid links to the second, and the second to the third.
What I want to know, is at any given time to get the grid I'm viewing at the moment.
Just edited adding the linkButton that exports the grid to excel. This is why I need the gridview that I'm currently on, so that the export command works at any time.
Here's the export code:
protected void buttonexport_Click(object sender, EventArgs c)
{
Master.Page.Form.Attributes.Remove("onsubmit");
CB.ChangeControlsToValue(grid1);
string attachment = "attachment; filename=File.xls";
//Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Thanks.
Nevemind, got a solution.
I've just checked the one that is visible at the moment and work with it.
bool list = grid1.Visible;
if (list)
{ CB.ChangeControlsToValue(grid1); }
and so on....

How to validate time using compare validator?

How to validate time using compare validator?
I have a Editable gridview that contain 2 column for time Start & End Time and I want to validate them that star time must be earlier than end time. I have 24 hour format.
For Example If I entered Start Time: 20:30 and the End Time is 19:30 it should give me error and prevent me from Updating the row
simply you can try this
In aspx code
<div>
<asp:GridView ID="gvDetails" DataKeyNames="EventID,EventName" runat="server" AutoGenerateColumns="false"
CssClass="Gridview" HeaderStyle-BackColor="#61A6F8" ShowFooter="true" HeaderStyle-Font-Bold="true"
HeaderStyle-ForeColor="White" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowDeleting="gvDetails_RowDeleting" OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating"
OnRowCommand="gvDetails_RowCommand">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
ToolTip="Update" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server" ImageUrl="~/Images/Edit.jpg"
ToolTip="Edit" Height="20px" Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit" runat="server"
ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/Images/AddNewitem.jpg"
CommandName="AddNew" Width="30px" Height="30px" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EventName">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("EventName") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("EventName") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrusrname" runat="server" />
<asp:RequiredFieldValidator ID="rfvEventName" runat="server" ControlToValidate="txtftrusrname"
Text="*" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StartTime">
<EditItemTemplate>
<asp:TextBox ID="txtStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStartTime" runat="server" Text='<%#Eval("StartTime") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrStartTime" runat="server" />
<asp:RequiredFieldValidator ID="rfvStartTime" runat="server" ControlToValidate="txtftrStartTime"
Text="*" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EndTime">
<EditItemTemplate>
<asp:TextBox ID="txtstate" runat="server" Text='<%#Eval("EndTime") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblstate" runat="server" Text='<%#Eval("EndTime") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrEndTime" runat="server" />
<asp:RequiredFieldValidator ID="rfvEndTime" runat="server" ControlToValidate="txtftrEndTime"
Text="*" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
And at code behind
protected void gvDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtftrusrname");
TextBox txtStartTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrStartTime");
TextBox txtEndTime = (TextBox)gvDetails.FooterRow.FindControl("txtftrEndTime");
if (Convert.ToDateTime(txtEndTime.Text.Trim().ToString()) <= Convert.ToDateTime(txtStartTime.Text.Trim().ToString()))
{
lblresult.Text = "Please enter valid end time";
}
else
{
con.Open();
SqlCommand cmd =
new SqlCommand(
"insert into tblEvents(EventName,StartTime,EndTime) values('" + txtUsrname.Text + "','" +
txtStartTime.Text + "','" + txtEndTime.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindEmployeeDetails();
lblresult.ForeColor = Color.Green;
lblresult.Text = txtUsrname.Text + " Details inserted successfully";
}
else
{
lblresult.ForeColor = Color.Red;
lblresult.Text = txtUsrname.Text + " Details not inserted";
}
}
}
}

my gridview footer is not showing the results of calculated columns

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

Gridview rowupdating doesn't work

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>

'GridView1' fired event PageIndexChanging which wasn't handled

I am using a gridview and I want to use paging. I have already set allow paging to true and page size to 5. I can see the numbers at the base of my gridview, but when i click on a number to move to respective page, it throws an error saying:
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled.
Code:
<asp:GridView ID="GridView1" runat="server" CellPadding="5"
AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="contact_id"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
PageSize="5">
<Columns>
<asp:TemplateField HeaderText="contact_id">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("contact_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="address">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("address") %>'></asp:Label><br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("city") %>'></asp:Label><br />
<asp:Label ID="Label7" runat="server" Text='<%# Eval("state") %>'></asp:Label><br />
<asp:Label ID="Label8" runat="server" Text='<%# Eval("pincode") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="email">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Eval("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="mobile">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Eval("mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="context">
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("context") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>PENDING</asp:ListItem>
<asp:ListItem>OK</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemStyle CssClass="button" />
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:GridView>
You will have to handle the PageIndexChanging event for the grid
Something like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//Bind grid
}
It will not work if you enable paging. You also need to write the event handler for PageIndexChangeEvent. Check this link: http://forums.asp.net/post/1177923.aspx
You would need to code "PageIndexChanging" event to make it work. Add an event handler for the PageIndexChanging where you set the GridView.CurrentPage = e.NewPage...
Add one more event in HTML mark up for pagging.
OnPageIndexChanging="GridView1_PageIndexChanging"
Now handle same event from code behind
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//Your code
}

Resources