【发布时间】:2018-07-10 17:02:25
【问题描述】:
我在数据库中有一个存储整数值的列(4、6、8 和 NULL)。 这些整数值应该是 TextBox 的最大允许长度,这是table。
这是来自 c# 的代码,我暂时在其中传递了特征名称和整数(动态数据库中文本框的最大位置数)。
protected void ddlBC_SelectedIndexChanged(object sender, EventArgs e)
{
//ddlKar.Items.Clear();
LogicTableAdapters.getLvLOneIntegerTableAdapter getKar = new LogicTableAdapters.getLvLOneIntegerTableAdapter();
DataTable dtKar = getKar.getLvLOneInteger(ddlBC.SelectedValue);
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2]{ new DataColumn("CharacteristicName", typeof(string)) new DataColumn("MaxNoPlaces", typeof(string))});
foreach (DataRow dr in dtKar.Rows)
{
dt.Rows.Add(dr["CharacteristicName"].ToString(), dr["MaxNoPlaces"].ToString());
}
gvKarakteristike.DataSource = dt;
gvKarakteristike.DataBind();
}
这是 ASPX 标记:
<asp:GridView ID="gvKarakteristike" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="LevelID" OnRowDataBound="gvKarakteristike_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Characteristics">
<ItemTemplate>
<asp:Label ID="Characteristics" runat="server" Width="150px" Height="30px" Font-Names="Georgia" margin-Left="100px" Text='<%# Bind("CharacteristicName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:DropDownList ID="ddlOpis" AppendDataBoundItems="true" Width="142px" Height="35px" Font-Names="Georgia" margin-Left="100px" runat="server">
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
<asp:TextBox ID="txtBoxOpis" runat="server" Font-Names="Georgia" margin-Left="100px" Text="" MaxLength='<%# Bind("MaxNoPlaces") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
我需要直接从数据库中动态地进行此操作,因为数据库会随着时间的推移而增长,文本框的数量也会随之增长。
问题出在这部分代码上:
<asp:TextBox ID="txtBoxOpis" runat="server" Font-Names="Georgia" margin-Left="100px" Text="" MaxLength='<%# Bind("MaxNoPlaces") %>'></asp:TextBox>
我在那里运行应用程序时出错:
App_Web_kn224wf0.dll 中出现“System.InvalidCastException”类型的异常,但未在用户代码中处理
我认为问题是当 NULL 值从数据库发送到 asp 时,它可以将其转换为 MaxLength。
有人可以帮我解决这个问题吗?
提前致谢!
【问题讨论】:
-
MaxLength='<%# Convert.ToInt32(Bind("MaxNoPlaces")) %>'怎么样?InvalidCastException可能会出现,因为MaxLength属性需要int值类型,而Bind则返回string或object。 -
是的,你在哪里对。我在 c# 部分将字符串转换为 int,然后将其发送 tu asp,它完美地工作了 TNX!