0

我想从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;
    }
}
4

2 回答 2

2

你得到的文字控制可能不是你想要的(可能有一个与空格匹配的文字)。尝试通过 id 获取:

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

或者尝试另一个索引:

string percentage = ((LiteralControl)e.Row.Cells[2].Controls[1]).Text; 
于 2012-06-08T13:56:55.623 回答
0

它应该是 Cells[3],而不是 Cells[2],因为您的 Literal 控件位于第 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;
        }
    }
}
于 2012-06-08T14:23:17.443 回答