2014-04-02 48 views
0

我在頁面上包含幾個標籤的DetailsView,並且我希望在頁面加載事件的這些標籤中放置一些值..但iam無法將值分配給這裏的標籤是我的代碼如何爲頁面加載事件detailsview中存在的標籤添加值

IAM試圖在兩者的OnPreRender()和OnDataBound()事件,但IAM獲得空指針異常..

有人幫助我在this..iam掙扎了幾天

<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

喜檢查第一mydetailsViews.Rows.Count()> 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();