内容页面上的Gridview RowCommand不会触发 - ViewState?

时间:2013-04-06 02:48:15

标签: c# visual-studio-2012 .net-4.5

我遇到的问题是我的GridView的RowCommand不会触发。我已经阅读了数百万篇帖子,结果我觉得我更加困惑。所以,如果你能看到具体是什么我在这里做错了,请指出。

几周前我确实问了一个类似的问题,但我使用嵌套在datalist中的gridview并使用EntityDataSource的'Include'兄弟姐妹来显示兄弟姐妹,这是与推荐关系的多方面,这对显示来说很好,但是搞清楚编辑更新和删除是噩梦。所以我已经把它简化了,但我现在已经停止了,因为我在回发中的某个地方失去了控制权。

我有一个包含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>&nbsp;<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(推荐)

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";
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。启用网格视图状态解决了问题