0

I have a form that pulls back some data for the user to update in the DB. I pull this into a GridView. I have a checkbox and a textbox for input on the field that was returned from the DB. I cannot figure out how to access the data that the user has inputted; it keeps coming back as empty strings (or false, in the case of the checkbox). Here is my front end code:

  <asp:GridView ID="missingCounties" runat="server" Width="300px" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" AutoGenerateColumns="False" OnRowCommand="newCountyInformation" DataKeyNames= "guID">
        <Columns>
        <asp:BoundField DataField="guID" HeaderText="guID" Visible="False"></asp:BoundField>
        <asp:BoundField DataField="CompanyName" HeaderText="CompanyName"></asp:BoundField>
        <asp:TemplateField>
            <HeaderTemplate>Is %</HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="percent" runat="server"/> 
                </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <HeaderTemplate>Fee</HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="fee" runat="server"/>  
                </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField>
            <HeaderTemplate>Submit</HeaderTemplate>
                <ItemTemplate>
                    <asp:LinkButton runat="server" CommandName="SubmitNewCounty"  Width="100px" Text="Submit" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"/>   
                </ItemTemplate>
        </asp:TemplateField>
    </Columns>
        <EmptyDataTemplate>
            <asp:Label ID="Label2" runat="server" Text="No new counties found."></asp:Label>
        </EmptyDataTemplate>
    </asp:GridView>

Here is what I've tried on the back end:

protected void newCountyInformation(object sender, GridViewCommandEventArgs e)
{
    var index = Convert.ToInt32(e.CommandArgument);
    var guID = missingCounties.DataKeys[index]["guID"].ToString();
    if (string.Equals(e.CommandName.ToLower(), "submitnewcounty"))
    {
        var percent = (CheckBox)missingCounties.Rows[index].Cells[2].FindControl("percent");
        var fee = (TextBox)missingCounties.Rows[index].Cells[3].FindControl("fee");

    }
}

In the backend, I've tried different things, such as: missingCounties.Rows[index].Cells[2].Text. Where am I going wrong with getting the user input data back?

As a disclaimer, I have looked over many similar questions on SO, but none of them have addressed/resolved this particular issue.

sparkyShorts
  • 630
  • 9
  • 28
  • 2
    At what point in the page lifecycle are you trying to access the data? – bingo Nov 12 '15 at 16:25
  • @bingo The call is made to the DB to populate the CompanyName and guID. The user then inputs the data and click the corresponding "Submit" button for the row they've inputted the data in (the Submit button is on the same row as the data). At that point, I try to access the data. Does that answer your question? – sparkyShorts Nov 12 '15 at 16:30

2 Answers2

1

Sounds like your data is being lost on postback. This is probably more of a workaround than a solution but you could add a client side click handler to your button in addition to the server side handler, and in it store the values you want in hidden fields. You should find the values persisted in the hidden fields when you hit the server side click handler.

I'm sure there must be a better way to do this but I've used a similar workaround with GridView myself in the past.

Ciara
  • 384
  • 2
  • 12
  • This looks to be along the lines of some of the suggested solutions in this answer. http://stackoverflow.com/questions/17589268/dynamically-created-controls-losing-data-after-postback. Let me give it a shot. – sparkyShorts Nov 12 '15 at 17:02
0

After many hours of frustration, I realized that I had been doing my databinding in my postback.

ASP.NET GridView does not update its row

Community
  • 1
  • 1
sparkyShorts
  • 630
  • 9
  • 28