2013-11-20 76 views
0

我想從後面的代碼綁定gridview。其中一列包含一個按鈕。該按鈕的文本應根據在row.For例如加載基於內容的按鈕,應該說無論是發佈或取消發佈內容改變。這是我所做的。從c設置gridview按鈕文本#

的GridView中的aspx代碼:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"> 

          <Columns> 
           <asp:BoundField HeaderText="Name" DataField="Name"> 
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle> 
           </asp:BoundField> 
           <asp:BoundField HeaderText="Sub Category" DataField="Sub Category"></asp:BoundField> 
           <asp:ImageField HeaderText="Image" DataImageUrlField="Image" ControlStyle-Height="39px" 
            ControlStyle-Width="150px"> 
           </asp:ImageField> 
           <asp:BoundField HeaderText="Start Date" DataField="Start Date"></asp:BoundField> 
           <asp:BoundField HeaderText="Expiry Date" DataField="Expiry Date"></asp:BoundField> 
           <asp:ButtonField HeaderText="Status" DataTextField="Status" ButtonType="Button" CommandName="publishButton_Click" /> 
          </Columns> 
                </asp:GridView> 

而這背後是我的代碼:

protected void Page_Load(object sender, EventArgs e){ 
GridView1.DataSource = fillOfferGridView(nodeid); 
      GridView1.DataBind(); 
} 
DataTable fillOfferGridView(int nodeId) 
     { 
      Document next = new Document(nodeId); 
      Button bf = new Button(); 
      DataTable dtOfferDetails = new DataTable(); 
      dtOfferDetails.Columns.Add("Name", typeof(string)); 
      dtOfferDetails.Columns.Add("Sub Category", typeof(string)); 
      dtOfferDetails.Columns.Add("Image", typeof(string)); 
      dtOfferDetails.Columns.Add("Start Date", typeof(string)); 
      dtOfferDetails.Columns.Add("Expiry Date", typeof(string)); 
      dtOfferDetails.Columns.Add("Status", typeof(Button)); 

      foreach (Document offer in next.Children) 
      { 
       DataRow dr = dtOfferDetails.NewRow(); 
       //string myOfferName = offer.GetProperty("offerName").Value; 
       //string offerNodeName = offer.Name; 
       //string offerurl = offer.NiceUrl; 
       //string offerType = offer.GetProperty("offerType").Value; 
       //string offerImage = offer.GetProperty("offerImage").Value; 
       dr["Name"] = offer.getProperty("offerName").Value; 
       dr["Sub Category"] = offer.getProperty("offerType").Value; 
       dr["Image"] = ResolveUrl(offer.getProperty("offerImage").ToString()); 
       dr["Start Date"] = offer.getProperty("offerLaunchDate").Value; 
       dr["Expiry Date"] = offer.getProperty("offerExpiryDate").Value; 
       if (offer.Published == true) 
       { 
        bf.Text = "Unpublish"; 
       } 
       else 
       { 
        bf.Text = "Publish"; 
       } 
       dr["Status"] = bf; 
       dtOfferDetails.Rows.Add(dr); 
      } 
      return dtOfferDetails; 
     } 

但是當我運行的代碼,而不是期望的文本我得到的System.Web.UI 。按鈕上的.webcontrols.button。我究竟做錯了什麼?

回答

1
dr["Status"] = bf.Text; 

如導致你有system.web.ui.webcontrols.button文本,而不是按鈕標題,如果你把你的狀態字段的DataGrid按鈕即可調用它的toString()方法答。

甚至更​​多。您不需要在代碼中創建新的Button。只要寫:

if (offer.Published == true) 
       { 
        dr["Status"] = "Unpublish"; 
       } 
       else 
       { 
        dr["Status"] = "Publish"; 
       } 
+1

也改變dtOfferDetails.Columns.Add(「狀態「,typeof(Button)); dtOfferDetails.Columns.Add(「Status」,typeof(string))。 – matk

+0

現在謝謝了:) – nitinvertigo

1

這條線必須是:

dr["Status"] = bf.Text; 
2

Ammend此行

dr["Status"] = bf; 

dr["Status"] = bf.Text;