我在List中有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;
}
有什么想法吗?提前谢谢。
答案 0 :(得分:1)
试试看看会发生什么
if (Application["mondayValues"] != null)
{
List<string> monValues = Application["mondayValues"] as List<string>;
for (int i = 0; i <= gridActivity.Rows.Count-1; i++)
{
GridViewRow row = gridActivity.Rows[i];
TextBox txtMon = (TextBox)row.FindControl("txtMon");
txtMon.Text = monValues[i];
}
}
答案 1 :(得分:0)
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 [“mondayValues”]中的最后一个值被分配给所有文本框txtMon,因为您正在迭代所有行并为它们分配值,在先前迭代中所做的任何更改都将丢失
这段代码对我来说没有多大意义,你能否详细说明你想要做的事情,以便我们可以看一下。