GridView RadioButton 样板列

效果图:

aspx HTML部分:
--------------
<script language="javascript" src="Script/SpecialPerform.js"
       type="text/javascript"></script>
...
<asp:GridView ID="gv_SpecialPerformList" runat="server"
     AutoGenerateColumns="False" Width="98%"
     OnRowDataBound="gv_SpecialPerformList_RowDataBound">
   <Columns>
        <asp:TemplateField>
             <ItemTemplate>                           
                   <asp:RadioButton runat="server" ID="rad_Select"/>
             </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="专场编号"  DataField="PerformAutoID" >
        </asp:BoundField>
        <asp:BoundField HeaderText="专场名称"  DataField="PerformName" >
        </asp:BoundField>
    </Columns>
</asp:GridView>
...

aspx cs 部分:
--------------
    #region 专场列表Grid RowDataBound绑定事件
    protected void gv_SpecialPerformList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;
        e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
        e.Row.Attributes.Add("onmouseout", "this.style.cursor=''");
        e.Row.Attributes.Add("onclick", "radButton('" + e.Row.RowIndex.ToString() + "','" + e.Row.Cells[1].Text.Trim() + "');");

    }
    #endregion

//绑定GridView时
//保存本次数据的条数
this.hidtxt_gvSPListRowCount.Text = dt.Rows.Count.ToString();

JS 部分:
--------
function radButton(rowIndex,rowPerformAutoID)
{
    //id="gv_SpecialPerformList_ctl02_rad_Select"
    var rowCount = parseInt(document.all.hidtxt_gvSPListRowCount.value)+2;
   
    for(var i=2;i<rowCount;i++)
    {
        var tmpName;
        if(i<10)
        {
            tmpName = "gv_SpecialPerformList_ctl0"+i+"_rad_Select";               
        }
        else
        {
            tmpName = "gv_SpecialPerformList_ctl"+i+"_rad_Select";   
        }
        //取得对应的Radio对象
        var tmpRadio = document.getElementById(tmpName);
        //当前选中 其他取消选中
        if((i-2) == rowIndex)
        {                 
            tmpRadio.checked = true;
        }
        else
        {
            tmpRadio.checked = false;
        }
    }
    document.all.hidtxt_PerformAutoID.value = rowPerformAutoID;
}

posted on 2008-03-21 11:48  freeliver54  阅读(1310)  评论(0编辑  收藏  举报

导航