0

好吧,既然没有人能够确定为什么我的 DataTable 无法获取更新的 GridView 数据,我现在正尝试直接从 GridView 获取值。

现在的问题是我从阅读 GridViewRows 获得的文本值都是空字符串。

这是我的代码:

ASPX Markup code for the GridView:

<asp:gridview ID="ESBAndTSRValuesInputGridView" runat="server" ShowFooter="true" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundField DataField="AwardID" HeaderText="Award ID" Visible="false" />
    <asp:BoundField DataField="AwardName" HeaderText="Award Name" />
    <asp:TemplateField HeaderText="ESBValue">
        <ItemTemplate>
            <asp:TextBox ID="ESBValue" Text='<%# Eval("ESBValue") %>' runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="TSRValue">
        <ItemTemplate>
            <asp:TextBox ID="TSRValue" Text='<%# Eval("TSRValue") %>'  runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:gridview>

从按钮单击的代码隐藏中初始化 GridView 数据:

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("AwardID", typeof(string)));
dt.Columns.Add(new DataColumn("AwardName", typeof(string)));
dt.Columns.Add(new DataColumn("ESBValue", typeof(string)));
dt.Columns.Add(new DataColumn("TSRValue", typeof(string)));
DataRow[] PSPAwards = dtAwards.Select("AWARDTYPE = 'PSP'");
foreach (DataRow dr in PSPAwards)
{
    dt.Rows.Add(dr["AWARDID"].ToString(), dr["AWARDNAME"].ToString(), "0", "100");
}
ViewState["ESBAndTSRValuesDataTable"] = dt;
ESBAndTSRValuesInputGridView.DataSource = dt;
ESBAndTSRValuesInputGridView.DataBind();

尝试读取 GridViewRow 值:

for (int rowCount = 0; rowCount < ESBAndTSRValuesInputGridView.Rows.Count; rowCount++)
{

    int cell0Val, cell1Val, cell2Val, cell3Val;

    //input text is blank for all:
    //TryParse returns false for all (because if I use Parse instead, exception occurs):

    Int32.TryParse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[0].Text, out cell0Val);
    Int32.TryParse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[1].Text, out cell1Val);
    Int32.TryParse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[2].Text, out cell2Val);
    Int32.TryParse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[3].Text, out cell3Val);

    int val1 = Int32.Parse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[0].Text);
    int val2 = Int32.Parse(dr["AWARDID"].ToString());

    if (val1 == val2)
    {
        rowIndex = rowCount;
        break;
    }
}

那么在阅读 GridViewRows 时如何获取实际的文本值?

我需要 BoundFields 的值以及 TextFields ..

我正在使用带有 .NET 2.0 Framework 的 Visual Studio 2005 ..

4

1 回答 1

2

用这个替换你的最后一个鳕鱼块

你不能直接从文本框中获取值..你必须通过 id 找到该控件

网格视图

<asp:GridView ID="ESBAndTSRValuesInputGridView" runat="server" ShowFooter="true"
    AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="Award Name">
            <ItemTemplate>
                <asp:Label ID="lblAwardID"  runat="server" Text='<%# Eval("AwardID") %>' Style="display: none;"></asp:Label>
                <asp:Label ID="lblAwardName"  runat="server" Text='<%# Eval("AwardName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ESBValue">
            <ItemTemplate>
                <asp:TextBox ID="ESBValue" Text='<%# Eval("ESBValue") %>' runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="TSRValue">
            <ItemTemplate>
                <asp:TextBox ID="TSRValue" Text='<%# Eval("TSRValue") %>' runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

获取值

 for (int rowCount = 0; rowCount < ESBAndTSRValuesInputGridView.Rows.Count; rowCount++)
        {

            int cell0Val, cell1Val, cell2Val, cell3Val;

            //input text is blank for all:
            //TryParse returns false for all (because if I use Parse instead, exception occurs):

            Int32.TryParse(((Label)ESBAndTSRValuesInputGridView.Rows[rowCount].FindControl("lblAwardID")).Text, out cell0Val);
            Int32.TryParse(((Label)ESBAndTSRValuesInputGridView.Rows[rowCount].FindControl("lblAwardName")).Text, out cell1Val);
            Int32.TryParse(((TextBox)ESBAndTSRValuesInputGridView.Rows[rowCount].FindControl("ESBValue")).Text, out cell2Val);
            Int32.TryParse(((TextBox)ESBAndTSRValuesInputGridView.Rows[rowCount].FindControl("TSRValue")).Text, out cell3Val);
            Int32.TryParse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[3].Text, out cell3Val);

            int val1 = Int32.Parse(ESBAndTSRValuesInputGridView.Rows[rowCount].Cells[0].Text);
            int val2 = Int32.Parse(dr["AWARDID"].ToString());

            if (val1 == val2)
            {
                rowIndex = rowCount;
                break;
            }
        }
于 2013-07-01T09:21:39.593 回答