无法动态创建控件GridView,错误"GridView,GridView'ctl02'触发了未处理的事件RowEditing". [英] Cann't creating dynamically control GridView, error "GridView, The GridView 'ctl02' fired event RowEditing which wasn't handled."
问题描述
大家好,最近开始学习asp.net,并且已经遇到了自己无法决定的问题.我需要创建一个GridView任意数字,该GridView从数据库输出信息并建议对其进行编辑(编辑"按钮).当我单击编辑"按钮时,代码崩溃,并显示错误"GridView" ctl02"触发了未处理的事件RowEditing."当我创建一个静态控件时,一切正常,但是我需要动态生成控件.
Hi all, recently started learning asp.net and already have a problem that can''t myself decide. I need to create arbitrary number a GridView which output information from DataBase and propose editing it ("edit" button). Code crashed when I click in button "edit" with that error "The GridView ''ctl02'' fired event RowEditing which wasn''t handled." When I create one static control everything is working fine, but I need dynamically generate controls.
public partial class _Default : System.Web.UI.Page
{
private SqlConnection Conn;
GridView Grid;
override protected void OnInit(EventArgs e)
{
Conn = new SqlConnection(
ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString
);
Conn.Open();
Grid = new GridView();
Grid.AutoGenerateColumns = false;
CommandField Command = new CommandField();
Command.ShowEditButton = true;
Grid.Columns.Add(Command);
BoundField Boundf = new BoundField();
Boundf.DataField = "FamilyName";
Grid.Columns.Add(Boundf);
SqlDataSource DataSource = new SqlDataSource(
ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString,
"SEL ECT FamilyName FR OM Samples");
Grid.DataSource = DataSource;
Grid.DataBind();
this.Form.Controls.Add(this.Grid);
}
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
此代码可以正常工作
This code work fine
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Interface<title>
<head>
<body>
<fo rm id="Form" runat="server">
<div>
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" CellPadding="3" DataSourceID="SqlDataSourceMyDB">
<Columns>
<asp:BoundField DataField="NumberInFamily" HeaderText="NumberInFamily" SortExpression="NumberInFamily" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceMyDB" runat="server" ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
SelectCommand="SELECT FamilyName Id FR OM Samples">
</asp:SqlDataSource>
</div>
<form>
<body>
<html>
请帮帮我.
P.S.请问我使用我的语言吗?
Please help me.
P.S. excuse me for my language
推荐答案
ConnectionStrings:MyDBConnectionString %> " =" 选择FamilyName ID FR OM样本" < /asp:SqlDataSource > < /div > < 表单 > < 正文 > < html >
ConnectionStrings:MyDBConnectionString %>" SelectCommand="SELECT FamilyName Id FR OM Samples"> </asp:SqlDataSource> </div> <form> <body> <html>
请帮帮我.
P.S.请原谅我的语言
Please help me.
P.S. excuse me for my language
阅读此 [^ ]关于动态创建控件.应该会让您入门.
Read this[^] about creating controls dynamically. Should get you started.
这篇关于无法动态创建控件GridView,错误"GridView,GridView'ctl02'触发了未处理的事件RowEditing".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!