Dear Experts,

I have a modal popup and a GridView in it. The GridView is populated programmatically, i.e. I use .DataBind(). I can enter data into some of the GridView's controls. After I press Submit, I expect to do something with the entered data. But in the button press handler, the GridView is empty. The data entered e.g. into a TextBox is preserved, it is only GridView that has this problem. I din't set EnableViewState of the page or of the GridView to false, moreover, I explicitly set it to true.
Maybe somebody could help me on this?

Thanks,
Dmitriy

show me your markup and code behind...!

<asp:Button ID="btnSubmitOffer" runat="server" Text="Submit Offer" 
        Width="100px" onclick="btnSubmitOffer_Click" />  
...
    <asp:Button ID="btnShowSubmitOfferPopup" runat="server" style="display:none" />
...
    <asp:Panel ID="pnlSubmitOffer" runat="server" CssClass="modalPopup" Style="display:none" Width="700px" >
        <asp:GridView ID="SubmitOfferGridView" runat="server" AllowSorting="True"
                AutoGenerateColumns="False" 
                Width="100%" Height="306px" DataKeyNames="EAN" >
            <AlternatingRowStyle CssClass="alternatingrowstyle" />
            <Columns>
                <asp:BoundField DataField="EAN" HeaderText="EAN" SortExpression="EAN" />
                <asp:TemplateField HeaderText="Offer Date" SortExpression="OfferDate">
                    <ItemTemplate>   
                        <asp:ImageButton runat="Server" ID="imgBtnOfferDate" 
                            ImageUrl="~/Images/Calendar_scheduleHS.png" 
                            AlternateText="Click here to display calendar" />
                        <asp:TextBox ID="OfferDate" runat="server" 
                            Text='<%# Bind("OfferDate","{0:M/d/yyyy}") %>'></asp:TextBox>
                        <ajaxToolkit:CalendarExtender ID="OfferDateCE" runat="server" 
                            TargetControlID="OfferDate" PopupButtonID="imgBtnOfferDate"/>                                
                        <asp:CompareValidator ID="OfferDateCompareValidator" runat="server" 
                            ControlToValidate="OfferDate" Display="Dynamic" 
                            ErrorMessage="Invalid OfferDate!" Text="*" Operator="DataTypeCheck" 
                            Type="Date"></asp:CompareValidator>
                        <ajaxToolkit:ValidatorCalloutExtender ID="OfferDateCompareValidator_ValidatorCalloutExtender" 
                            runat="server" Enabled="True" TargetControlID="OfferDateCompareValidator"></ajaxToolkit:ValidatorCalloutExtender>
                    </ItemTemplate>
                </asp:TemplateField>   
                <asp:TemplateField HeaderText="Amount" SortExpression="Amount">
                    <ItemTemplate>    
                        <asp:TextBox ID="Amount" runat="server" Text='<%#Bind("Amount") %>'></asp:TextBox>
                        <asp:CompareValidator ID="AmountCompareValidator" runat="server" 
                            ControlToValidate="Amount" Display="Dynamic" 
                            ErrorMessage="Amount has invalid currency value!" Text="*" 
                            Operator="DataTypeCheck" Type="Currency"></asp:CompareValidator>
                    </ItemTemplate>
                </asp:TemplateField>    
                <asp:TemplateField HeaderText="Expiration Date" SortExpression="ExpirationDate">
                    <ItemTemplate>   
                        <asp:ImageButton runat="Server" ID="imgBtnExpirationDate" 
                            ImageUrl="~/Images/Calendar_scheduleHS.png" 
                            AlternateText="Click here to display calendar" />
                        <asp:TextBox ID="ExpirationDate" runat="server" 
                            Text='<%# Bind("ExpirationDate","{0:M/d/yyyy}") %>'></asp:TextBox>
                        <ajaxToolkit:CalendarExtender ID="ExpirationDateCE" runat="server" 
                            TargetControlID="ExpirationDate" PopupButtonID="imgBtnExpirationDate"/>                                
                        <asp:CompareValidator ID="ExpirationDateCompareValidator" runat="server" 
                            ControlToValidate="ExpirationDate" Display="Dynamic" 
                            ErrorMessage="Invalid ExpirationDate!" Text="*" Operator="DataTypeCheck" 
                            Type="Date"></asp:CompareValidator>
                        <ajaxToolkit:ValidatorCalloutExtender ID="ExpirationDateCompareValidator_ValidatorCalloutExtender" 
                            runat="server" Enabled="True" 
                            TargetControlID="ExpirationDateCompareValidator"></ajaxToolkit:ValidatorCalloutExtender>
                    </ItemTemplate>
                </asp:TemplateField>                                                  
            </Columns>
            <HeaderStyle CssClass="headerstyle" />
        </asp:GridView> 

        <div align="center">
            <asp:Button ID="btnSubmitSubmitOffer" runat="server" Text="Submit" OnClick="btnSubmitSubmitOffer_Click" />
            <asp:Button ID="btnCancelSubmitOffer" runat="server" Text="Cancel" />
        </div>
    </asp:Panel>

    protected void btnSubmitOffer_Click(object sender, EventArgs e)
    {
        RememberOldValues();    // Store in Session keys of the checked rows on the current page

        var keys = Session["CHECKED_ITEMS"] as Dictionary<string, Offer>;

        if (keys == null || keys.Count == 0)
        {
            SetWarningLabel("You did not select any records.");
            return;
        }

        DataTable submitOfferTable = new DataTable();
        DataColumn columnEAN = new DataColumn("EAN", Type.GetType("System.String"));
        columnEAN.Unique = true;
        submitOfferTable.Columns.Add(columnEAN);
        DataColumn columnOfferDate = new DataColumn("OfferDate", Type.GetType("System.DateTime"));
        submitOfferTable.Columns.Add(columnOfferDate);
        DataColumn columnAmount = new DataColumn("Amount", Type.GetType("System.Double"));
        submitOfferTable.Columns.Add(columnAmount);
        DataColumn columnExpirationDate = new DataColumn("ExpirationDate", Type.GetType("System.DateTime"));
        submitOfferTable.Columns.Add(columnExpirationDate);

        foreach (string EAN in keys.Keys)
        {
            DataRow submitOfferRow = submitOfferTable.NewRow();
            submitOfferRow["EAN"] = EAN;
            submitOfferRow["OfferDate"] = DateTime.Now;
            submitOfferRow["Amount"] = keys[EAN].Amount;
            submitOfferRow["ExpirationDate"] = (keys[EAN].Date == DateTime.MinValue) ? DateTime.MinValue : keys[EAN].Date.AddDays(63);   // by default, expiration date is PubDate + 9 weeks

            submitOfferTable.Rows.Add(submitOfferRow);
        }

        SubmitOfferGridView.DataSource = submitOfferTable;
        SubmitOfferGridView.DataBind();

        SubmitOfferModalPopupExtender.Show();
    }

   protected void btnSubmitSubmitOffer_Click(object sender, EventArgs e)
    {
        var keys = Session["CHECKED_ITEMS"] as Dictionary<string, Offer>;

        if (keys == null || keys.Count == 0)
            return;

        string commandText;
        string FailedUpdatesEAN = string.Empty;
        string FailedInsertsEAN = string.Empty;

        foreach (GridViewRow row in SubmitOfferGridView.Rows)
        {
            // First update the BookQuest table setting TantorStatus for selected records to 1 (On Offer)
            commandText =
                "UPDATE BookQuest SET TantorStatus = 1, Color = " + ColorTranslator.ToOle(Color.LightBlue) +
                " WHERE EAN = '" + row.Cells[0].Text + "';";

            object result = DBOperations.Execute(true, CommandType.Text, null, commandText, null);
            int recordsUpdated = 0;
            if (result != null && !Int32.TryParse(result.ToString(), out recordsUpdated)) // exception was thrown: if the returned value is not the number of updated records, it is an error message
            {
                FailedUpdatesEAN += ((FailedUpdatesEAN == string.Empty) ? "" : ", ") + row.Cells[0].Text;
                continue;
            }

            // Now insert Offer Details into the BookQuest_Offer table
            commandText =
                "INSERT INTO BookQuest_Offer (OfferDate, Amount, ExpirationDate, BookQuestID) VALUES('" +
                Convert.ToDateTime(((TextBox)row.FindControl("OfferDate")).Text).ToString("yyyy:MM:dd hh:mm:ss") + "'," +
                Convert.ToDouble(((TextBox)row.FindControl("Amount")).Text) + ",'" +
                Convert.ToDateTime(((TextBox)row.FindControl("ExpirationDate")).Text).ToString("yyyy:MM:dd hh:mm:ss") +
                "', (SELECT ID FROM BookQuest WHERE EAN = '" + row.Cells[0].Text + "'));";

            result = DBOperations.Execute(true, CommandType.Text, null, commandText, null);
            if (result != null && !Int32.TryParse(result.ToString(), out recordsUpdated)) // exception was thrown: if the returned value is not the number of updated records, it is an error message
            {
                FailedInsertsEAN += ((FailedInsertsEAN == string.Empty) ? "" : ", ") + row.Cells[0].Text;
                continue;
            }
        }

        if (FailedUpdatesEAN != string.Empty || FailedInsertsEAN != string.Empty)
        {
            string warning = string.Empty;

            if (FailedUpdatesEAN != string.Empty)
            {
                warning = "Failed to update Tantor Statur for products with following EAN: <br> " + FailedUpdatesEAN + ".<br><br>";
            }
            if (FailedInsertsEAN != string.Empty)
            {
                warning = "Updated Tantor Status, but failed to enter offer details for products with the following EAN: <br> " + FailedInsertsEAN + ".";
            }
            SetWarningLabel(warning);
        }
        Filter();
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.