Reputation: 31
I have a grid view which I allow the user to select multiple lines via a check box, from the selected lines I want to insert a dataid into a database table.
I have used a foreach on the aspx section which looks OK however nothing is inserting on the button click.
Gridview Code.
<asp:GridView ID="GV_EyeBall" runat="server" AutoGenerateColumns="False" Font-Size="Small" Width="44%" >
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="DataId" HeaderText="DataId" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="DataId" HeaderStyle-CssClass="text-center" />
<asp:BoundField DataField="BusinessName" HeaderText="BusinessName" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="BusinessName" HeaderStyle-CssClass="text-center" ControlStyle-Font-Size="Small" />
<asp:BoundField DataField="Industry" HeaderText="Industry" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="Industry" HeaderStyle-CssClass="text-center" />
<asp:BoundField DataField="TelephoneNumber" HeaderText="TelephoneNumber" ItemStyle-Width="90px" ItemStyle-Wrap="false" ItemStyle-CssClass="checkIt" SortExpression="TelephoneNumber" HeaderStyle-CssClass="text-center" />
<asp:TemplateField ItemStyle-Width="75px" ItemStyle-Wrap="false">
<ItemTemplate>
<asp:CheckBox ID="Removechk" runat="server" ItemStyle-Width="100px" HeaderText="Remove" />
<%--<asp:HiddenField ID="DataID" runat="server" Value='<%# Eval("DataID") %>' />--%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Silver" ForeColor="Black" />
</asp:GridView>
aspx page code
protected void Btnsubmit_Click(object sender, EventArgs e)
{
string str = string.Empty;
string strname = string.Empty;
foreach (GridViewRow gvrow in GV_EyeBall.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("Removechk");
if (chk.Checked)
{
Calldatabase.InsertEyeballids(Convert.ToInt32(gvrow.Cells[0].Text));
}
}
}
Page Load
protected void Page_Load(object sender, EventArgs e)
{
var eyeball = Calldatabase.Eyeball();
GV_EyeBall.DataSource = eyeball;
GV_EyeBall.DataBind();
}
Upvotes: 0
Views: 39
Reputation: 1017
It seems your grid view is getting binded again on click of your submit button on Page_Load. Since you haven't shared your Page_Load method I have implemented following working snippet based on your issue.
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="tempApp13.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Job" HeaderText="Job" />
<asp:BoundField DataField="Salary" HeaderText="Salary" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRemove" runat="server" HeaderText="Remove" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
<asp:Literal ID="litMessage" runat="server" />
</form>
</body>
</html>
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace tempApp13
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gv.DataSource = new List<Employee>
{
new Employee { ID = 1, Name = "John", Job = "Clerk", Salary = 25000 },
new Employee { ID = 2, Name = "Allen", Job = "Clerk", Salary = 20000 },
new Employee { ID = 3, Name = "Smith", Job = "Sales", Salary = 21000 },
new Employee { ID = 4, Name = "Martin", Job = "Sales", Salary = 35000 },
new Employee { ID = 5, Name = "Bruce", Job = "Analyst", Salary = 35000 },
new Employee { ID = 6, Name = "James", Job = "Clerk", Salary = 25000 },
};
gv.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv.Rows)
{
var chk = (CheckBox)row.FindControl("chkRemove");
if (chk.Checked)
{
litMessage.Text += row.Cells[1].Text + " ";
}
}
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Job { get; set; }
public int Salary { get; set; }
}
}
I have taken a sample data source employees and binded it with grid view on Page_Load where I am checking its not IsPostBack. So on initial load the data binded to the grid view but won't binded again on page post back. Rest of the code is similar and pretty straight forward.
Hope it helps
Thanks for providing your Page_Load method. Check the following code:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
var eyeball = Calldatabase.Eyeball();
GV_EyeBall.DataSource = eyeball;
GV_EyeBall.DataBind();
}
}
Upvotes: 1