I'm having trouble retrieving the value of a cell in a gridview.
I think it may have something to do with the item template calling a function since I am able to retrieve the values of cells that are not calling a function in the same gridview.
How can i extract the displayed value from this cell?
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
lblTest.Text=row.Cells[7].Text;
}
<ItemTemplate>
<%# GetWednesday(decimal.Parse(Eval("Wed").ToString())).ToStrin开发者_JAVA百科g("N2") %>
</ItemTemplate>
Edit:
Poking around a little more, it appears your content is there, but not as text. It is a DataBoundLiteralControl
in the cell's Controls
collection. You can get the value with:
var value = (row.Cells[7].Controls[0] as DataBoundLiteralControl).Text.Trim();
Use of the Trim()
function is necessary to remove leading and trailing whitespace above and below the binding statement in your template.
I still prefer the approach described below.
End
If I understand correctly, I think your value is not stored in ViewState.
You can get at your text with this alternate approach:
<ItemTemplate>
<asp:Label ID="mylabel" runat="server"
Text='<%# GetWednesday(decimal.Parse(Eval("Wed").ToString())).ToString("N2") %>' />
</ItemTemplate>
And from code behind:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
var label = GridView1.SelectedRow.FindControl("mylabel") as Label;
lblTest.Text = label.Text;
}
精彩评论