I have a nested grid views:
<asp:GridView ID="gvRequisitions" runat="server" AutoGenerateColumns="false"
DataKeyNames="PurchaseRequisitionID" OnRowDataBound="OnRowDataBound" ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt="" style="cursor:pointer" src="../images/plus.png" width="12" height="12" />
<asp:Panel ID="pnlItems" runat="server" Style="display: none">
<asp:GridView ID="gvItems" runat="server" AutoGenerateColumns="false" ShowFooter="true"
OnRowDataBound="gvItems_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblItemID" runat="server" Text='<%# Eval("ItemID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddItem" runat="server" OnClick="addItem" Text="add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item Code">
<ItemTemplate>
<asp:Label ID="lblItemCode" runat="server" Text='<%# Eval("ItemCode") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtItemCode" runat="server" Visible="true" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'
Width="225"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDescription" runat="server" Width="225"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date req.">
<ItemTemplate>
<asp:Label ID="lblDateRequired" runat="server" Text='<%# Eval("DateRequired") %>'
Width="75"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDateRequired" runat="server" Width="75"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Measure">
<ItemTemplate>
<asp:Label ID="lblMeasure" runat="server" Text='<%# Eval("MeasureID") %>'
Width="120" ></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlMeasure" runat="server" Width="120"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>' Width="75"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="75"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost">
<ItemTemplate>
<asp:Label ID="lblCostPerUnit" runat="server" Text='<%# Eval("CostPerUnit") %>' Width="75"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCostPerUnit" runat="server" Width="75"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%# Eval("Total") %>' Width="120"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>No Items For this Requisition</EmptyDataTemplate>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblPurchaseRequisitionID" runat="server" Text='<%# Eval("PurchaseRequisitionID") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddReq" runat="server" Text="Add" OnClick="addPurreq" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Requested By">
<ItemTemplate>
<asp:Label ID="lblRequestor" runat="server" Text='<%# Eval("Requestor") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlRequestor" runat="server" Width="200"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDepartmentID" runat="server" Text='<%# Eval("DepartmentID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDepartment" runat="server" Width="200" ></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Budget">
<ItemTemplate>
<asp:Label ID="lblBudgetID" runat="server" Text='<%# Eval("BudgetID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBudget" runat="server" Width="200"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supplier">
<ItemTemplate>
<asp:Label ID="lblSupplierID" runat="server" Text='<%# Eval("SupplierID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSupplier" runat="server" Width="200"></asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created">
<ItemTemplate>
<asp:Label ID="lblDateCreated" runat="server" Text='<%# Eval("DateCreated","{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In code behind I have:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string purchaseRequisitionID = gvRequisitions.DataKeys[e.Row.RowIndex].Value.ToString();
GridView gvItems = (GridView)e.Row.FindControl("gvItems");
// new code to send reference to gvItems_RowDataBound
gvItems.RowDataBound += new GridViewRowEventHandler(gvItems_RowDataBound);
int id = Convert.ToInt32(purchaseRequisitionID);
using (SqlConnection conn = new SqlConnection(constr))
{
using(SqlCommand comm = new SqlCommand("procItems_CRUD"))
{
comm.Parameters.AddWithValue("#Action", "SELECT");
comm.Parameters.AddWithValue("#ID", id);
using(SqlDataAdapter sda = new SqlDataAdapter())
{
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
sda.SelectCommand = comm;
using(DataTable dt = new DataTable())
{
sda.Fill(dt);
gvItems.DataSource = dt;
gvItems.DataBind();
}
}
}
}
}
}
And:
protected void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlMeasure = e.Row.FindControl("ddlMeasure") as DropDownList;
if(ddlMeasure == null)
{
}
}
}
But I cannot see the drop down list as it returns a null value (as I know it is not a DataRow) but how can I see the footer row and the elements in footer row so that I can add another Item to the order/populate DropDownList etc.
Have been trying to sort this for some time and cannot find a working solution so any pointer(s) will be very much appreciated.
You need to use DataControlRowType.Footer.
protected void gvItems_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a footer row
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlMeasure = e.Row.FindControl("ddlMeasure") as DropDownList;
if (ddlMeasure != null)
{
ddlMeasure.Items.Insert(0, new ListItem("DLL Found!", "-1"));
}
}
}
I have been searching on here and other sites for three days for a solution. I need to select one or more rows (using checkbox) and delete selected row(s) from a gridview who's datasource is a session datatable as you will see. Not interested in SQLConnections or LINQ Entities. As a result I am not sure if I need to use e.commandarguments, or how to go about it, but I have tried them with no luck.
Error is visible and clearly commented on CART.ASPX.CS page inside the following method
if (chkRemCart != null && chkRemCart.Checked)
Any assistance would be greatly appreciated and if you require any further coding or explanations to help with a solution, just ask.
Thank you in advance.
CART.ASPX
<asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" CellPadding="4"
HeaderStyle-CssClass="header" EmptyDataText="You have successfully cleared your Shopping Cart"
OnRowDataBound="gvCart_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Movie Selector">
<ItemTemplate>
<asp:CheckBox ID="chkRemCart" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label ID="lblMovieID" runat="server" Text='<%# Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Genre">
<ItemTemplate>
<asp:Label ID="lblGenre" runat="server" Text='<%# Eval("Genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rating">
<ItemTemplate>
<asp:Label ID="lblRating" runat="server" Text='<%# Eval("Rating") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price","{0:n}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnRem" runat="server" OnClick="RemCart" Text="Remove Selection" CssClass="button" Title="Remove Movies From Cart" />
CART.ASPX.CS
Page_Load calling RefreshPages() method. gvCart displaying all columns and rows of items in Datasource/Session/Datatable
public void RefreshPages()
{
if (Session["SelectedMovies"] != null)
{
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
protected void RemCart(object sender, EventArgs e)
{
// Check session exists
if (Session["selectedMovies"] != null)
{
// Opening / Retreiving DataTable.
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
foreach (GridViewRow row in gvCart.Rows)
{
CheckBox chkRemCart = row.Cells[0].FindControl("chkRemCart") as CheckBox;
if (chkRemCart != null && chkRemCart.Checked)
{
// Error appearing on line below. Scroll right to read.
int MovieId = Convert.ToInt32(gvCart.DataKeys[row.RowIndex].Value); // Error here. //Additional information: Index was out of range. Must be non - negative and less than the size of the collection.
MovieTable.Rows.RemoveAt(row.RowIndex);
//Saving session
Session["selectedMovies"] = MovieTable;
// Updating gvCart
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
}
}
Assign DataKeyNames="PrimaryKey" in gridview like below and check
<asp:GridView ID="gvCart" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" DataKeyNames="MovieId" CellPadding="4"
HeaderStyle-CssClass="header" EmptyDataText="You have successfully cleared your Shopping Cart"
OnRowDataBound="gvCart_RowDataBound" >
<Columns>
<asp:TemplateField HeaderText="Movie Selector">
<ItemTemplate>
<asp:CheckBox ID="chkRemCart" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Movie ID">
<ItemTemplate>
<asp:Label ID="lblMovieID" runat="server" Text='<%# Eval("MovieId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Genre">
<ItemTemplate>
<asp:Label ID="lblGenre" runat="server" Text='<%# Eval("Genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rating">
<ItemTemplate>
<asp:Label ID="lblRating" runat="server" Text='<%# Eval("Rating") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%# Eval("Price","{0:n}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code behind:
public void RefreshPages()
{
if (Session["SelectedMovies"] != null)
{
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
protected void RemCart(object sender, EventArgs e)
{
// Check session exists
if (Session["selectedMovies"] != null)
{
// Opening / Retreiving DataTable.
DataTable MovieTable = (DataTable)Session["SelectedMovies"];
foreach (GridViewRow row in gvCart.Rows)
{
CheckBox chkRemCart = row.Cells[0].FindControl("chkRemCart") as CheckBox;
if (chkRemCart != null && chkRemCart.Checked)
{
// Error appearing on line below. Scroll right to read.
int MovieId = Convert.ToInt32(gvCart.DataKeys[row.RowIndex].Value); // Error here. //Additional information: Index was out of range. Must be non - negative and less than the size of the collection.
DataRow[] drs = dt.Select("MovieId = '" + MovieId + "'"); // replace with your criteria as appropriate
if (drs.Length > 0)
{
MovieTable.Rows.Remove(drs[0]);
}
}
}
//Saving session
Session["selectedMovies"] = MovieTable;
// Updating gvCart
gvCart.DataSource = MovieTable;
gvCart.DataBind();
}
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyManageConnectionString"].ToString());
string InstituteId = HttpContext.Current.Request.Cookies["InstituteId"].Value;
protected void Page_Load(object sender, EventArgs e)
{
((Label)Master.FindControl("lblPageName")).Text = "Employee List";
((Label)Master.FindControl("lblPageName")).ForeColor = System.Drawing.Color.Black;
((Label)Master.FindControl("lblPageName1")).Text = " Employee List";
if (Request.Cookies["InstituteId"] == null)
{
string OriginalUrl = HttpContext.Current.Request.RawUrl;
string LoginPageUrl = "INlogin.aspx";
HttpContext.Current.Response.Redirect(String.Format("{0}?R={1}", LoginPageUrl, OriginalUrl));
}
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
string strQuery = "select * from tblEmployee where InstituteId=#InstituteId";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("#InstituteId", InstituteId);
gvEmpList.DataSource = GetData(cmd);
gvEmpList.DataBind();
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
//SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string ActivationCode = Guid.NewGuid().ToString();
string txtEmployeeName = ((TextBox)gvEmpList.FooterRow.FindControl("txtEmployeeName")).Text;
string txtEmployeeAddress = ((TextBox)gvEmpList.FooterRow.FindControl("txtEmployeeAddress")).Text;
string txtEmployeeEmailId = ((TextBox)gvEmpList.FooterRow.FindControl("txtEmployeeEmailId")).Text;
string txtMobile = ((TextBox)gvEmpList.FooterRow.FindControl("txtMobile")).Text;
string txtBirthDate = ((TextBox)gvEmpList.FooterRow.FindControl("txtBirthDate")).Text;
string txtdateOfJoining = ((TextBox)gvEmpList.FooterRow.FindControl("txtdateOfJoining")).Text;
string txtExperiance = ((TextBox)gvEmpList.FooterRow.FindControl("txtExperiance")).Text;
string txtLastSchoolDetails = ((TextBox)gvEmpList.FooterRow.FindControl("txtLastSchoolDetails")).Text;
string txtSalaryDetails = ((TextBox)gvEmpList.FooterRow.FindControl("txtSalaryDetails")).Text;
bool chkmerriageStatus = ((CheckBox)gvEmpList.FooterRow.FindControl("chkmerriageStatus")).Checked;
string txtNationality = ((TextBox)gvEmpList.FooterRow.FindControl("txtNationality")).Text;
try
{
//SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
//con.Open();
//cmd.Connection = con;
cmd.CommandText = "INSERT INTO tblEmployee (InstituteId, EmployeeName, EmployeeAddress, EmployeeEmailId, Mobile, BirthDate, dateOfJoining, Experiance, LastSchoolDetails, SalaryDetails, merriageStatus, Nationality, ActivationCode, ActivationStatus, Password) VALUES (#InstituteId, #EmployeeName, #EmployeeAddress, #EmployeeEmailId, #Mobile, #BirthDate, #dateOfJoining, #Experiance, #LastSchoolDetails, #SalaryDetails, #merriageStatus, #Nationality, #ActivationCode, #ActivationStatus, #Password);" + "select * from tblEmployee where InstituteId=#InstituteId";
cmd.Parameters.AddWithValue("#InstituteId", InstituteId);
cmd.Parameters.AddWithValue("#EmployeeName", txtEmployeeName);
cmd.Parameters.AddWithValue("#EmployeeAddress", txtEmployeeAddress);
cmd.Parameters.AddWithValue("#EmployeeEmailId", txtEmployeeEmailId);
cmd.Parameters.AddWithValue("#Mobile", txtMobile);
cmd.Parameters.AddWithValue("#BirthDate", txtBirthDate);
cmd.Parameters.AddWithValue("#dateOfJoining", txtdateOfJoining);
cmd.Parameters.AddWithValue("#Experiance", txtExperiance);
cmd.Parameters.AddWithValue("#LastSchoolDetails", txtLastSchoolDetails);
cmd.Parameters.AddWithValue("#SalaryDetails", txtSalaryDetails);
cmd.Parameters.AddWithValue("#merriageStatus", chkmerriageStatus);
cmd.Parameters.AddWithValue("#Nationality", txtNationality);
cmd.Parameters.AddWithValue("#ActivationCode", ActivationCode);
cmd.Parameters.AddWithValue("#ActivationStatus", false);
string GTPassword = System.Web.Security.Membership.GeneratePassword(8, 3);
cmd.Parameters.AddWithValue("#Password", GTPassword);
//int result = cmd.ExecuteNonQuery();
//con.Close();
//if (result == 1)
//{
// sendActivation(ActivationCode, txtEmployeeName, txtEmployeeEmailId);
// Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Registration Successfull')", true);
//}
//else {
// Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Registration Fail')", true);
//}
gvEmpList.DataSource = GetData(cmd);
gvEmpList.DataBind();
//BindData();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
When the add button is clicked btnAdd_click inserts data twice. Also I am not getting any exception errors in btnadd_click code.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="dvGrid" style="overflow-x: scroll; Overflow: scroll;">
<asp:UpdatePanel ID="upEmployeeList" runat="server">
<ContentTemplate>
<asp:GridView Width="100%" ID="gvEmpList" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeId" CssClass="gvmydatagrid" PagerStyle-CssClass="gvpager" HeaderStyle-CssClass="gvheader" RowStyle-CssClass="gvrows" ShowFooter="True" OnPageIndexChanging="gvEmpList_PageIndexChanging" OnRowEditing="gvEmpList_RowEditing" OnRowUpdating="gvEmpList_RowUpdating" OnRowCancelingEdit="gvEmpList_RowCancelingEdit" AllowPaging="True" PageSize="25">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server"
CommandArgument='<%# Eval("EmployeeId")%>'
OnClientClick="return confirm('Do you want to delete?')"
Text="Delete" OnClick="lnkRemove_Click"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField HeaderText="EmployeeId" InsertVisible="False" SortExpression="EmployeeId">
<EditItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server" Text='<%# Eval("EmployeeId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEmployeeId" runat="server" Text='<%# Bind("EmployeeId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="InstituteId" SortExpression="InstituteId">
<EditItemTemplate>
<asp:Label ID="txtInstituteId" runat="server" Text='<%# Bind("InstituteId") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblInstituteId" runat="server" Text='<%# Bind("InstituteId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="EmployeeName" SortExpression="EmployeeName">
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblEmployeeName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmployeeAddress" SortExpression="EmployeeAddress">
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeAddress" runat="server" Text='<%# Bind("EmployeeAddress") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmployeeAddress" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblEmployeeAddress" runat="server" Text='<%# Bind("EmployeeAddress") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmployeeEmailId" SortExpression="EmployeeEmailId">
<EditItemTemplate>
<asp:TextBox ID="txtEmployeeEmailId" runat="server" Text='<%# Bind("EmployeeEmailId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmployeeEmailId" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblEmployeeEmailId" runat="server" Text='<%# Bind("EmployeeEmailId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="Mobile">
<EditItemTemplate>
<asp:TextBox ID="txtMobile" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMobile" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblMobile" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BirthDate" SortExpression="BirthDate">
<EditItemTemplate>
<asp:TextBox ID="txtBirthDate" runat="server" Text='<%# Bind("BirthDate") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBirthDate" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblBirthDate" runat="server" Text='<%# Bind("BirthDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="dateOfJoining" SortExpression="dateOfJoining">
<EditItemTemplate>
<asp:TextBox ID="txtdateOfJoining" runat="server" Text='<%# Bind("dateOfJoining") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtdateOfJoining" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lbldateOfJoining" runat="server" Text='<%# Bind("dateOfJoining") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Experiance" SortExpression="Experiance">
<EditItemTemplate>
<asp:TextBox ID="txtExperiance" runat="server" Text='<%# Bind("Experiance") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtExperiance" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblExperiance" runat="server" Text='<%# Bind("Experiance") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastSchoolDetails" SortExpression="LastSchoolDetails">
<EditItemTemplate>
<asp:TextBox ID="txtLastSchoolDetails" runat="server" Text='<%# Bind("LastSchoolDetails") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastSchoolDetails" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblLastSchoolDetails" runat="server" Text='<%# Bind("LastSchoolDetails") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SalaryDetails" SortExpression="SalaryDetails">
<EditItemTemplate>
<asp:TextBox ID="txtSalaryDetails" runat="server" Text='<%# Bind("SalaryDetails") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSalaryDetails" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblSalaryDetails" runat="server" Text='<%# Bind("SalaryDetails") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="merriageStatus" SortExpression="merriageStatus">
<EditItemTemplate>
<asp:CheckBox ID="chkmerriageStatus" runat="server" Checked='<%# Bind("merriageStatus") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="chkmerriageStatus" runat="server"></asp:CheckBox>
</FooterTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkmerriageStatus" runat="server" Checked='<%# Bind("merriageStatus") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nationality" SortExpression="Nationality">
<EditItemTemplate>
<asp:TextBox ID="txtNationality" runat="server" Text='<%# Bind("Nationality") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNationality" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblNationality" runat="server" Text='<%# Bind("Nationality") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="Password">
<EditItemTemplate>
<asp:TextBox ID="txtPassword" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
</EditItemTemplate>
<%--<FooterTemplate>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</FooterTemplate>--%>
<ItemTemplate>
<asp:Label ID="lblPassword" runat="server" Text='<%# Bind("Password") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="gvheader" />
<PagerStyle CssClass="gvpager" />
<RowStyle CssClass="gvrows" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvEmpList" />
</Triggers>
</asp:UpdatePanel>
</div>
When I click on add button, footer item data will insert into my database but is insert twice. It is in debug mode and jumping around everywhere.
it show "The process or thread has changed since last step."
You are sending the insert command again by calling gvEmpList.DataSource = GetData(cmd); inside btnAdd_Click. Please check it. The cmd object inside btnAdd_Click is an insert command.
Remove following code from btnAdd_Click
gvEmpList.DataSource = GetData(cmd);
gvEmpList.DataBind();
And call following method
BindData();
I have wired a gridSearchResults_RowUpdating event for everytime an update link button is clicked. What I want to do is loop through the row at the event rowindex and add the textbox value in the row cell to a string collection. I am having troubh accessing the textbox in the cell. If this doesn't make sense my code should help make things clearer.
HTML
<asp:TemplateField HeaderText="Title" SortExpression="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("jobTitle") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTitle" runat="server" Text='<%# Bind("jobTitle") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmailAddress" runat="server" Text='<%# Bind("EmailAddress") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Telephone Number" SortExpression="TelephoneNumber">
<ItemTemplate>
<asp:Label ID="lblTelephoneNumber" runat="server" Text='<%# Eval("TelephoneNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTelephoneNumber" runat="server" Text='<%# Bind("TelephoneNumber") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company" SortExpression="Company">
<ItemTemplate>
<asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Company") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCompany" runat="server" Text='<%# Bind("Company") %>' ReadOnly="true"
BorderStyle="none" BorderWidth="0px" Width="80">
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbEditLink" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbCancelLink" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
<asp:LinkButton ID="lbUpdateLink" runat="server" CausesValidation="False" CommandName="Update" Text="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void gridSearchResults_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = gridSearchResults.Rows[e.RowIndex];
foreach (gridviewrow c in gridSearchResults.Rows[e.RowIndex])
{
}
}
So does anyone know a good way of looping through the cells of a gridview row and grabbing the textbox value in the gridview row cell. Thanks
Try this:
foreach (TableCell cell in gv.Rows(e.RowIndex).Cells)
{
TextBox txt1 = (TextBox)cell.FindControl("txtControlName");
// or if you don't want to hard code the control name and assuming that there is only 1 control in the cell then use this:
TextBox txt2 = (TextBox)cell.Controls(0);
}
I have a Gridview that allows user to edit and delete the records. I am using .net 3.5 using C#.
When I have just one record my edit command is working fine but problem comes when there are more that 1 record in my gridview.
In this case while binding the controls it it is unable to find the controls the second time and thus throws the error.
Here is my code:
protected void gdScheduleNew_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (ViewState["Updating"].Equals(true))
{
TextBox txtactsta = (TextBox)e.Row.FindControl("txtactualstartDate");
TextBox txtactend = (TextBox)e.Row.FindControl("txtactualendDate");
LinkButton lnkUpdate = (LinkButton)e.Row.FindControl("lnkUpdate");
txtactsta.Attributes.Add("ReadOnly", "true");
txtactend.Attributes.Add("ReadOnly", "true");
}
}
}
markup code is:
<asp:GridView ID="gdScheduleNew" runat="server" Width="100%" SelectedIndex="0"
CellPadding="4" AutoGenerateColumns="False" ForeColor="#333333" Height="100px"
OnRowDataBound="gdScheduleNew_RowDataBound" onrowediting="gdSchedule_RowEditing">
<FooterStyle Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="Planned Efforts">
<ItemTemplate>
<asp:Label ID="lblscheduleid" runat="server" Text='<%# Bind("scheduleid") %>'
Visible="false"></asp:Label>
<asp:Label ID="lblPlannedEffort" runat="server" Text='<%# Bind("sepcet") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblscheduleid" runat="server" Text='<%# Bind("scheduleid") %>'
Visible="false"></asp:Label>
<asp:TextBox ID="txtsepcet" runat="server" Text='<%# Bind("sepcet") %>'
Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtsepcet" runat="server" Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Efforts">
<ItemTemplate>
<asp:Label ID="lblActualEffort" runat="server"
Text='<%# Bind("actualeffort") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtactualeffort" runat="server"
Text='<%# Bind("actualeffort") %>' Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtactualeffort" runat="server" Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Planned Start Date">
<ItemTemplate>
<asp:Label ID="lblPlannedStart" runat="server"
Text='<%# Bind("plannedstartDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtplannedstartDate" runat="server"
Text='<%# Bind("plannedstartDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtplannedstartDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Planned End Date">
<ItemTemplate>
<asp:Label ID="lblPlannedEnd0" runat="server"
Text='<%# Bind("plannedendDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtplannedendDate" runat="server"
Text='<%# Bind("plannedendDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtplannedendDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual Start Date">
<ItemTemplate>
<asp:Label ID="lblActualStart" runat="server"
Text='<%# Bind("actualstartDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtactualstartDate" runat="server"
Text='<%# Bind("actualstartDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtactualstartDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Actual End Date">
<ItemTemplate>
<asp:Label ID="lblActualEnd" runat="server"
Text='<%# Bind("actualendDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtactualendDate" runat="server"
Text='<%# Bind("actualendDate") %>' class="dateclass" Width="88px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ftxtactualendDate" runat="server" class="dateclass"
Width="88px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Member Name">
<ItemTemplate>
<asp:Label ID="lblMember" runat="server" Text='<%# Bind("membername") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlmem" runat="server" DataTextField="MemberName" DataValueField="MemberID">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="fddlmem" runat="server" DataTextField="MemberName" DataValueField="MemberID">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<br />
<asp:LinkButton ID="LnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAddnew" runat="server" CausesValidation="true"
CommandName="AddNew" Text="Submit"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The error is on this line: txtactsta.Attributes.Add("ReadOnly", "true");:
object reference no set to an instance of object
How can I fix this?
Doesn't gdScheduleNew_RowDataBound get called for every row in your gridview? txtactualstartDate and txtactualendDate only exist on edit rows, they do not exist on non editing rows.
Try this:
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)