【问题标题】:Send column number value to asp MaxLength将列号值发送到 asp MaxLength
【发布时间】: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='&lt;%# Convert.ToInt32(Bind("MaxNoPlaces")) %&gt;' 怎么样? InvalidCastException 可能会出现,因为 MaxLength 属性需要 int 值类型,而 Bind 则返回 stringobject
  • 是的,你在哪里对。我在 c# 部分将字符串转换为 int,然后将其发送 tu asp,它完美地工作了 TNX!

标签: c# asp.net webforms


【解决方案1】:

TextBox.MaxLength 属性需要 int 的值类型,如以下声明:

public virtual int MaxLength { get; set; }

因为DataTable中声明string数据类型的MaxNoPlaces列绑定了GridView.DataBind()方法,所以直接绑定到对应属性时会遇到InvalidCastException。您应该在绑定之前使用typeof(int) 并进行整数转换,例如Convert.ToInt32()int.Parse()

dt.Columns.AddRange(new DataColumn[2]{ new DataColumn("CharacteristicName", typeof(string)), 
                    new DataColumn("MaxNoPlaces", typeof(int))});

foreach (DataRow dr in dtKar.Rows)
{
    // perform integer conversion
    dt.Rows.Add(dr["CharacteristicName"].ToString(), Convert.ToInt32(dr["MaxNoPlaces"]));
}

如果您在绑定到文本框控件之前完成了转换,那么像&lt;asp:TextBox runat="server" MaxLength='&lt;%# Convert.ToInt32(Bind("MaxNoPlaces")) %&gt;' ... /&gt; 这样的绑定转换就变得不必要了。

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2014-03-12
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2019-07-03
    • 2017-05-20
    • 1970-01-01
    相关资源
    最近更新 更多