Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:35:09+00:00 2026-06-13T04:35:09+00:00

I am now stuck. After spending ages trying to maintain dynamically added linkbutton or

  • 0

I am now stuck. After spending ages trying to maintain dynamically added linkbutton or label to a gridview it seems I am overwriting a checkbox state on post back.

As stated I am dynamically adding either a linkbutton or a label to a place holder in a gridview with the following:

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

Here is the 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>

To make sure that my link button command works I am rebuilding the gridview as I would in the Page_load. Here is the 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();


        }
    } 

To maintain the dynamically added controls on post back I am calling the following:

protected void Page_PreLoad(object sender, EventArgs e)
    {


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


        }


    }

Of course it now seems that by using this method I am overwriting the state of the check-box gridview column. Am I approaching this all wrong? I would like to be able to use the viewstate to maintain the set up of my dynamically added controls to the gridview. Is there a way I can access the state of the checkbox control form gridview before/after I reload the gridview in PreLoad event?

I am checking the state of my checkbox column with a button click as follows:

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

        }


    }

Whenever I iterate over the checkbox controls in the gridview they are all unchecked e.g overwritten by my PreLoad code!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:35:10+00:00Added an answer on June 13, 2026 at 4:35 am

    In the end I decided not to use dynamically created controls. Instead I added a buttonfield to the grid view. In my rowdatabound event I then set the visibility of the button to false if it does meet my criteria. This way view state is maintained as i do not need to create control on post back.

    Seems with webforms that avoiding dynamic controls where possible is the best bet. I would still like to know which event to tap into to override viewstate re-instating controls?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After fighting all morning, I got the handshake to work but now I'm stuck
I'm stuck after step 3 in trying to setup remote cross-debugging with Eclipse/RSE: Installed
I have now been stuck at this for some time so I though to
I'm fighting with raw sockets in Win32 and now I'm stuck, the soetsockopt function
I wrote some small apps using .NET 3.5 but now I am stuck with
I have an ANT configuration file which is becoming complicated, and now I'm stuck
I've made my own tree data structure via classes. Now I'm stuck with really
Got stuck here: http://jsfiddle.net/UFkg8/ Right now the animation is top-down. What do I need
I'm stuck here: Installing/Updating AsseticBundle 9c1b7269a4517d1ae94af2dc0d6d6fc4b31e6c10 HEAD is now at 41b5913 Merge pull request
I am stuck with facebook authentication for the last 5 hours now and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.