我正在使用foreach循环从网格视图中获取复选框的值,并通过在复选框regID
事件上设置当前用户CheckedChanged
来更新记录。
在ASPX中
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="cbPaid" runat="server" AutoPostBack="true" OnCheckedChanged="cbPaid_CheckedChanged" Checked='<%# bool.Parse(Eval("status").ToString() == "Paid" ? "True": "False") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
并在aspx.cs
protected void cbPaid_CheckedChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GdParticipants.Rows)
{
CheckBox cbPaid = (CheckBox)row.FindControl("cbPaid");
if (cbPaid!=null && cbPaid.Checked == true)
{
string status = "Paid";
int amount = Convert.ToInt32(ViewState["EventFee"]);
DateTime date = DateTime.Now;
string user = Session["user_id"].ToString();
string regID = row.Cells[2].Text;
string type = "Deposit";
string account = "Participation";
int balance = BAL.fetch_availableBalance();
int total_balance = amount + balance;
string detail = "Participant: "+regID+" fee has been Recieved";
BAL.updateParticipants(regID, status, amount, user, date);
BAL.saveBudgetTracking(type,account,amount,0,total_balance,detail,date);
}
}
Response.Redirect("~/show_members.aspx");
}
这种方法的问题是每当新用户通过选中一个复选框来更新新记录时,它将遍历整个记录并更新以前记录中的当前更改。
任何人都可以指导我如何才能从正在检查复选框的行中获取当前的regID以及checkbox-CheckedChanged
事件上的复选框值,而不是循环?
答案 0 :(得分:1)
发件人是CheckBox对象,它的2级部分是GridViewRow:
int MyLastPaperSize=5;
dlg.PageSettings.PaperSize.RawKind= MyLastPaperSize;
答案 1 :(得分:0)
使用以下代码,它将避免通过网格循环。
protected void cbPaid_CheckedChanged(object sender, EventArgs e)
{
int selRowIndex = ((GridViewRow)(((CheckBox)sender).Parent.Parent)).RowIndex;
CheckBox cb = (CheckBox)GdParticipants.Rows[selRowIndex].FindControl("cbPaid");
if (cb.Checked)
{
//respective cell's value
string regID = GdParticipants.Rows[selRowIndex].Cells[2].Text;
//Perform your rest of the logic
}
}
答案 2 :(得分:0)
您可以通过父级搜索同级控件。
我个人喜欢使用标签控件,以便我可以调用 FindControl ,因为如果重新排序列,则row.Cells [2] .Text会 在运行时崩溃 应用程序。
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server"
ID="RegIDLabel"
Text='<%# Eval("RegID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbPaid" runat="server"
AutoPostBack="true"
OnCheckedChanged="cbPaid_CheckedChanged"
Checked='<%# bool.Parse(Eval("status").ToString() == "Paid" ? "True": "False") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = new List<Item>
{
new Item {RegID = "1", Status = "Paid"},
new Item {RegID = "2", Status = "Other"},
new Item {RegID = "3", Status = "Paid"},
};
GridView1.DataBind();
}
}
protected void cbPaid_CheckedChanged(object sender, EventArgs e)
{
var cbPaid = sender as CheckBox;
var cell = cbPaid.Parent as DataControlFieldCell;
var row = cell.Parent as GridViewRow;
var regIDLabel = row.FindControl("RegIDLabel") as Label;
var regId = regIDLabel.Text;
if (cbPaid.Checked)
{
// Do something
}
}
}