从GridView获取输入数据

时间:2015-11-12 16:20:18

标签: c# asp.net

我有一个表单,可以撤回一些数据供用户在数据库中更新。我将其拉入GridView。我有一个复选框和一个文本框,用于输入从DB返回的字段。我无法弄清楚如何访问用户输入的数据;它会一直作为空字符串返回(或者在复选框的情况下为false)。这是我的前端代码:

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

以下是我在后端尝试的内容:

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

    }
}

在后端,我尝试了不同的东西,例如:missingCounties.Rows[index].Cells[2].Text。我在哪里弄错用户输入数据?

作为免责声明,我在SO上查看了许多类似的问题,但没有一个问题已经解决/解决了这一特定问题。

2 个答案:

答案 0 :(得分:1)

听起来你的数据在回发时丢失了。这可能是解决方法而不是解决方案,但除了服务器端处理程序之外,您还可以向按钮添加客户端单击处理程序,并在其中将所需的值存储在隐藏字段中。当您点击服务器端点击处理程序时,您应该会在隐藏字段中找到这些值。

我确定必须有更好的方法来做到这一点,但我过去曾经使用过与GridView类似的解决方法。

答案 1 :(得分:0)

经过几个小时的挫折,我意识到我在回发中一直在进行数据绑定。

ASP.NET GridView does not update its row