所以,我希望在我的gridview中得到我的复选框的值,但我得到的是复选框的最后一个值的值。例如:如果我选中了复选框,我得到的是checkbox.checked = false。当我取消选中复选框时,我得到的是checkbox.checked = true。
这是我的代码
protected void Page_Load(object sender, EventArgs e)
{
loadData();
}
private void loadData()
{
List<User> retVal = new GetUserSystem().User_GetUser();
List<String> retVal2 = new GetUserSystem().User_GetAuth(Session["ProjectName"].ToString());
if (retVal2 != null)
{
for (int i = 0; i < retVal2.Count; i++)
{
for (int j = 0; j < retVal.Count; j++)
{
if (retVal2[i].Equals(retVal[j].username.ToString()))
{
retVal[j].auth = "Y";
}
else if (retVal[j].auth == "" && !retVal2[i].Equals(retVal[j].username.ToString()))
{
retVal[j].auth = "N";
}
}
}
}
else
{
for (int j = 0; j < retVal.Count; j++)
{
retVal[j].auth = "N";
}
}
gvUser.DataSource = retVal;
gvUser.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
int i = 0;
foreach (GridViewRow gvrow in gvUser.Rows)
{
var lblUsername = gvrow.FindControl("lblUsername") as Label;
var checkbox = gvrow.Cells[1].FindControl("cbxAuth") as CheckBox;
if (checkbox.Checked)
{
new AddSecuritySystem().User_AddSecurity(lblUsername.Text.ToString(),Session["ProjectName"].ToString(),"Y");
}
else
{
new AddSecuritySystem().User_AddSecurity(lblUsername.Text.ToString(),Session["ProjectName"].ToString(),"N");
}
i++;
}
Response.Redirect("Main.aspx");
}
这是aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/Main.Master" AutoEventWireup="true" CodeBehind="Security.aspx.cs" Inherits="ProjectManagement.Security" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<asp:Panel ID="pnlUser" runat="server" Width="350px" CssClass="CellPadding" BorderColor="#003399" BorderStyle="Solid" BorderWidth="1px">
<asp:Label runat="server" Style="color: blue; font-size: large;">Security</asp:Label>
<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="false" Width="100%" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" GridLines="Horizontal" ForeColor="Black">
<Columns>
<asp:TemplateField HeaderText="User">
<EditItemTemplate>
<asp:TextBox ID="txtUsername" runat="server" Text='<%# Bind("Username") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUsername" runat="server" Text='<%# Bind("Username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<EditItemTemplate>
<asp:CheckBox ID="cbxAuth" runat="server" Checked='<%# Convert.ToString(Eval("Auth")).Equals("Y") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbxAuth" runat="server" Checked='<%# Convert.ToString(Eval("Auth")).Equals("Y") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="White" Font-Bold="false" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
</asp:Panel>
</asp:Content>
任何人都可以帮助我吗?
答案 0 :(得分:0)
由于您在谈论网络表单,因此不应在回发时调用loaddata()
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
loadData();
}
}
不检查IsPostback
上的page_load
,您的旧数据会再次加载到button click
,加载到gridview
并更改复选框的状态。