动态控制Viewstate

时间:2012-10-16 22:41:16

标签: c# asp.net webforms

我现在卡住了。花了很长时间尝试维护动态添加链接按钮或标签到gridview后,似乎我在回发后覆盖了一个复选框状态。

如上所述,我正在使用以下内容向gridview中的占位符动态添加链接按钮或标签:

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

这是gridview:

<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列的状态。我接近这一切都错了吗?我希望能够使用viewstate来维护我对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代码覆盖!!!

1 个答案:

答案 0 :(得分:1)

最后我决定不使用动态创建的控件。相反,我在网格视图中添加了一个按钮字段。在我的rowdatabound事件中,我将按钮的可见性设置为false,如果它符合我的标准。这样维护视图状态,因为我不需要在回发时创建控件。

似乎webforms在可能的情况下避免动态控制是最好的选择。我仍然想知道要使用哪个事件来覆盖viewstate重新设置控件?