我在页面上有一个DetailsView,其中包含很少的标签,我想在pageload事件上的那些标签中添加一些值..但是iam无法为这里的标签指定值是我的代码
iam尝试OnPreRender()和OnDataBound()事件但是我得到了nullpointer异常..
有人帮我这个......挣扎了好几天
<asp:DetailsView ID="gv_wifisummery" runat="server" Width="550px"
AutoGenerateRows="true" ForeColor="#333333" CellPadding="3"
CellSpacing="3" OnPreRender="DetailsView2_DataBound" OnDataBound="check" >
<Fields>
<asp:TemplateField HeaderText="Devices ">
<ItemTemplate>
<asp:Label ID="lbldevices" runat="server" Text='devices'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
<AlternatingRowStyle BackColor="White" Height="30px"/>
<CommandRowStyle BackColor="White" Font-Bold="True" ForeColor="Blue" />
<FieldHeaderStyle Font-Bold="True" Width="150px" />
<FooterStyle Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" Font-Size="12px" />
</asp:DetailsView>
- ============================================== ==============================================
protected void DetailsView2_DataBound(object sender, EventArgs e)
{
DetailsView myDetailsView = (DetailsView)sender;
myDetailsView.ChangeMode(DetailsViewMode.Insert);
//Insert
if (myDetailsView.CurrentMode == DetailsViewMode.Insert)
{
((Label)myDetailsView.FindControl("lbldevices")).Text = DateTime.Now.ToString("M/d/yyyy HH:mm");
}
}
protected void check(object sender, EventArgs e)
{
DetailsView myDetailsView = (DetailsView)sender;
if (myDetailsView.CurrentMode == DetailsViewMode.Insert)
{
// ((Label)myDetailsView.FindControl("lbldevices")).Text = DateTime.Now.ToString("M/d/yyyy HH:mm");
}
((Label)myDetailsView.FindControl("lbldevices")).Text = DateTime.Now.ToString("M/d/yyyy HH:mm");
}
答案 0 :(得分:0)
首先检查mydetailsViews.Rows.Count()&gt; 0。 我认为你的detailview没有行,这就是为什么它会抛出错误。
试试这个......
DataTable table = new DataTable();
table.Columns.Add("Devices");
DataRow dr = table.NewRow();
dr["Devices"] = DateTime.Now.ToString("M/d/yyyy HH:mm");
table.Rows.Add(dr);
myDetailsView.DataSource = table;
myDetailsView.DataBind();