我遇到的问题是我的 GridView 的 RowCommand 不会触发。我已经阅读了数百万篇文章,结果我觉得我更加困惑。所以,如果你能看到我在这里做错了什么,请指出。
几周前我确实问过一个类似的问题,但我使用的是嵌套在数据列表中的网格视图并使用 EntityDataSource 的“包含”兄弟姐妹来显示兄弟姐妹,这是与推荐关系的多方面,这很好显示,但弄清楚编辑更新和删除是一场噩梦。所以我已经把它简化了,但现在我停在了我的轨道上,因为我在回发的某个地方失去了控制。
我有一个带有 ContentPlaceholder 的母版页:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="HomelessStudent.Web.SiteMasterPage" ViewStateMode="Inherit" EnableViewState="True" %>
<asp:ContentPlaceHolder ID="ContentPlaceHolderAgent" runat="server">
</asp:ContentPlaceHolder>
在代理页面上-
<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolderAgent" runat="server">
<asp:Panel ID="SiblingPanel" runat="server" ViewStateMode="Enabled" Visible="True">
<asp:GridView ID="SiblingGridView" runat="server" CssClass="grid"
AutoGenerateColumns="false"
DataKeyNames="Id"
ShowFooter="true"
OnDataBound="SiblingGridView_DataBound"
OnRowEditing="SiblingGridView_RowEditing"
OnRowUpdating="SiblingGridView_RowUpdating"
OnRowCommand="SiblingGridView_RowCommand"
OnRowDeleting="SiblingGridView_RowDeleting" ViewStateMode="Enabled" ClientIDMode="Static">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="SiblingName">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# Bind("SiblingName") %>' ID="TextBox1"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("SiblingName") %>' ID="Label1"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="NewSiblingName" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<EditItemTemplate>
<asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateLinkButton" ></asp:LinkButton> <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" CausesValidation="False" ID="LinkButton2"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditLinkButton" ClientIDMode="Static"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton runat="server" CommandName="AddNew" Text="Add" ID="AddNewLinkButton"></asp:LinkButton> </FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowHeader="True" HeaderText="Delete"></asp:CommandField>
</Columns>
</asp:GridView>
</asp:Panel>
后面的代码(在此处绑定数据,因为仅显示属于特定记录的那些兄弟姐妹,该记录的 ID(Guid)在 queryString 中,也许......
if (IsPostBack == false)
{
if (Request.QueryString["id"] == null)
{
if (Session["studentid"] == null)
{
Response.Redirect("StudentPage.aspx");
}
else
{
referral = GetMostRecentReferral((String)Session["studentid"]);
PopulateUI(referral);
}
}
else
{
referral = GetThisReferral(Request.QueryString["id"]);
PopulateUI(referral);
}
}
然后,在 PopulateUI(referral)
some stuff...
FillSiblingGrid();
String studentId = ((referral.StudentID).ToString()).Trim();
getSelectedStudentDeatails(studentId);
Session["referralid"] = (Guid)referral.Id;
并填充兄弟姐妹网格-
private void FillSiblingGrid()
{
if (referral != null)
{
List<Sibling> siblings = new List<Sibling>();
using (HomelessStudentDataEntities db = new HomelessStudentDataEntities())
{
siblings = (from s in db.Siblings
where s.ReferralID == referral.Id
select s).ToList();
}
if (siblings.Count > 0)
{
SiblingGridView.DataSource = siblings;
SiblingGridView.DataBind();
}
else
{
int TotalColumns = SiblingGridView.Rows[0].Cells.Count;
SiblingGridView.Rows[0].Cells.Clear();
SiblingGridView.Rows[0].Cells.Add(new TableCell());
SiblingGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
SiblingGridView.Rows[0].Cells[0].Text = "No siblings found";
}
}
}