在datafrid中添加了一列模板列,定义为单选框列,如何把选中的那一列数据的主码取出来,并且传递到另一张页面里?不同页面之间的传值要用ViewState吗?万分感谢!

解决方案 »

  1.   

    不需要ViewState,直接用参数形式传递过去然后接受就可以了
    单选的可以参考
    <asp:TemplateColumn HeaderText="序号">
    <ItemTemplate>
       <INPUT id=radio onclick=fun_option(this) type=radio value='<%# DataBinder.Eval(Container.DataItem,"UserID") %>' name=radio>                                    </ItemTemplate>                                                                  </asp:TemplateColumn>
    你是在什么时候打开新页面呢?
    如果超连接打开,参考如下
    <asp:TemplateColumn  HeaderText="省市">
    <ItemTemplate>
           <a href='form1.aspx?id=<%# DataBinder.Eval(Container.DataItem,"UserID") %>&name=<%# DataBinder.Eval(Container.DataItem,"UserName") %>' target="_blank">
           <%# DataBinder.Eval(Container.DataItem,"UserName") %>
           </a>
    ItemTemplate>
    </asp:TemplateColumn>参考
    http://singlepine.cnblogs.com/articles/266538.html
      

  2.   

    最简单的办法就是把每行数据的id绑定在单选框的value里可以get方式传啊
      

  3.   

    把每行数据的id绑定在单选框的value里
    例如:<INPUT id=radio onclick=fun_option(this) type=radio value='<%# DataBinder.Eval(Container.DataItem,"UserID") %>' name=radio> 我要把选中的userID号取出来放在一个变量中,然后调用存储过程删除这条以userID为主码的记录,怎么取出来呢?
      

  4.   

    <INPUT type="checkbox" name="cbId" value="<%# Eval("ID") %>">提交后用Request.Form["cbId"]可以得到 "2,323,55,226,884,233" 这样的字符串 可以直接用于SQL命令中,如: string sqlStr = "delete from atable where id in (" + Request.Form["cbId"] + ")";
      

  5.   

    to 楼上:
    我已经照着做了,但ms删不掉。datagrid里显示的数据是调用一个存储过程,而这个存储过程调用了一个视图,并且把主码的列名重新命名了,删除的语句则是直接作用于基本表。这些与不能删除有关系吗?
      

  6.   

    <INPUT id=radio onclick=fun_option(this) type=radio value='<%# DataBinder.Eval(Container.DataItem,"UserID") %>' name=radio>
    就是这样
      

  7.   

    This is actually a known issue that radio buttons in the ItemTemplate  are not mutually exclusive: BUG: Radio Buttons Are Not Mutually Exclusive When Used in a Repeater Server Control 
    http://support.microsoft.com/?scid=kb;en-us;Q316495 We can workaround this by using either client side script or handling the CheckedChanged event on the server side. Here is the way to handle the CheckedChanged  event. Please note that this method requires more postback than client side script does: 1. We can use a Datagrid's to display the rows as Lewis suggested. 
    2. Create a ItemTemplate column and add a RadioButton to the column. Bind the RadioButton appropriately. 
    3. Open the ASPX view of the page, add "OnCheckedChanged" attribute to the <asp:RadioButton> element, and make it point to a public handler method defined in the code behind file. Here is a code snippet for example: 
    In ASPX file 
    ========== 
    <asp:TemplateColumn HeaderText="RadioCol"> 
               <ItemTemplate> 
                       <asp:RadioButton id="RadioButton4" AutoPostBack="True" 
    OnCheckedChanged="RadioButton4_CheckedChanged" runat="server" 
    GroupName="DtFrom"></asp:RadioButton> 
                 </ItemTemplate> 
    </asp:TemplateColumn> In the code behind file 
    =================== 
    public void RadioButton4_CheckedChanged(object sender, System.EventArgs e) 

      RadioButton rb=(RadioButton)sender; 
      Response.Write(rb.ClientID.ToString());  //This line is just for test purpose. 
      for (int i =0 ; i <DataGrid1.Items.Count; i++) 
      { 
            ((RadioButton)DataGrid1.Items[i].Controls[0].Controls[1]).Checked= false; 
      } 
      // check the selected radio button 
      rb.Checked= true; 
    } To get the selected RadioButton when you submit the page, I think you can try the FindCotrol method. You could also handle the submit button's server-side click event and iterate the rows of the table to find out the selected radio button.