我有一个gridview显示出来一个数据库的内容(包括姓名列,年龄列,省份列),每行内容都加了一个checkbox。每当我选中某一行的checkbox,则
在gridview外面的一个小区域(比如一个叫SelectedItem的div)显示我选择的一个的姓名。我又选择一个checkbox,则SelectedItem里面又添加一个姓名,排列方式为一行一行的;反之,如果我把某个选择的checkbox不选了,则这个SelectedItem里面的姓名列表就自动删除我不选择的那个姓名。
这个怎么实现?SelectedItem里面需要使用什么控件啊?
(提醒,我觉得我还是不使用js了,使用checkbox的select_change方法,然后对SelectedItem进行动态修改。所以,我不知道SelectedItem里面使用什么控件,该怎么运作)
我说的是否明白?多谢。

解决方案 »

  1.   

    哦,那好吧,我就使用js在客户端。gridview放在一个updatepanel里面,这样selectedItem的div不受刷新影响。但是selectedItem的div里面放个什么好呢?难道就放一个textbox,每次gridview里面点击checkbox,就加一个名字,然后加一个<br>?有没有好的方法???
      

  2.   

    使用的是northwind数据库,它用的是 linkbuuton添加,你改成checkbox就可以了,但没选中的时候还要在判断次将他删除<%@ Page language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if(e.CommandName=="Add")
        {
          // Convert the row index stored in the CommandArgument
          // property to an Integer.
          int index = Convert.ToInt32(e.CommandArgument);      // Retrieve the row that contains the button clicked 
          // by the user from the Rows collection.
          GridViewRow row = CustomersGridView.Rows[index];      // Create a new ListItem object for the customer in the row.     
          ListItem item = new ListItem();
          item.Text = Server.HtmlDecode(row.Cells[2].Text);      // If the customer is not already in the ListBox, add the ListItem 
          // object to the Items collection of the ListBox control. 
          if (!CustomersListBox.Items.Contains(item))
          {
            CustomersListBox.Items.Add(item);
          }           
        }
      }  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
      {    // The GridViewCommandEventArgs class does not contain a 
        // property that indicates which row's command button was
        // clicked. To identify which row's button was clicked, use 
        // the button's CommandArgument property by setting it to the 
        // row's index.
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Retrieve the LinkButton control from the first column.
          LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];      // Set the LinkButton's CommandArgument property with the
          // row's index.
          addButton.CommandArgument = e.Row.RowIndex.ToString();
        }  }</script><html xmlns="http://www.w3.org/1999/xhtml" >
      <head id="Head1" runat="server">
        <title>GridView RowCommand Example</title>
    </head>
    <body>
        <form id="form1" runat="server">      <h3>GridView RowCommand Example</h3>      <table width="100%">         
            <tr>                
              <td style="width:50%">            <asp:gridview id="CustomersGridView" 
                  datasourceid="CustomersSource"
                  allowpaging="true" 
                  autogeneratecolumns="false"
                  onrowcommand="CustomersGridView_RowCommand"
                  onrowcreated="CustomersGridView_RowCreated"  
                  runat="server">              <columns>
                    <asp:buttonfield buttontype="Link" 
                      commandname="Add" 
                      text="Add"/>
                    <asp:boundfield datafield="CustomerID" 
                      headertext="Customer ID"/>
                    <asp:boundfield datafield="CompanyName" 
                      headertext="Company Name"/> 
                    <asp:boundfield datafield="City" 
                      headertext="City"/>         
                  </columns>            </asp:gridview>          </td>          <td style="vertical-align:top; width:50%">            Customers: <br/>
                <asp:listbox id="CustomersListBox"
                  runat="server"/>           </td>  
            </tr>      
          </table>      <!-- This example uses Microsoft SQL Server and connects  -->
          <!-- to the Northwind sample database. Use an ASP.NET     -->
          <!-- expression to retrieve the connection string value   -->
          <!-- from the Web.config file.                            -->
          <asp:sqldatasource id="CustomersSource"
            selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
            connectionstring="server=(local);database=northwind;integrated security=sspi"
            runat="server"/>    </form>
      </body>
    </html>
      

  3.   

    有点借鉴,但是不全。如果是checkbox,点击的时候,需要判断是选择了,还是不选择了。这样的话,需要将checkbox对象进行传递吧。而不是简单的一个commandName那么样。