0

我现在被困住了。在花费很长时间试图将动态添加的链接按钮或标签维护到网格视图之后,似乎我在回发时覆盖了复选框状态。

如前所述,我正在使用以下内容动态地将链接按钮或标签添加到网格视图中的占位符:

protected void LedgerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int index = e.Row.RowIndex;

            if (items[index].noteID > 0)
            {

                PlaceHolder label = (PlaceHolder)e.Row.FindControl("HasPrevious");
                LinkButton link = new LinkButton();
                link.CommandName = "LinkCommand";
                link.Command += new CommandEventHandler(link_Command);
                link.Text = "Yes";
                link.ID = index.ToString();
                label.Controls.Add(link);

            }
            else
            {
                PlaceHolder label = (PlaceHolder)e.Row.FindControl("HasPrevious");
                Label noNote = new Label();
                noNote.Text = "No";
                label.Controls.Add(noNote);
            }

这是网格视图:

<asp:GridView runat="server" ID="LedgerGrid" AutoGenerateColumns="false" 
    onselectedindexchanged="LedgerGrid_SelectedIndexChanged" 
    onrowdatabound="LedgerGrid_RowDataBound" onrowcommand="LedgerGrid_RowCommand" 
    >
<Columns>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
  <asp:PlaceHolder ID="HasPrevious" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Invoice Date" DataField="invoicedate" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="Invoice" DataField="invoice" />
<asp:BoundField HeaderText="Fee Debt" DataField="Fee_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Cost Debt" DataField="Cost_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="VAT Debt" DataField="VAT_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Total Debt" DataField="Total_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Client Code" DataField="ClientCode" />
<asp:ButtonField  HeaderText="Matter Number" DataTextField="matternumber" CommandName="ViewMatter" />
<asp:BoundField HeaderText="Decription" DataField="matterdescription" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="AddInvoiceCheck" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

为了确保我的链接按钮命令有效,我正在重建 gridview,就像在 Page_load 中一样。这是页面加载:

 protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            string clientCode = Server.HtmlEncode(Request.QueryString["clientcode"]);
            items = DataAccess.DataAccess.GetLedgerDetails(clientCode);
            ViewState["LedgerItems"] = items;
            ViewState["clientcode"] = clientCode;
            LedgerGrid.DataSource = items;
            LedgerGrid.DataBind();
            LedgerClientObject clientDetails = DataAccess.DataAccess.GetClientDetails(clientCode);
            ClientCodeLabel.Text = string.Format("Group Code: {0}", clientDetails.GroupCode);
            ClientNameLabel.Text = string.Format("Client Name: {0}", clientDetails.ClientName);
            ClientCodeFilter.DataSource = clientDetails.ClientCodes;
            ClientCodeFilter.DataBind();


        }
    } 

为了维护回发时动态添加的控件,我调用以下命令:

protected void Page_PreLoad(object sender, EventArgs e)
    {


        if (IsPostBack)
        {
            items = (List<LedgerItem>)ViewState["LedgerItems"];
            LedgerGrid.DataSource = items;
            LedgerGrid.DataBind();


        }


    }

当然,现在看来,通过使用这种方法,我正在覆盖复选框 gridview 列的状态。我接近这一切都错了吗?我希望能够使用视图状态来维护我动态添加到网格视图的控件的设置。有没有办法可以在 PreLoad 事件中重新加载 gridview 之前/之后访问复选框控件表单 gridview 的状态?

我正在通过单击按钮检查复选框列的状态,如下所示:

        protected void Unnamed1_Click(object sender, EventArgs e)
    {
        NoteModel note = new NoteModel();
        for (int i = 0; i < LedgerGrid.Rows.Count; i++)
        {
            int invoice = Convert.ToInt32(LedgerGrid.Rows[i].Cells[2].Text);
            CheckBox check = (CheckBox)LedgerGrid.Rows[i].FindControl("AddInvoiceCheck");
            if (check.Checked)
            {
                note.invoices.Add(invoice);

            }



        }

        if (note.invoices == null)
        {

            string clientcode = (string)ViewState["clientcode"];
            ViewState["InvoiceError"] = 1;
            Response.Redirect(string.Format("Ledger.aspx?clientcode={0}", clientcode));
        }
        else
        {
            string clientcode = (string)ViewState["clientcode"];
            Session["NoteObject"] = note;
            Response.Redirect(string.Format("AddNote.aspx?cc={0}", clientcode));

        }


    }

每当我遍历 gridview 中的复选框控件时,它们都未选中,例如被我的 PreLoad 代码覆盖!!!

4

1 回答 1

1

最后我决定不使用动态创建的控件。相反,我在网格视图中添加了一个按钮域。然后,在我的 rowdatabound 事件中,如果它确实符合我的标准,我将按钮的可见性设置为 false。这种方式保持视图状态,因为我不需要在回发时创建控制。

似乎最好的选择是尽可能避免动态控制的网络表单。我仍然想知道要利用哪个事件来覆盖视图状态恢复控件?

于 2012-10-19T10:34:55.267 回答