我在列表中有 4 个值我想在第一列的 gridview 行中绑定这些值
我尝试使用下面的代码,但它被每行中列表的最后一个值覆盖
代码.aspx
<asp:GridView ID="gridActivity" runat="server" AllowSorting="true" AutoGenerateColumns="false"
CellPadding="5" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84" Height="171px" style="margin-left: 0px; margin-right: 0px" Width="780px">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle Width="10px" Wrap="false" ForeColor="White" Font-Bold="True"
BackColor="SteelBlue"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="Activities">
<ItemTemplate>
<asp:Label ID="lblActivity" runat="server" Text='<%#Eval("ActivityName") %>' CssClass="columnStyle"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Monday">
<ItemTemplate>
<asp:TextBox ID="txtMon" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tuesday">
<ItemTemplate>
<asp:TextBox ID="txtTue" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Wednesday">
<ItemTemplate>
<asp:TextBox ID="txtWed" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Thursday">
<ItemTemplate>
<asp:TextBox ID="txtThu" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Friday">
<ItemTemplate>
<asp:TextBox ID="txtFri" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Saturday">
<ItemTemplate>
<asp:TextBox ID="txtSat" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday">
<ItemTemplate>
<asp:TextBox ID="txtSun" runat="server" Width ="100%"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Width ="100%"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DbConnection.Open();
gridActivity.DataSource = Application["listActivity"];
gridActivity.DataBind();
DbConnection.Close();
}
if (Application["mondayValues"] != null)
{
//Here I can get all the values
foreach (string mon in Application["mondayValues"] as List<string>)
{
//Here is the problem
foreach (GridViewRow row in gridActivity.Rows)
{
TextBox txtMon = (TextBox)row.FindControl("txtMon");
txtMon.Text = mon;
}
}
}
}
在下面的代码中,我可以获得所有星期一的行值并存储到 Application["monday"]
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in gridActivity.Rows)
{
TextBox txtMon = (TextBox)row.FindControl("txtMon");
Application["mondayValues"] = txtMon.Text;
mondayData = new MondayData { MondayValue = txtMon.Text };
listMonday.Add(txtMon.Text);
}
Application["mondayValues"] = listMonday;
}
有任何想法吗?提前致谢。