【问题标题】:Convert Integer value from database to textbox max length将整数值从数据库转换为文本框最大长度
【发布时间】:2018-07-06 10:36:02
【问题描述】:

我在数据库中有一个存储整数值的列(4、6、8...)。 这些整数值应该是 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();
}

这是asp部分:

 <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" Width="130px" Height="28px" Font-Names="Georgia" margin-Left="100px" Text='<%# 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>

我需要直接从数据库中动态地进行此操作,因为数据库会随着时间的推移而增长,文本框的数量也会随之增长。

我可以用 jquery 或 asp 来做这件事吗? 提前致谢!

【问题讨论】:

    标签: c# jquery asp.net webforms


    【解决方案1】:

    你可以试试下面的代码,看看它是否有效。我将 Text 属性更改为 MaxLength。

     <asp:TextBox ID="txtBoxOpis" runat="server" Width="130px" Height="28px" Font-Names="Georgia" margin-Left="100px" MaxLength='<%= Bind("MaxNoPlaces") %>'></asp:TextBox>
    

    【讨论】:

      【解决方案2】:

      保留多行可能会使您更难设置文本框的最大长度。您实际上可以通过 javascript 动态设置 MaxLength。看看这个帖子:Control maxlength of multiline textbox

      【讨论】:

      • 每当您引用另一个链接时,请务必提及其中的症结所在,以便读者可以直接使用您的建议,而不是离开。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      相关资源
      最近更新 更多