我有一个使用Gridview
的 C#.net 应用程序。我在尝试确定如何完成此任务时遇到问题。
我需要Gridview(on page_load
)根据变量中的数字显示行数。例如,如果我的variable= 4
我只需要在我的gridview中使用模板字段文本框显示4行。我不希望显示任何数据,只是用户能够在每个文本框中输入他们的电子邮件。我有4行显示但不是每行templatefield
。到目前为止这是我的代码。
的.aspx
<asp:GridView ID="gvAddEmail" runat="server" Width="100%" CssClass="table table-striped table-bordered table-responsive" Caption="Enter Email" ShowFooter="true" AutoGenerateColumns="True" CellPadding="0">
<Columns>
<asp:TemplateField HeaderText="Email Address(s)" HeaderStyle-CssClass="visible-xs" ItemStyle-CssClass="visible-xs">
<ItemTemplate>
<%# Eval ("VisitorEmail") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtVEmail" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#ccffcc" />
<EmptyDataTemplate>
<tr>
<th scope="col">Email</th>
</tr>
<tr>
<td>
<asp:TextBox ID="txtVEmail" runat="server"></asp:TextBox>
</td>
<!-- <td>
<asp:Button ID="btnSubmitVI" runat="server" Text="Submit Visitor Information" OnClick="InsertVisitorInfo" CommandName="EmptyDataTemplate" />
</td> -->
</tr>
</EmptyDataTemplate>
</asp:GridView>
代码
protected void Page_Load(object sender, EventArgs e) {
InitGrid();
if (!IsPostBack) {
PanelSignIn.Visible = false;
}
}
public class Data {
public string VisitorEmail {
get;
set;
}
}
private void InitGrid() {
int NumberOfRows = GetLocation();
Response.Write("Number of Rows that should display: " + NumberOfRows); //debugging
List < Data > InitialData = new List < Data > ();
for (int i = 0; i < NumberOfRows; i++) {
InitialData.Add(new Data());
}
gvAddEmail.DataSource = InitialData;
gvAddEmail.DataBind();
}
答案 0 :(得分:0)
我认为你应该改变你的
<ItemTemplate>
<%# Eval ("VisitorEmail") %>
</ItemTemplate>
到
<ItemTemplate>
<asp:TextBox runat="server" id="textBoxEmail" text='<%# Bind("VisitorEmail") %>'>
</asp:TextBox>
</ItemTemplate>
的.aspx
<div>
<asp:DropDownList runat="server" ID="ddlNumberOfRow" AutoPostBack="True" onselectedindexchanged="ddlNumberOfRow_SelectedIndexChanged">
<asp:ListItem>5</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
</asp:DropDownList>
<asp:GridView runat="server" ID="gvAddEmail" AutoGenerateColumns="False">
<columns>
<asp:TemplateField HeaderText="Visitor Email">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("VisitorEmail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox runat="server" id="textBoxEmail" text='<%# Bind("VisitorEmail") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
</div>
的.cs
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGvAddEmail();
}
}
private void BindGvAddEmail()
{
List<myData> initialData = new List<myData>();
for (int i = 0; i < int.Parse(ddlNumberOfRow.SelectedValue.ToString()); i++)
{
initialData.Add(new myData());
}
gvAddEmail.DataSource = initialData;
gvAddEmail.DataBind();
}
protected void ddlNumberOfRow_SelectedIndexChanged(object sender, EventArgs e)
{
BindGvAddEmail();
}
}
public class myData
{
public string VisitorEmail
{
get;
set;
}
}