asp.net gridview的复选框选择 [英] asp.net gridview checkbox selection
问题描述
需要一些帮助来解决这个问题。
我有一个GridView和GridView控件里我有一个复选框,并单击复选框后,我做了回发事件,并试图只是对数据库更新此特定的行。
这是我的GridView的复选框,code。看到OnCheckedChanged。
< ASP:模板列HEADERTEXT =样本>
<的ItemTemplate>
< ASP:复选框=服务器
ID =chkSample
选中='<%#绑定(样本)%>
OnCheckedChanged =UpdateSupplyLed
的AutoPostBack =真>
< / ASP:复选框>
< / ItemTemplate中>
< / ASP:的TemplateField>
code:
保护无效UpdateSupplyLed(对象发件人,EventArgs的)
{
的foreach(在SamplingGridView.Rows GridViewRow迪)
{
复选框chkBx =(复选框)di.FindControl(chkSample);
如果(chkBx = NULL和放大器;!&安培; chkBx.Checked)
{
//这里更新数据库逻辑。
}
}
}
以上code正常工作,但它让我所有的检查,我只是检查了一个irresepective复选框。我不希望所有的人。
我怎样才能被刚刚选中的只有一行值。某些行可能已经被确认已经因为状态是如此的记录,我不希望更新这些记录。
我想我已经得到了我的问题吧!
更新:答案是:
保护无效UpdateSupplyLed(对象发件人,EventArgs的)
{
复选框chkSampleStatus =发件人为复选框;
布尔样本= chkSampleStatus.Checked;
GridViewRow行= chkSampleStatus.NamingContainer为GridViewRow;
文本框txtId = row.FindControl(ID)的文本框;
INT ID = Int32.Parse(txtId.Text);
}
试试这个:
复选框chkBx =发件人为复选框;
而不是迭代的所有行。
我还没有使用复选框在以这种方式自己一个GridView。通常我会用GridView的OnRowCommand事件,而不是和使用则rowIndex或CommandArgument值更新数据库。
关于它的思考OnRowcommand可能会非常棘手的复选框火,一个更好的解决方案可能与复选框的CheckChanged活动贴合,使用控制NamingContainer导航到GridViewRow服务器端。是这样的:
GridViewRow行= chkBx.NamingContainer为GridViewRow;
我假设的那张复选框=>小区=>行,如果你谷歌ASP.NET NamingContainer你会得到一些更多的细节。
Need some help to solve this.
I have a gridview and inside the gridview I have a checkbox and after clicking the checkbox, I am doing a postback event and trying to update this particular row only on the database.
This is my gridview checkbox code. see the OnCheckedChanged.
<asp:TemplateField HeaderText="Sample">
<ItemTemplate>
<asp:CheckBox runat="server"
ID="chkSample"
Checked='<%# Bind("Sample") %>'
OnCheckedChanged="UpdateSupplyLed"
AutoPostBack="True">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
Code:
protected void UpdateSupplyLed(object sender, EventArgs e)
{
foreach (GridViewRow di in SamplingGridView.Rows)
{
CheckBox chkBx = (CheckBox)di.FindControl("chkSample");
if (chkBx != null && chkBx.Checked)
{
//update database logic here.
}
}
}
The above code works fine but it is getting me all the checkboxes that are checked irresepective of the one that I just checked. I don't want all of them.
How can I get the only one row value that have been just checked. Some of the rows might have been checked already because the status is true for those records and I don't want to update those records.
I think I've got my question right!
Update: The answer is:
protected void UpdateSupplyLed(object sender, EventArgs e)
{
CheckBox chkSampleStatus = sender as CheckBox;
bool sample = chkSampleStatus.Checked;
GridViewRow row = chkSampleStatus.NamingContainer as GridViewRow;
TextBox txtId = row.FindControl("Id") as TextBox;
int id = Int32.Parse(txtId.Text);
}
Try this:
CheckBox chkBx = sender as CheckBox;
Rather than iterate all the rows.
I haven't used CheckBox's in a GridView in this way myself. Usually I would use the GridView's OnRowCommand event instead and use the RowIndex or CommandArgument value to update the database.
Thinking about it OnRowcommand could be tricky for a CheckBox to fire, a better solution might be sticking with the CheckChanged event of the checkbox and navigate up to the GridViewRow serverside using controls NamingContainer. Something like:
GridViewRow row = chkBx.NamingContainer as GridViewRow;
I'm assuming the goes CheckBox => Cell => Row if you Google ASP.NET NamingContainer you'll get some more specifics.
这篇关于asp.net gridview的复选框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!