我的一个DataGrid绑定了关于书的一些信息 中有一个模版列 里面有一个表格 ,里面显示了书的名字 我是
<TR>
<TD>&nbsp; 书名:<%# DataBinder.Eval(Container, "DataItem.bookName") %>
</TD>
</TR>
这样显示书名的。里面还有一个button 我把它的CommandName设成了com1,用于我在点击的时候可以写button的click事件  但是我在写事件的时候怎么才能知道是哪一行的button被点了??还有我怎么才能在点击事件里取到书的名字??
  解决马上结贴!!!!我用的03

解决方案 »

  1.   

    void CommandBtn_Click(Object sender, CommandEventArgs e) 
          {         switch(e.CommandName)
             {            case "com1":               // Call the method to sort the list.
                   Sort_List((String)e.CommandArgument);
                   break;            case "com2":               // Display a message for the Submit button being clicked.
                   Message.Text = "You clicked the Submit button";               // Test whether the command argument is an empty string ("").
                   if((String)e.CommandArgument == "")
                   {
                      // End the message.
                      Message.Text += ".";
                   }
                   else
                   {
                      // Display an error message for the command argument. 
                      Message.Text += ", however the command argument is not recogized.";
                   }                
                   break;            default:               // The command name is not recognized. Display an error message.
                   Message.Text = "Command name not recogized.";
                   break;          }
      

  2.   

    孟子E章  大哥写的这么清楚 我给做下 解释工作
    模版的btclick 时间是通过datagrid等 控件的 CommandBtn_Click事件 统一执行的
    怎么区分是那个bt的事件呢
      switch(e.CommandName)
    CommandName 这个名字就是 你的模版的bt按钮的标记 后台可以根据CommandName 名字 区分是那个 bt按钮 的事件
    你在模版的bt按钮 右击 就能出来选择 菜单内 找到 他的设置
      

  3.   

    .......我的DataGrid没有CommandBtn_Click事件呀??
      

  4.   

    不知道怎么的 在模版列里的控件都不触发ItemCommand事件。只有按钮列那些才会触发
      

  5.   

    但是模版列里加入的控件并不能触发DataGrid的ItemCommand事件啊??应该怎么解决?
      

  6.   

    ItemCommand事件 是Datagrid 事件(如button 等)的集合 你要想分辨出是那个
    bt的事件 就需要
    switch(e.Item.事件名称)
    //如果 某个事件名称 为com1
       case "com1":
       .... 执行你的代码
    }
      

  7.   

    当单击 DataGrid 控件中某项的任一按钮时会引发 ItemCommand 事件。它使您能够以编程方式测定单击特定命令按钮的时间并采取相应的操作。该事件常用来处理 DataGrid 控件的自定义命令按钮。你可以用CommandName来区别是点击了哪一列,<asp:DataGrid id="ItemsGrid"
               BorderColor="black"
               BorderWidth="1"
               CellPadding="3"           
               OnItemCommand="ItemsGrid_Command"
               AutoGenerateColumns="false"
               runat="server">         <HeaderStyle BackColor="#aaaadd">
             </HeaderStyle>
     
             <Columns>            <asp:EditCommandColumn
                     EditText="Edit"
                     CancelText="Cancel"
                     UpdateText="Update" 
                     HeaderText="Edit item">               <ItemStyle Wrap="False">
                   </ItemStyle>               <HeaderStyle Wrap="False">
                   </HeaderStyle>            </asp:EditCommandColumn>            <asp:ButtonColumn 
                     HeaderText="Delete item" 
                     ButtonType="LinkButton" 
                     Text="Delete" 
                     CommandName="Delete"/>
                 ........
    </Columns>
     
          </asp:DataGrid>void ItemsGrid_Command(Object sender, DataGridCommandEventArgs e)
          {         switch(((LinkButton)e.CommandSource).CommandName)
             {            case "Delete":
                   DeleteItem(e);
                   break;            // Add other cases here, if there are multiple ButtonColumns in 
                // the DataGrid control.            default:
                   // Do nothing.
                   break;         }      }