2

在单击按钮两次时获取选定的值。但是单击一次时不起作用

 <asp:GridView ID="viewgrid"  runat="server" AutoGenerateColumns="False" 
      OnRowCommand="viewgrid_RowCommand" OnRowDataBound="viewgrid_RowDataBound" 
      DataKeyNames="RegionID"  >
     <AlternatingRowStyle BackColor="#DCDCDC" />
        <Columns>
           <asp:TemplateField HeaderText="RegionName">
              <HeaderStyle BackColor="#4B6C9E" ForeColor="White" />
                  <ItemTemplate>
                     <table style="width: 200px">
                        <tr>
                           <td>
                              <asp:Label ID="lblregion" runat="server" 
                                 Text='<%# Eval("name") %>' ToolTip='<%# Eval("RegionID") %>'>
                               </asp:Label><br />
                             </td>
                         </tr>
                         <tr>
                             <td>
                                <asp:ListBox ID="listlocation" runat="server" Width="200px" 
                                    Height="38px" SelectionMode="Multiple" 
                                    AppendDataBoundItems="true"></asp:ListBox>
                              </td>
                           </tr>
                       </table>
                    </ItemTemplate>
                    <FooterTemplate>
                    </FooterTemplate>
                    <FooterStyle BackColor="#4B6C9E" ForeColor="White" Height="15px" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Action">
                       <HeaderStyle BackColor="#4B6C9E" ForeColor="White" />
                        <ItemTemplate>
                            <table style="margin-top: 5px">
                                <tr>
                                    <td>
                                        <asp:Button ID="btnright" runat="server" CommandName="moveright" Text=">" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Button ID="btnallright" runat="server" CommandName="moveallright" Text=">>" />
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <FooterTemplate>
                        </FooterTemplate>
                        <FooterStyle BackColor="#4B6C9E" ForeColor="White" />
                    </asp:TemplateField>
                </Columns>

            </asp:GridView>



  protected void viewgrid_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    GridView grd = (GridView)sender;
    if (e.CommandName == "moveright")
    {

        //string newRegionID = string.Empty;
        GridViewRow gridrow = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        int index = gridrow.RowIndex;


        //ListBox region = (ListBox)(gridrow.FindControl("listlocation"));

        ListBox region = (ListBox)(viewgrid.Rows[index].FindControl("listlocation"));
        List<ListItem> selectedregions = new List<ListItem>();
        List<object> toRemove = new List<object>();


        foreach (ListItem li in region.Items)
        {
            if (li.Selected)
            {
                selectedregions.Add(li);
                listlocation2.Items.Add(li);
            }
        }
        foreach (ListItem lll in selectedregions)
        {
            region.Items.Remove(lll);
        }


    }

    if (e.CommandName == "moveallright")
    {
        lblmessage.Visible = false;

        GridViewRow gridrow = (GridViewRow)((Button)e.CommandSource).NamingContainer;
        int index = gridrow.RowIndex;
        ListBox locationlist1 = (ListBox)(viewgrid.Rows[index].FindControl("listlocation"));


        while (locationlist1.Items.Count != 0)
        {
            for (int i = 0; i < locationlist1.Items.Count; i++)
            {

                listlocation2.Items.Add(locationlist1.Items[i]);

                locationlist1.Items.Remove(locationlist1.Items[i]);
            }
        }
    }

} 
4

0 回答 0