指定的转换无效
一个页面里有一个Datagrid
前台代码:
<asp:datagrid id="dgVidicon" runat="server" DataKeyField="Id"
oneditcommand="edit" OnCancelCommand="cancel" OnUpdateCommand="update" AllowPaging="True"
PageSize="10" AutoGenerateColumns="False"
PagerStyle-Visible="False">
<SelectedItemStyle BackColor="White"></SelectedItemStyle>
<AlternatingItemStyle BackColor="AliceBlue"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" VerticalAlign="Middle"
BackColor="SkyBlue"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Id" Visible="False">
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="TypeName" HeaderText="类型名称">
<HeaderStyle Width="24%"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="类型选项">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"TypeValue").ToString()%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txt_DropItem" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="排序">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"Sort").ToString()%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txt_Sort" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete">
<HeaderStyle Width="15%"></HeaderStyle>
</asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
<HeaderStyle Width="15%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
</Columns>
<PagerStyle ForeColor="White" BackColor="DodgerBlue" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
public void edit(object sender, DataGridCommandEventArgs e)
{
int tempPageIndex = dgVidicon.CurrentPageIndex;
dgVidicon.EditItemIndex=e.Item.ItemIndex;
DataFiller(tempPageIndex);
TextBox abox=(TextBox)dgVidicon.Items[e.Item.ItemIndex].Cells[2].Controls[0];
TextBox bbox=(TextBox)dgVidicon.Items[e.Item.ItemIndex].Cells[3].Controls[0];
abox.Width = Unit.Percentage(100);
bbox.Width = Unit.Percentage(100);
}
设置列宽
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == (DataControlRowState.Edit
[解决办法]
DataControlRowState.Alternate)
[解决办法]
e.Row.RowState == DataControlRowState.Edit)
{
TextBox curText = e.Row.Cells[2].Controls[0] as TextBox;;
curText.Width = Unit.Pixel(70);
}
}
[解决办法]
写成
<EditItemTemplate><asp:TextBox runat="server" ID="txt_DropItem" /></EditItemTemplate>
Controls[0]指的是空白文本,去掉空白应该就好了
或者指定ID,FindControl