请问高手:为什么我的这个RadioButtonList控件的事件不起作用?
我估计是我在.CS中事件的代码有问题。我现在想实现的功能是.当我在点击RadioButtonList的某个选项项时,然后我在页面中的一个TextBox控件txtIntroduce控件的值(此值读取的是SAT_Explain字段的值)也相应的变化。请高手教教我.致谢!
.aspx中的控件代码:
<asp:RadioButtonList ID="TypeList" runat="server" RepeatDirection="Horizontal" Width="95%"
                        RepeatColumns="4" OnSelectedIndexChanged="TypeList_SelectedIndexChanged">
.cs中的事件代码:
protected void TypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //bind
            SqlDataAdapter myadp = new SqlDataAdapter("select sat_id,sat_name,SAT_Explain from tc_serviceapptype where sat_parentid=2", MyCls.SqlConn);
            DataSet myds = new DataSet();
            myadp.Fill(myds, "sertype");            TypeList.DataSource = myds;
            TypeList.DataValueField = "sat_id";
            TypeList.DataTextField = "sat_name";
            TypeList.DataBind();
            if (this.TypeList.SelectedValue.Equals("1"))
            {
                txtIntroduce.Text = myds.Tables["sertype"].Rows[0]["SAT_Expain"].ToString();
            }

解决方案 »

  1.   

    你在TypeList_SelectedIndexChanged对TypeList进行了重新绑定,所以TypeList选择的永远是绑定时所选择的项。
    建议:将TypeList的绑定移出来。
      

  2.   

    protected   void   TypeList_SelectedIndexChanged(object   sender,   EventArgs   e) 
                    { 
                            //bind 
                            SqlDataAdapter   myadp   =   new   SqlDataAdapter("select   sat_id,sat_name,SAT_Explain   from   tc_serviceapptype   where   sat_parentid=2",   MyCls.SqlConn); 
                            DataSet   myds   =   new   DataSet(); 
                            myadp.Fill(myds,   "sertype");                         TypeList.DataSource   =   myds; 
                            TypeList.DataValueField   =   "sat_id"; 
                            TypeList.DataTextField   =   "sat_name"; 
                            TypeList.DataBind(); 
                            foreach(ListItme li in This.TypeList.Items)
                             {
                            if   (li.selected&&this.TypeList.SelectedValue.Equals("1")) 
                            { 
                                    txtIntroduce.Text   +=   myds.Tables["sertype"].Rows[0]["SAT_Expain"].ToString()+"<br/>"; 
                            }
                           } 
      

  3.   

    老兄:我用的你上面这段代码
            protected void TypeList_SelectedIndexChanged(object sender, EventArgs e)
            {
                SqlDataAdapter myadp = new SqlDataAdapter("select sat_id,sat_name,SAT_Explain from tc_serviceapptype where sat_parentid=2", MyCls.SqlConn);
                DataSet myds = new DataSet();
                myadp.Fill(myds, "sertype");            TypeList.DataSource = myds;
                TypeList.DataValueField = "sat_id";
                TypeList.DataTextField = "sat_name";
                TypeList.DataBind();            foreach (ListItem li in this.TypeList.Items)
                {
                    if (this.TypeList.SelectedValue.Equals("1"))
                    {
                        txtIntroduce.Text = myds.Tables["sertype"].Rows[0]["SAT_Explain"].ToString();
                    }
                }
            }在我选择RadioButtonList控件的其它项时,TextBox控件(控件名是txtIntroduce,与RadioButtonList在同一个页面)的内容总是显示为第一项的内容(此内容是字段SAT_Explain记录的).我现在需要达到的效果是:用户选择RadioButtonList控件的其它项时,TextBox控件中的内容也要跟着读取字段SAT_Explain中的相对应的内容. 请高手帮我改改代码,在下学习一下.
      

  4.   

    是txtIntroduce.Text  + =   myds.Tables["sertype"].Rows[0]["SAT_Explain"].ToString(); 
      

  5.   

    高手:
    现在主要两个问题,解决就好了:
    1、if (this.TypeList.SelectedValue.Equals("1"))这个条件判断的结果老是为false,并且我这写我想是一定有问题的,Equals("1")就是我直截给的一个值,其实这个Equals("")引号中的值是应该变化的。
    2、我现在需要达到的效果是:用户选择RadioButtonList控件的其它项时,TextBox控件中的内容也要跟着读取字段SAT_Explain中的相对应的内容.    也就是用户在点击选择RadioButtonList各个项时,TextBox中要显示不同的内容。
    请高手帮我改改代码,或给我一个关于RadioButtonList控件事件的代码我学习一下。
    我加分了。
      

  6.   

    好的。谢谢老兄提醒。
    我把这个问题的页面代码发到一个链接下面,请高手下载解决:www.tontel.com.cn/download/RadioButtonList.rar
    致谢!
      

  7.   

    高手帮我解决后留个联系试E_mail或MSN或QQ   谢谢!
    我的:E_mail、QQ、MSN都是[email protected]   
      

  8.   

    问题已解决.要将正确事件代码如:
     protected void TypeList_SelectedIndexChanged(object sender, EventArgs e)
            {
                DataSet myds = ViewState["myds"] as DataSet;
                int SelectedIndex = TypeList.SelectedIndex;
                foreach (DataRow row in myds.Tables[0].Rows)
                {
                    if (TypeList.Items[SelectedIndex].Value.Equals(Convert.ToString(row["sat_id"])))
                    {
                        txtIntroduce.Text = Convert.ToString(row["SAT_Explain"]);
                    }
                }不过, 这个情况也要在Page_Load里面添加判断代码.
    if (TypeList.Items.Count > 0)
                {
                    if (TypeList.Items.Count >= SelectedIndex)
                    {
                        TypeList.SelectedIndex = SelectedIndex;
                    }
                    else
                    {
                        TypeList.SelectedIndex = 0;
                    }
                }谢谢各位参与给在下帮忙.