I'm having a problem trying to find a label control that is inside a GridView. Please see my codes below:
我在尝试找到GridView中的标签控件时遇到问题。请参阅下面的代码:
<asp:GridView ID="MyGridView" runat="server">
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtDate" MaxLength="10" Width="70" />
<asp:ImageButton ID="imgScoreDate" runat="server" ImageUrl="~/images/calendar.gif" />
<ajaxtoolkit:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True" Format="MM/dd/yyyy" TargetControlID="txtDate" PopupButtonID="imgDate" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is my .cs file:
这是我的.cs文件:
protected void LoadGridView()
{
//Do something else
foreach (GridViewRow row in MyGridView.Rows)
{
//Tried A
System.Web.UI.WebControls.Label lblName = row.FindControl("lblName") as System.Web.UI.WebControls.Label;
lblName.Text = "Name";
//Tried B
((System.Web.UI.WebControls.Label)row.FindControl("lblName")).Text = "Name";
}
}
I debug this code and it seems to work fine because my breakpoint is being hit each time the debugger runs. It even loops through my foreach
block the same count as to how many rows my GridView has.
我调试此代码,它似乎工作正常,因为我的断点正在每次调试器运行时被击中。它甚至在我的foreach块中循环与GridView有多少行相同。
But I don't understand why my lblName
control doesn't get the "Name" text as a value? Am I missing anything here? I tried both //Tried A
and //Tried B
methods but they both doesn't update my label's text.
但我不明白为什么我的lblName控件没有将“Name”文本作为值?我在这里遗漏了什么?我尝试了两个//尝试了A和//尝试过的B方法,但它们都没有更新我的标签文本。
Any help would be appreciated!
任何帮助,将不胜感激!
Thanks! Cheers!
谢谢!干杯!
3 个解决方案
#1
1
You want to call LoadGridView
inside PreRender
. Basically, you want to call it after GridView
is bound with data.
您想在PreRender中调用LoadGridView。基本上,您希望在GridView与数据绑定后调用它。
protected void Page_PreRender(object sender, EventArgs e)
{
LoadGridView();
}
Look at PreRender
event of ASP.NET Page Life Cycle.
查看ASP.NET页面生命周期的PreRender事件。
#2
1
On your gridview add:
在您的gridview上添加:
<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound
:
然后定义MyGridView_RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Label l = (Label) e.Row.FindControl("lblName");
}
What I think is happening is the control is not recreated server side in its current spot.
我认为正在发生的是控制不是在其当前位置重新创建服务器端。
#3
0
try this on .aspx page
在.aspx页面上试试这个
<asp:GridView ID="MyGridView" runat="server"
onrowdatabound="MyGridView_RowDataBound" .../>
code behind ::
代码背后::
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGridView();
}
}
void LoadGridView()
{
DataTable dt = new DataTable();
// dt= call ur database method to get data
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Name = (Label)e.Row.FindControl("lblName");
lbl_Name.Text = "Name";
}
}
cheers!
干杯!
#1
1
You want to call LoadGridView
inside PreRender
. Basically, you want to call it after GridView
is bound with data.
您想在PreRender中调用LoadGridView。基本上,您希望在GridView与数据绑定后调用它。
protected void Page_PreRender(object sender, EventArgs e)
{
LoadGridView();
}
Look at PreRender
event of ASP.NET Page Life Cycle.
查看ASP.NET页面生命周期的PreRender事件。
#2
1
On your gridview add:
在您的gridview上添加:
<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound
:
然后定义MyGridView_RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Label l = (Label) e.Row.FindControl("lblName");
}
What I think is happening is the control is not recreated server side in its current spot.
我认为正在发生的是控制不是在其当前位置重新创建服务器端。
#3
0
try this on .aspx page
在.aspx页面上试试这个
<asp:GridView ID="MyGridView" runat="server"
onrowdatabound="MyGridView_RowDataBound" .../>
code behind ::
代码背后::
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGridView();
}
}
void LoadGridView()
{
DataTable dt = new DataTable();
// dt= call ur database method to get data
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Name = (Label)e.Row.FindControl("lblName");
lbl_Name.Text = "Name";
}
}
cheers!
干杯!