因为没有人能够确定my DataTable could not get the updated GridView data的原因,我现在正试图直接从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 ..
我正在使用Visual Studio 2005和.NET 2.0 Framework ..
答案 0 :(得分:2)
用这个替换你的最后一个鳕鱼块
你不能直接从文本框中获取值..你必须通过id
找到该控件gridview的
<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;
}
}