0

我在列表中有 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;
}

网格

有任何想法吗?提前致谢。

4

2 回答 2

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];
    }           
}
于 2013-09-12T06:24:13.950 回答
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,因为您正在迭代所有行并为它们分配值,在早期迭代中所做的任何更改都将丢失。

这段代码对我来说没有多大意义,你能否包含更多关于你想要做的事情的细节,以便我们可以调查一下。

于 2013-09-12T06:08:46.907 回答