我在数据库中有一个表:
ID Name MaxPlaces
1 Product 20
2 Group 30
3 AFG 40
我需要将MaxPlaces列绑定到asp中的文本框以MaxLenght属性(MaxLength ='<%#Bind(“ MaxPlaces”)%>'):
<tr>
<td>
<asp:Label ID="lblProduct" runat="server" Text="Product:" Font-Names="Open Sans"></asp:Label></td>
<td>
<asp:TextBox ID="txtProduct" runat="server" Font-Names="Merriweather" margin-Left="100px" AutoPostBack="true" MaxLength='<%# Bind("MaxPlaces") %>'></asp:TextBox><br />
</td>
</tr>
我已经创建了一个存储过程:
@ID nvarchar(100)
AS
BEGIN
SELECT ID, Name, MaxPlaces
FROM dbo.Level
WHERE ID = @ID;
END
这意味着MaxLenght属性的值必须为'20'(数据库的第一行)
我需要C#代码将参数值设置为'1'并将'20'发送给ASP绑定。
有人可以帮助我提供此代码吗?
谢谢!
答案 0 :(得分:0)
这是代码的解释
public partial class DropDownExample : System.Web.UI.Page
{
protected int GetMaxLength(string passedValue)
{
//could check passedValue to determine length from database not shown
//kudos https://forums.asp.net/t/1274891.aspx?Textbox+maxlength+using+variable
return 10;
}
protected void Page_Load(object sender, EventArgs e)
{
//you need this if you want to do it this way
//you could just set the maxlength on control in codebehind
txtProduct.DataBind();
}
}
aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="DropDownExample.aspx.cs" Inherits="FredWebForm.DropDownExample" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<asp:TextBox ID="txtProduct" runat="server" Font-Names="Merriweather" margin-Left="100px"
AutoPostBack="true" MaxLength='<%# GetMaxLength("passedValue")%>'></asp:TextBox>
</asp:Content>