本文介绍了在选项卡上单击插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码:
Code :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (
SqlConnection con =
new SqlConnection(@"Data Source=MAJ-056\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"))
{
SqlDataAdapter da = new SqlDataAdapter("select * from tbl", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
using (
SqlConnection con =
new SqlConnection(@"Data Source=MAJ-056\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into tbl values('" + TextBox1.Text + "')", con);
cmd.ExecuteNonQuery();
}
BindGrid();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
using (
SqlConnection con =
new SqlConnection(@"Data Source=MAJ-056\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"))
{
int id = Convert.ToInt32(GridView1.DataKeys[e.NewSelectedIndex].Values[0]);
con.Open();
SqlCommand cmd = new SqlCommand("select * from tbl where Id=" + id + "", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = Convert.ToString(dr["Name"]);
}
}
}
}
设计:
Design :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanging="GridView1_SelectedIndexChanging"
AutoGenerateSelectButton="True" DataKeyNames="Id">
</asp:GridView>
</div>
</form>
</body>
</html>
推荐答案
这篇关于在选项卡上单击插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!