我想从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;
}
}
答案 0 :(得分:0)
您是否尝试过更改
string percentage = ((LiteralControl)e.Row.Cells[4].Controls[0]).Text;
到
string percentage = ((DataBoundLiteralControl)e.Row.Cells[4].Controls[0]).Text;