现在的位置: 首页 > 综合 > 正文

(OnClientClick 和 OnClick 新页面回传值)(gridview中获取控件 获取值)

2012年12月23日 ⁄ 综合 ⁄ 共 2476字 ⁄ 字号 评论关闭

曾经遇到这么一个问题 
在一个弹出的窗口中
我的一个 button 要执行两个事件
一 、
    protected void Button1_Click(object sender, EventArgs e)
    {
string revengeboy="";
revengeboy = "我是杀手马大哈一天到晚杀杀杀";//这个只是做例子随便写了个
    }
二、
之后还得执行这个事件
<script language="javascript" type="text/javascript">
// <!CDATA[

  function InputButton1_onclick()
  {
        window.dialogArguments.aspnetForm.InputRoles.value = '<%= revengeboy%>';//把值回传
        window.close(); //关闭窗口
   }
// ]]>
</script>

郁闷的是OnClientClick  是比 OnClick先执行的 要想达到 先附值在 执行脚本中的方法OnClientClick ="InputButton1_onclick()"  OnClick=Button1_Click 这是完全达不到所要的效果的
而且我自做聪明的以为
    protected void Button1_Click(object sender, EventArgs e)
    {
        GetSelectedRoles();
        this.Button1.Attributes.Add("OnClick", "javascript:InputButton1_onclick()");
    }
这样可以达到效果  ,但我错了错的不算很离谱,怎么说呢 因为这个按扭要点两下才可以达到效果;

最终方法:
无奈经历了这么几个错误 ,我想只有把他写到后台页面或许可以...
    protected void Button1_Click(object sender, EventArgs e)
    {
        GetSelectedRoles();
        Response.Write("<script>window.dialogArguments.aspnetForm.InputRoles.value ='"+revengeboy+"';window.close();</script>");  
    } 恩 这个达到了我要的效果 哈哈!!!

    private void  GetSelectedRoles()//从gridview中找到控件并获取值
    {
        foreach (GridViewRow row in GridView1.Rows)
        {

            //this.Lable3.Text +=  + (row.FindControl("Label2") as Label).Text;
            //itemname = itemname + ((Label)GridView1.Rows[row.RowIndex].FindControl("Label2")).Text;//.Cells[4].Controls[0]).Text.Trim();
            roles = ((Label)GridView1.Rows[row.RowIndex].Cells[1].FindControl("TxtRoleName")).Text.ToString();
            CheckBox cb1 = (CheckBox)GridView1.Rows[row.RowIndex].FindControl("CheckBox4");
            CheckBox cb2 = (CheckBox)GridView1.Rows[row.RowIndex].FindControl("CheckBox5");
            CheckBox cb3 = (CheckBox)GridView1.Rows[row.RowIndex].FindControl("CheckBox6");
            if (cb1.Checked == true || cb2.Checked == true || cb3.Checked == true)
            {
                if (cb1.Checked == true)
                {
                    read = "R-Y";
                }
                else
                {
                    read = "R-N";
                }
                if (cb2.Checked == true)
                {
                    write = "W-Y";
                }
                else
                {
                    write = "W-N";
                }
                if (cb3.Checked == true)
                {
                    update = "U-Y";
                }
                else
                {
                    update = "U-N";
                }
                itemname = itemname +  roles + "," + read + "," + write + "," + update+"|" ;
            }
           
        }
        this.Label3.Text = itemname.ToString();
    }

抱歉!评论已关闭.