如何将CheckBox fom Gridview的chaecked数据插入数据库? [英] How to insert chaecked data of CheckBox fom Gridview to database ?
本文介绍了如何将CheckBox fom Gridview的chaecked数据插入数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在gridview中插入已检查的复选框数据。
我的HTML文件就像这样::::
I want to insert checked checkbox data from gridview in database.
MY HTML FILE IS LIKE THIS::::
<pre lang="xml"><asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EmailId" SortExpression="EmailId">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EmailId") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("EmailId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:TemplateField HeaderText="Date_Time" SortExpression="Date_Time" Visible="false">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Date_Time") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Visible="false" Text='<%# Bind("Date_Time") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Checkall" SortExpression="Checkall">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Checkall") %>'></asp:TextBox>
</EditItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="Chkall" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
我的代码背后的代码:::
My CODE BEHIND FILE IS LIKE THIS:::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["TDB"].ConnectionString;
int select;
protected void Page_Load(object sender, EventArgs e)
{
}
private void BindGrid()
{
Class1 cs = new Class1();
GridView1.DataSource = cs.DisplayData();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGrid();
}
/// <summary>
/// ///Test insert multiple values////
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked == true)
{
select++;
}
}
for (int i = 0; i < GridView1.Rows.Count-1; i++)
{
// string Date_Time = GridView1.Rows[i].Cells[4].Text;
GridViewRow row = GridView1.Rows[i];
string U_Id = GridView1.Rows[i].Cells[1].Text;
string Name = GridView1.Rows[i].Cells[2].Text;
string Email_Id = GridView1.Rows[i].Cells[3].Text;
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked == true)
{
InsertData(U_Id, Name, Email_Id, System.DateTime.Now.ToString());
}
}
Response.Write("Record inserted successfully");
}
void InsertData(string U_Id, string Name, string Email_Id, string Date_Time)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Dashboard values('" + U_Id + "','" + Name + "','" + Email_Id + "','" + Date_Time + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
I have insert code and html file??? now pls help me with this cod eand tell me where to change things
推荐答案
虽然你的代码有可能会担心,我正在编写解决你问题的片段。
将此替换为Button2_Click-
Although your code have may concerns, I am writing the snippet which should solve your problem.
Replace this for Button2_Click-
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked == true)
{
select++;
}
}
for (int i = 0; i < GridView1.Rows.Count-1; i++)
{
// string Date_Time = GridView1.Rows[i].Cells[4].Text;
GridViewRow row = GridView1.Rows[i];
string U_Id = ((TextBox)GridView1.Rows[i].FindControl("TextBox5")).Text;
string Name = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text;
string Email_Id = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text;
CheckBox cb = (CheckBox)Rows[i].FindControl("CheckBox1");
if (cb.Checked == true)
{
InsertData(U_Id, Name, Email_Id, System.DateTime.Now.ToString());
Response.Write("Record inserted successfully");
}
}
}
如果没有请解决您的问题,请告诉我:)
In case this doesn't resolve your problem, please let me know :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["TDB"].ConnectionString;
int select;
protected void Page_Load(object sender, EventArgs e)
{
}
private void BindGrid()
{
Class1 cs = new Class1();
GridView1.DataSource = cs.DisplayData();
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
BindGrid();
}
/// <summary>
/// ///Test insert multiple values////
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
foreach (GridViewRow row in GridView1.Rows)
{
Label Id = row.FindControl("Label5")as Label;
Label Name = row.FindControl("Label1")as Label;
Label EmailId = row.FindControl("Label2")as Label;
Label Date_Time = row.FindControl("Label3") as Label;
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.Checked==true)
{
InsertData(Id.Text, Name.Text, EmailId.Text,System.DateTime.Now.ToString());
}
}
}
public void InsertData(string Id, string Name, string EmailId,string Date_Time)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Test values('" + Id + "','" + Name + "','" + EmailId + "','"+Date_Time+"')", con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
首先你会发现按钮上的复选框点击如下
First Of You have find Check box on button click like below
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
foreach (GridViewRow row in MyGridView.Rows)
{
TextBox U_Id = row.FindControl("Textbox5")as TextBox;
TextBox Name = row.FindControl("Textbox1")as TextBox;
TextBox Email_Id = row.FindControl("Textbox2")as TextBox;
CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
if (cb.checked==True)
{
InsertData(U_Id, Name, Email_Id, System.DateTime.Now.ToString());
}
}
}
Public void InsertData(string U_Id, string Name, string Email_Id, string Date_Time)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TDB"].ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into Dashboard values('" + U_Id + "','" + Name + "','" + Email_Id + "','" + Date_Time + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
这篇关于如何将CheckBox fom Gridview的chaecked数据插入数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文