GridView中的RadioButton單選的方法

iDotNetSpace發表於2010-05-31

方法一,客戶端

 //繫結

  protected void gvAssDealReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                RadioButton rb = (RadioButton)e.Row.FindControl("raButton");

                rb.Attributes.Add("onclick", "judge(this)");//給RadioButton新增onclick屬性

            }


        }

指令碼


         var last = null;//最後訪問的RadioButton的ID
         function judge(obj)
        {

             if(last == null)

            {

                 last = obj.id;

            //      alert(last);

             }

             else

            {

                 var lo = document.getElementById(last);

                 lo.checked = "";

          //        alert(last + "   " + lo.checked);

                 last = obj.name;

             }

             obj.checked = "checked";

             var dealid = document.getElementById("");
            
             //dealid.value = obj.;

         }

   

方法二,伺服器端

    protected void raButton_CheckedChanged(object sender, EventArgs e)
        {
            清空checkbox
            foreach (GridViewRow row in this.gvAssDealReport.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    RadioButton rb = (RadioButton)row.FindControl("raButton");
                    rb.Checked = false;                  
                }

            }

           選中
            RadioButton btn = sender as RadioButton;
            btn.Checked = true;

            foreach (GridViewRow row in this.gvAssDealReport.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    RadioButton rb = (RadioButton)row.FindControl("raButton");

                    if (rb.Checked)
                    {
                        txtDealID.Text = row.Cells[1].Text.Trim();
                        btnQueryDealReport_Click(null, null);
                        break;
                    }
                }

            }


        }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-664084/,如需轉載,請註明出處,否則將追究法律責任。

相關文章