试图在移动的GridView项的复选框以列表框与"选取[按钮的方法 [英] Trying to move Checkbox Items in Gridview to Listbox with a "Select" button method

查看:136
本文介绍了试图在移动的GridView项的复选框以列表框与"选取[按钮的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作就是要我用code创建一个复选框的GridView的分配。这样做之后,我接手选择(通过点击一个按钮)复选框项目列表框中显示的数值为文本。

样本页面是在这里:的http:// ASPNET。 cob.ohio.edu/mis3200/asppub/MIS3200/Unit8/bobcat8POS.aspx

我的code到目前为止是这样的:

 保护无效btnSelect_Click(对象发件人,EventArgs的发送)
{    lblAlready.Text =的String.Empty; //空,说你已经有一个项目的标签    的foreach(GridViewRow行gvSnacktastic.Rows)//检查网格视图中的所有项目
    {
        复选框checkItOut = row.Cells [0] .Controls [0]为复选框; //获取复选框
        如果(checkItOut = NULL&放大器;!&安培; checkItOut.Checked)//如果存在复选框,并选中
        {
              布尔storeVariable = TRUE; //存储变量,它告诉我们,如果我们需要将它添加到列表            的foreach(列表项的listItem在lbSelected.Items)//循环列表框中的项目,看看我们是否已经拥有了它。我没有在很长一段时间使用列表框,这可能是稍有不妥
              {
                    //我不知道这是否是。文本 - 列表框项目的文本比较复选框项说明
                    如果(listItem.Text == checkItOut.Text)
                    {
                         lblAlready.Text =项+ listItem.Text +已经被添加。 //使我们已经添加标签
                         storeVariable = FALSE; //我们并不需要添加这个项目
                    }
              }
              如果(storeVariable)//如果我们需要添加这个项目
              {
                  lbSelected.Items.Add(checkItOut.Text); //创建一个复选框项目说明一个新的列表框项目
               }
        }
    }
}

它不会显示任何错误,但是当我运行页面选择按钮不会做什么我想它。会有人愿意看样品页面,让我知道我在code很想念?

 < ASP:GridView控件ID =gvSnacktastic=服务器的AutoGenerateColumns =FALSE
                                的DataKeyNames =SID的DataSourceID =sdsSnacktastic可见=假>
                                <柱体和GT;
                                    < ASP:的TemplateField>
                                        <&ItemTemplate中GT;
                                            < ASP:复选框ID =cbSnacktastic=服务器/>
                                        < / ItemTemplate中>
                                    < / ASP:的TemplateField>
                                    < ASP:CommandField中按钮类型=按钮ShowSelectButton =真可见=FALSE/>
                                    < ASP:BoundField的数据字段=SID的HeaderText =SIDInsertVisible =FALSE
                                        只读=真SORTEX pression =SID可见=FALSE/>
                                    < ASP:BoundField的数据字段=说明的HeaderText =说明
                                        SORTEX pression =说明/>
                                    < ASP:BoundField的数据字段=价格DataFormatString ={0:C2}的HeaderText =价
                                        SORTEX pression =价格/>
                                < /专栏>
                            < / ASP:GridView的>


解决方案

在这里你去。

问题是,在你的情况,如果你的复选框,而不是第一列文本的文本进行检查。

更新:

 保护无效buttonSubmit_OnClick(对象发件人,EventArgs的发送)
{
    lblAlready.Text =的String.Empty; //空,说你已经有一个项目的标签    的foreach(GridViewRow行gvSnacktastic.Rows)//检查网格视图中的所有项目
    {
        VAR checkItOut = row.FindControl(checkItOut)的复选框; //获取复选框        //如果存在复选框,并选中
        如果(checkItOut == NULL ||!checkItOut.Checked)
        {
            继续;
        }        VAR storeVariable = TRUE; //存储变量,它告诉我们,如果我们需要将它添加到列表        //循环遍历列表框中的项目,看看我们是否已经拥有了它。我没有在很长一段时间使用列表框,这可能是稍有不妥
        的foreach(列表项的listItem在this.lbSelected.Items)
        {
            //我不知道这是否是。文本 - 列表框项目的文本比较复选框项说明
            如果(listItem.Text!= row.Cells [3]。文本)
            {
                继续;
            }            //使我们已经添加标签
            this.lblAlready.Text =项+ listItem.Text +已经被添加。            //我们并不需要添加这个项目
            storeVariable = FALSE;
        }        //如果我们需要添加这个项目
        如果(storeVariable)
        {
            this.lbSelected.Items.Add(row.Cells [3]。文本); //创建一个复选框项目说明一个新的列表框项目
        }        checkItOut.Checked = FALSE;
    }
}

I am working on an assignment that is asking me to use code to create a gridview with a checkbox. After doing so, I am to take the checkbox items selected (through a button click) over to the listbox to display the values as text.

The sample page is here: http://aspnet.cob.ohio.edu/mis3200/asppub/MIS3200/Unit8/bobcat8POS.aspx

My code so far is this:

 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 
               }
        }
    }
}

It doesn't display any errors, but when I run the page the select button does not do what I want it to. Would anyone be willing to look at the sample page and let me know what I am missing in my code?

<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>

解决方案

Here you go.

Issue is that in your if condition you check with the text of the checkbox instead of the text of the 1st column.

Update:

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;
    }
}

这篇关于试图在移动的GridView项的复选框以列表框与&QUOT;选取[按钮的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆