我正在进行一项任务,要求我使用代码创建带有复选框的gridview。执行此操作后,我将选中(通过按钮单击)复选框项目到列表框以将值显示为文本。
示例页面位于:http://aspnet.cob.ohio.edu/mis3200/asppub/MIS3200/Unit8/bobcat8POS.aspx
到目前为止,我的代码是:
protected void btnSelect_Click(object sender, EventArgs e)
{
lblAlready.Text = string.Empty; // empty the label that says you already have an item
foreach (GridViewRow row in gvSnacktastic.Rows) // check all the items in the grid view
{
CheckBox checkItOut = row.Cells[0].Controls[0] as CheckBox; // get the checkbox
if (checkItOut != null && checkItOut.Checked) // if the checkbox exists and is checked
{
bool storeVariable = true; // store a variable that tells us if we need to add it to the list
foreach(ListItem listItem in lbSelected.Items) // Loop through list box items to see if we already have it. i haven't used listbox in a long time, this might be slightly wrong
{
// I'm not sure if it's .Text - compare the text of the listbox item to the checkbox item description
if(listItem.Text== checkItOut.Text)
{
lblAlready.Text = "The Item " + listItem.Text + " has already been added."; // make our already added label
storeVariable = false; // we don't need to add this item
}
}
if(storeVariable) // if we do need to add this item
{
lbSelected.Items.Add(checkItOut.Text); // create a new list box item with the check box item's description
}
}
}
}
它没有显示任何错误,但是当我运行页面时,选择按钮不能达到我想要的效果。有人愿意看看示例页面,让我知道我的代码中缺少什么吗?
<asp:GridView ID="gvSnacktastic" runat="server" AutoGenerateColumns="False"
DataKeyNames="SID" DataSourceID="sdsSnacktastic" Visible="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cbSnacktastic" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" Visible="False" />
<asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False"
ReadOnly="True" SortExpression="SID" Visible="False" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="Price" DataFormatString="{0:c2}" HeaderText="Price"
SortExpression="Price" />
</Columns>
</asp:GridView>
答案 0 :(得分:1)
你走了。
问题是,在if条件下,您使用复选框的文本而不是第1列的文本进行检查。
<强>更新强>
protected void buttonSubmit_OnClick(object sender, EventArgs e)
{
lblAlready.Text = string.Empty; // empty the label that says you already have an item
foreach (GridViewRow row in gvSnacktastic.Rows) // check all the items in the grid view
{
var checkItOut = row.FindControl("checkItOut") as CheckBox; // get the checkbox
// if the checkbox exists and is checked
if (checkItOut == null || !checkItOut.Checked)
{
continue;
}
var storeVariable = true; // store a variable that tells us if we need to add it to the list
// Loop through list box items to see if we already have it. i haven't used listbox in a long time, this might be slightly wrong
foreach (ListItem listItem in this.lbSelected.Items)
{
// I'm not sure if it's .Text - compare the text of the listbox item to the checkbox item description
if (listItem.Text != row.Cells[3].Text)
{
continue;
}
// make our already added label
this.lblAlready.Text = "The Item " + listItem.Text + " has already been added.";
// we don't need to add this item
storeVariable = false;
}
// if we do need to add this item
if (storeVariable)
{
this.lbSelected.Items.Add(row.Cells[3].Text); // create a new list box item with the check box item's description
}
checkItOut.Checked = false;
}
}