2012-06-08 88 views
0

獲取單元格的值我想從字面上是一個GridView中獲取文本,在GridView控件

但是當我通過

無法投類型的System.Web.UI的對象運行的程序,例外。 LiteralControl類型System.Web.UI.DataBoundLiteralControl。

的.aspx代碼:

<asp:GridView ID="gridview3" runat="server" OnRowDataBound="RowDataBound" DataKeyNames="qno" AutoGenerateColumns="false" ShowFooter="true" OnRowCancelingEdit="cancel" OnRowCommand="create" OnRowDeleting="delete" OnRowEditing="edit" OnRowUpdating="update"> 

      <Columns> 
      <asp:TemplateField HeaderText="Selection"> 
      <ItemTemplate> 
       <asp:CheckBox ID="check1" runat="server"/> 
      </ItemTemplate> 

      </asp:TemplateField> 
      <asp:TemplateField HeaderText="ID" Visible="true"> 
     <ItemTemplate> 
      <asp:Label ID="id7" runat="server" Text='<%#Eval("assessid") %>' ></asp:Label> 

     </ItemTemplate> 
     </asp:TemplateField> 

    <asp:TemplateField HeaderText="Qno" Visible="true"> 
     <ItemTemplate> 
     <asp:DropDownList AppendDataBoundItems="true" AutoPostBack="true"  ID="DropDownList1" runat="server"> 
     <asp:ListItem ></asp:ListItem> 


     </asp:DropDownList> 

     </ItemTemplate> 



    </asp:TemplateField> 




     <asp:TemplateField HeaderText="description" Visible="true"> 
     <ItemTemplate> 

     <asp:Literal ID="id6" runat="server" Text='<%#Eval("description") %>' > 
     </asp:Literal> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:TextBox ID="TextBox1" Text='<%#Eval("description") %>' runat="server" ></asp:TextBox> 

     </EditItemTemplate> 
     <FooterTemplate> 

     <asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox> 




     </FooterTemplate> 


    </asp:TemplateField> 











    <asp:TemplateField HeaderText="strongagree" Visible="true"> 
     <EditItemTemplate> 
     = 

     <asp:TemplateField HeaderText="Action" Visible="true"> 

     <ItemTemplate> 
      <asp:LinkButton ID="LinkButton5" Text="Edit" CommandName="edit" runat="server"></asp:LinkButton> 
    </ItemTemplate> 
    <EditItemTemplate> 

     <asp:LinkButton ID="LinkButton1" Text="update" CommandName="update" runat="server"></asp:LinkButton> 

     <asp:LinkButton ID="LinkButton3" Text="cancel" CommandName="cancel" runat="server"></asp:LinkButton> 
    </EditItemTemplate> 
    <FooterTemplate> 

    <asp:LinkButton ID="LinkButton7" Text="DeleteAll" CommandName="delete" runat="server"></asp:LinkButton> 

    </FooterTemplate> 
</asp:TemplateField> 

    <asp:TemplateField HeaderText="" Visible="true"> 

     <ItemTemplate> 

      <asp:LinkButton ID="LinkButton23" Text="delete" CommandName="delete" runat="server"></asp:LinkButton> 




    </ItemTemplate> 

    <FooterTemplate> 
    <asp:Button ID="bdh" Text="insert " CommandName="insert" runat="server" /> 

    </FooterTemplate> 

</asp:TemplateField> 







</Columns> 






      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
      <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> 
      <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
      <SortedAscendingCellStyle BackColor="#FDF5AC" /> 
      <SortedAscendingHeaderStyle BackColor="#4D0000" /> 
      <SortedDescendingCellStyle BackColor="#FCF6C0" /> 
      <SortedDescendingHeaderStyle BackColor="#820000" /> 




     </asp:GridView> 

代碼背後

protected void RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     string percentage = ((LiteralControl)e.Row.Cells[2].Controls[0]).Text; 
    } 
} 

回答

2

你找回字面控制可能不是你想要的(有可能是一個文字匹配一個空白)。嘗試通過ID來獲得它:

string percentage = ((LiteralControl)e.Row.FindControl("id6").Text; 

或者是嘗試另一個指標:

string percentage = ((LiteralControl)e.Row.Cells[2].Controls[1]).Text; 
0

它應該是細胞[3],而不是細胞[2],因爲你的文字控制在第4列。但是不要在那裏硬編碼索引。當您想要稍後添加更多列時,這會很痛苦。相反,嘗試像這樣...

protected void RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     Literal id6 = (Literal)e.Row.FindControl("id6"); 
     string percentage = ""; 

     if (id6 != null) { 
      percentage = id6.Text; 
     } 
    } 
}