我正在创建一个包含两个网格视图的页面,其中一个事件负责另一个网格视图中的内容。 (就像你有一个任务而你的任务有一些步骤,所以当你从一个任务列表中点击一个任务时,你可以看到一个与该任务相关的步骤表) 但我正如上面那样面对例外。有人可以帮我解决一下吗?
的Index.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<!-- Bootstrap core CSS-->
<link href="../Content/bootstrap.min.css" rel="stylesheet" />
<!-- Custom styles for this template -->
<link href="../Styles/dashboard.css" rel="stylesheet" />
<style>
td, th
{
border-color: rgb(222, 226, 230);
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class='table - responsive'>
<asp:GridView ID="tasklistsGridView" runat="server" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="Id" AllowPaging="True" CellPadding="3" OnPageIndexChanging="tasklistsGridView_PageIndexChanging" OnRowDeleting="tasklistsGridView_RowDeleting" OnRowEditing="tasklistsGridView_RowEditing" OnRowUpdating="tasklistsGridView_RowUpdating" OnRowCancelingEdit="tasklistsGridView_RowCancelingEdit" PageSize="4" BackColor="#ffffff" BorderColor="#ffffff" BorderStyle="Solid" BorderWidth="0px" CellSpacing="2" OnRowCommand="tasklistsGridView_RowCommand" class='table table - striped table - sm' OnSelectedIndexChanged="tasklistsGridView_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" Width="100px" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtName" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescription" Width="100px" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDescription" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Prerequisites" SortExpression="Prerequisites">
<EditItemTemplate>
<asp:TextBox ID="txtPrerequisites" Width="100px" runat="server" Text='<%# Bind("Prerequisites") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPrerequisites" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Prerequisites") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Workgroup" SortExpression="Workgroup">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="Id">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="Id">
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Workgroup") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tag" SortExpression="Tag">
<EditItemTemplate>
<asp:TextBox ID="txtTag" Width="100px" runat="server" Text='<%# Bind("Tag") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTag" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Tag") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<asp:CommandField HeaderText="Objectives" ShowSelectButton="True" ShowHeader="True" />
</Columns>
<EmptyDataTemplate>
<h3>No available tasklists! Create a new one?</h3>
<form role="form">
<div class="form-group">
<label class="control-label" for="name">Name</label>
<asp:TextBox ID="newTasklistName" type="text" class="form-control" placeholder="Enter Tasklist Name" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label class="control-label" for="description">Description</label>
<asp:TextBox ID="newTasklistDescription" type="text" class="form-control" placeholder="Describe your Tasklist" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<label class="control-label" for="prerequisites">Prerequisites</label>
<asp:TextBox ID="newTasklistPrerequisites" type="text" class="form-control" placeholder="Enter Prerequisites for your Tasklist" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<div class="btn-group dropdown">
<asp:Button ID="workgroupDropdownLabel" type="button" class="btn btn-light dropdown-toggle" runat="server" Text="Workgroups" style="padding-left: 0.4em;" />
<asp:DropDownList ID="workgroupDropdown" type="button" class="btn btn-light" runat="server" style="position:relative; left:-0.3em; top: 0.1em; padding-left: 0.1em;" DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="Id"></asp:DropDownList>
</div>
</div>
<div class="form-group">
<label class="control-label" for="tag">Tag</label>
<asp:TextBox ID="newTasklistTag" type="text" class="form-control" placeholder="Tag your Tasklist" runat="server"></asp:TextBox>
</div>
<asp:Button ID="newTasklistSubmitButton" class="btn btn-default" runat="server" Text="Submit" />
</form>
</EmptyDataTemplate>
<%--<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />--%>
<SelectedRowStyle BackColor="#505050" Font-Bold="True" ForeColor="White" />
<%--<HeaderStyle BackColor="white" Font-Bold="True" ForeColor="White" />--%>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SOP3ConnectionString %>" SelectCommand="SELECT [name], [Id] FROM [workgroup]" ProviderName="System.Data.SqlClient"></asp:SqlDataSource>
<div class='table - responsive'>
<asp:GridView ID="objectivesGridView" runat="server" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="Id" AllowPaging="True" CellPadding="3" OnPageIndexChanging="objectivesGridView_PageIndexChanging" OnRowDeleting="objectivesGridView_RowDeleting" OnRowEditing="objectivesGridView_RowEditing" OnRowUpdating="objectivesGridView_RowUpdating" OnRowCancelingEdit="objectivesGridView_RowCancelingEdit" PageSize="5" BackColor="#ffffff" BorderColor="#ffffff" BorderStyle="None" BorderWidth="0px" CellSpacing="2" OnRowCommand="objectivesGridView_RowCommand" class='table table - striped table - sm'>
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
<asp:TemplateField HeaderText="Objective" SortExpression="Objective">
<EditItemTemplate>
<asp:TextBox ID="txtObjective" Width="100px" runat="server" Text='<%# Bind("Objective") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtObjective" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Objective") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tasklist" SortExpression="Tasklist">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="name" DataValueField="Id">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="name" DataValueField="Id">
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Tasklist") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
<EmptyDataTemplate>
<h3>No available objectives for the selected tasklist! Create a new one?</h3>
<form role="form">
<div class="form-group">
<label class="control-label" for="name">Name</label>
<asp:TextBox ID="newObjective" type="text" class="form-control" placeholder="Enter Objective" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<div class="btn-group dropdown">
<asp:Button ID="tasklistDropdownLabel" type="button" class="btn btn-light dropdown-toggle" runat="server" Text="Tasklists" style="padding-left: 0.4em;" />
<asp:DropDownList ID="tasklistDropdown" type="button" class="btn btn-light" runat="server" style="position:relative; left:-0.3em; top: 0.1em; padding-left: 0.1em;" DataSourceID="SqlDataSource2" DataTextField="name" DataValueField="Id"></asp:DropDownList>
</div>
</div>
<asp:Button ID="newObjectiveSubmitButton" class="btn btn-default" runat="server" Text="Submit" />
</form>
</EmptyDataTemplate>
<%--<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />--%>
<SelectedRowStyle BackColor="#505050" Font-Bold="True" ForeColor="White" />
<%--<HeaderStyle BackColor="white" Font-Bold="True" ForeColor="White" />--%>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="SELECT [name], [Id] FROM [tasklist]" ProviderName="System.Data.SqlClient"></asp:SqlDataSource>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../Scripts/jquery-3.3.1.slim.min.js"></script>
<script>window.jQuery || document.write('<script src="../Scripts/jquery-3.3.1-slim.min.js"><\/script>')</script>
<script src="../Scripts/umd/popper.min.js"></script>
<script src="../Scripts/bootstrap.min.js"></script>
<!-- Icons -->
<script src="../Scripts/feather.min.js"></script>
<script>
feather.replace()
</script>
<!-- Custom Scripts -->
<script>
var tables = document.getElementsByTagName('table');
for (var i = 0; i < tables.length; i++)
{
document.getElementsByTagName('table')[i].className += " table-hover";
document.getElementsByTagName('table')[i].className += " thead-dark";
document.getElementsByTagName('table')[i].className += " table-responsive-sm";
}
</script>
</form>
index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class index : System.Web.UI.Page
{
SqlConnection connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand();
DataTable table = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
showTasklists();
}
}
private void showTasklists()
{
try
{
command.Connection = connection;
command.CommandText = "select * from tasklist";
adapter = new SqlDataAdapter(command);
adapter.Fill(table);
tasklistsGridView.DataSource = table;
tasklistsGridView.DataBind();
}
catch (Exception x)
{
Response.Write("Exception: " + x.Message);
}
}
protected void tasklistsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
tasklistsGridView.PageIndex = e.NewPageIndex;
showTasklists();
}
protected void tasklistsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
command.Connection = connection;
command.CommandText = "delete from tasklist where Id='" + tasklistsGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
showTasklists();
}
catch (Exception x)
{
Response.Write("Exception: " + x.Message);
}
}
protected void tasklistsGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
tasklistsGridView.EditIndex = e.NewEditIndex;
showTasklists();
}
protected void tasklistsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
TextBox txtName = (TextBox)tasklistsGridView.Rows[e.RowIndex].FindControl("txtName");
TextBox txtDescription = (TextBox)tasklistsGridView.Rows[e.RowIndex].FindControl("txtDescription");
TextBox txtPrerequisites = (TextBox)tasklistsGridView.Rows[e.RowIndex].FindControl("txtPrerequisites");
DropDownList ddlWorkgroup = (DropDownList)tasklistsGridView.FooterRow.FindControl("DropDownList1");
TextBox txtTag = (TextBox)tasklistsGridView.Rows[e.RowIndex].FindControl("txtTag");
command.Connection = connection;
command.CommandText = "update tasklist set name ='" + txtName.Text + "',description ='" + txtDescription.Text + "',prerequisites ='" + txtPrerequisites.Text + "',workgroup ='" + ddlWorkgroup.SelectedItem.ToString() + "',tag ='" + txtTag.Text + "' WHERE Id='" + tasklistsGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
connection.Open();
command.ExecuteNonQuery();
tasklistsGridView.EditIndex = -1;
showTasklists();
connection.Close();
}
catch (Exception x)
{
Response.Write("Exception: " + x.Message);
}
}
protected void tasklistsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
tasklistsGridView.EditIndex = -1;
showTasklists();
}
protected void tasklistsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtName = (TextBox)tasklistsGridView.FooterRow.FindControl("txtName");
TextBox txtDescription = (TextBox)tasklistsGridView.FooterRow.FindControl("txtDescription");
TextBox txtPrerequisites = (TextBox)tasklistsGridView.FooterRow.FindControl("txtPrerequisites");
DropDownList ddlWorkgroup = (DropDownList)tasklistsGridView.FooterRow.FindControl("DropDownList1");
TextBox txtTag = (TextBox)tasklistsGridView.FooterRow.FindControl("txtTag");
command.Connection = connection;
command.CommandText = "insert into tasklist(name, description,prerequisites,workgroup,tag) Values('" + txtName.Text + "', '" + txtDescription.Text + "', '" + txtPrerequisites.Text + "', '" + ddlWorkgroup.SelectedItem.ToString() + "', '" + txtTag.Text + "')";
connection.Open();
command.ExecuteNonQuery();
showTasklists();
connection.Close();
}
}
catch (Exception x)
{
Response.Write("Exception: " + x.Message);
}
}
protected void tasklistsGridView_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["tasklistid"] = tasklistsGridView.SelectedRow.Cells[0].Text;
try
{
//2. Open Connection
connection.Open();
//3. Create and Execute Command
string query = "select Id, objective from objectives where tasklistId='" + Convert.ToInt32(ViewState["tasklistid"]) + "';";
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
DataTable Table = new DataTable();
Table.Load(reader);
objectivesGridView.DataSource = Table;
objectivesGridView.DataBind();
//4. Close connection
connection.Close();
}
catch (Exception x)
{
Response.Write("exception: " + x.Message);
}
}
private void showObjectives()
{
try
{
command.Connection = connection;
command.CommandText = "select * from objectives";
adapter = new SqlDataAdapter(command);
adapter.Fill(table);
objectivesGridView.DataSource = table;
objectivesGridView.DataBind();
}
catch (Exception x)
{
Response.Write("showObjectivesException: " + x.Message);
}
}
protected void objectivesGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
objectivesGridView.PageIndex = e.NewPageIndex;
showObjectives();
}
protected void objectivesGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
command.Connection = connection;
command.CommandText = "delete from objectives where Id='" + objectivesGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
showObjectives();
}
catch (Exception x)
{
Response.Write("objectivesGridView_RowDeletingException: " + x.Message);
}
}
protected void objectivesGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
objectivesGridView.EditIndex = e.NewEditIndex;
showObjectives();
}
protected void objectivesGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
TextBox txtObjective = (TextBox)objectivesGridView.Rows[e.RowIndex].FindControl("txtObjective");
DropDownList ddlObjectives = (DropDownList)objectivesGridView.FooterRow.FindControl("DropDownList2");
command.Connection = connection;
command.CommandText = "update objectives set objective ='" + txtObjective.Text + "',tasklistId ='" + ddlObjectives.SelectedItem + "' WHERE Id='" + objectivesGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
connection.Open();
command.ExecuteNonQuery();
objectivesGridView.EditIndex = -1;
showObjectives();
connection.Close();
}
catch (Exception x)
{
Response.Write("objectivesGridView_RowUpdatingException: " + x.Message);
}
}
protected void objectivesGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
objectivesGridView.EditIndex = -1;
showObjectives();
}
protected void objectivesGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtObjective = (TextBox)objectivesGridView.FooterRow.FindControl("txtObjective");
DropDownList ddlObjective = (DropDownList)objectivesGridView.FooterRow.FindControl("DropDownList2");
command.Connection = connection;
command.CommandText = "insert into objectives(objective,tasklistId) Values('" + txtObjective.Text + "', '" + ddlObjective.SelectedItem + "')";
connection.Open();
command.ExecuteNonQuery();
showObjectives();
connection.Close();
}
}
catch (Exception x)
{
Response.Write("objectivesGridView_RowCommandException: " + x.Message);
}
}
}
Here's the output snap (you might want to look at that red arrow on the top)
答案 0 :(得分:0)
检查所有查询是否都返回名为Tasklist
的字段<asp:Label ID="Label5" runat="server" Text='<%# Bind("Tasklist")%>'></asp:Label>