RadioButtonList回发问题

时间:2011-01-27 23:19:39

标签: asp.net postback radiobuttonlist

环境:ASP NET 2.0 - Production Server没有Ajax Control Toolkit,所以这里没有真正的Control工具包。

3 RadioButtons列表: 列出一个载荷,在回发后,列表一中的项目用于选择Lab值。 选择实验室值后,将填充第3个radiututtonlist。有一些文本框但它们未在示例中显示。文本框回发自己的变化。如果两个文本框都是 不为空,为会话创建记录。

现在,如果第3个radiobuttonlist从默认值更改,则会出现一系列3个隐藏用户控件,这些控件代表更改的3个级别原因(数据库中的子/父记录)。

我遇到的问题是当我在radiobuttonlist上选择一个不同的项目时,radiobutton 3 OnSelectedIndex在我的用户控制着火后触发。我的用户控件需要第三个列表的值才能转到数据库并获取与实验室关联的正确记录集。

问题是因为直到web控件加载之后才处理最后的radiobuttonlist,安装用户控件的代码永远不会发生。

以下是基本的HTML代码:           

    <asp:RadioButtonList ID="rdoLab" runat="server" OnSelectedIndexChanged="rdoLab_OnSelectedIndexChange">
    </asp:RadioButtonList>
    <asp:TextBox ID="textbox1" runat="server" OnTextChanged="TextBoxProcess" />
    <asp:TextBox ID="textbox2" runat="server" OnTextChanged="TextBoxProcess" />
    <asp:RadioButtonList ID="rdoPrimary" RepeatColumns="3" OnSelectedIndexChanged="rdoPrimary_OnSelectedIndexChanged"  runat="server"  ToolTip="Select Normal, Hypo or Hyper - Normal is default value." AutoPostBack="True" >
        <asp:ListItem Value="3" Text="Normal" Selected="true"/>
        <asp:ListItem Value="1" Text="Hypo" />                                
        <asp:ListItem Value="2" Text="Hyper" />
    </asp:RadioButtonList>
     <asp:Panel ID="UpdLab" runat="server" Visible="true"  EnableViewState="true">
            <asp:Table ID="tblAdmin" runat="server">
                <asp:TableRow>
                      <asp:TableCell runat="server" id="tblCell1" Visible="false" CssClass="tdCell"  VerticalAlign="top"> 
                            <uc1:Lab ID="Lab1" runat="server" EnableViewState="true" EnableTheming="true" />                                            
                      </asp:TableCell>
                      <asp:TableCell runat="server" ID="tblCell2" Visible="false"  CssClass="tdCell" VerticalAlign="top">
                            <uc1:Lab ID="Lab2" runat="server" EnableViewState="true" EnableTheming="true" />
                       </asp:TableCell>
                       <asp:TableCell runat="server" ID="tblCell3" Visible="false"   CssClass="tdCell" VerticalAlign="top">
                            <uc1:Lab ID="Lab3" runat="server" EnableViewState="true" EnableTheming="true" />
                       </asp:TableCell>
                </asp:TableRow>
            </asp:Table>

    </asp:Panel>                            

这是背后的页面:

protected override void OnPreLoad(EventArgs e)
    {
        base.OnPreLoad(e);
        GetSessionVars();

        if (CommonUI.strTest((string)Session["rdoLabs"]) && CommonUI.strTest((string)Session["rdoPrimary"]) && Convert.ToString(hrdoLabs.Value) == (string)Session["rdoLabs"])
        {
            divLabLvl.Visible = true;
            // Get cboListItems from the web user controls...
            Session["ArrLstItems"] = "";
            ArrayList ArrLstItems = new ArrayList();
            ArrayList GetWuc = GetWUCS();

            for (int i = 0; i < GetWuc.Count; i++)
            {
                Lab wuc = (Lab)GetWuc[i];
                CheckBoxList cboItemList = (CheckBoxList)wuc.FindControl("cboItems");
                string cboItems = GetCboItemList(cboItemList);
                HiddenField hcboItems = (HiddenField)wuc.FindControl("hcboItems");
            }
            Session["ArrLstItems"] = (ArrayList)ArrLstItems;
        }

    } 
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            DbDataReader ddrGrp = rdoGroups();
            if (ddrGrp.HasRows)
            {
                rdoGroup.DataSource = ddrGrp;
                rdoGroup.DataBind();
            }
            ddrGrp.Close();
        }
        else
        {
            DbDataReader ddrLab = rdoUserLabs();
            if (ddrLab.HasRows)
            {
                rdoLabs.DataSource = ddrLab;
                rdoLabs.DataBind();
                if (CommonUI.strTest((string)Session["rdoLabs"]))
                {
                    if (Convert.ToInt32(Session["rdoLabs"]) > 0)
                    {
                        rdoLabs.SelectedValue = (string)Session["rdoLabs"];
                        SetLabCss();
                    }
                }
            }
            ddrLab.Close();
        }

    }

    protected void rdoGroup_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        //...do some stuff
    }

    protected void rdoLabs_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        //... reload 
    }

    protected DbDataReader rdoGroups()
    {

        int group_type_id = GroupTypeId();
        Group grp = new Group();
        return grp.GetGroups(group_type_id);

    }

    protected DbDataReader rdoUserLabs()
    {
        RadioButtonList rdoGrp = (RadioButtonList)CommonUI.FindCtrlRecursive(this.Master, "rdoGroup");
        int GroupId = Convert.ToInt32(rdoGrp.SelectedValue);
        LabAbnormalReasons lar = new LabAbnormalReasons();
        return lar.GetLabsList(GroupId);
    }

    protected void rdoPrimary_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        Session["Save"] = ((RadioButtonList)sender).ID;
        RadioButtonList rdoGroups = (RadioButtonList)CommonUI.FindCtrlRecursive(this.Master, "rdoGroup");
        RadioButtonList rdoLabs = (RadioButtonList)CommonUI.FindCtrlRecursive(this.Master, "rdoLabs");
        int UserId = Convert.ToInt32(Session["UserId"]);
        int DocId = Convert.ToInt32(Session["DocId"]);
        SubmitLab_Data(arrLstItems, arrOthers);
    }

    protected void GetSessionVars()
    {

        RadioButtonList rdoGroup = (RadioButtonList)CommonUI.FindCtrlRecursive(this.Master, "rdoGroup");
        RadioButtonList rdoPrimary = (RadioButtonList)CommonUI.FindCtrlRecursive(this.Master, "rdoPrimary");
        RadioButtonList rdoLabs = (RadioButtonList)CommonUI.FindCtrlRecursive(this.Master, "rdoLabs");

        if (rdoGroup.SelectedIndex != -1)
        {
            Session["rdoGroup"] = (string)rdoGroup.SelectedValue;
        }
        if (rdoLabs.SelectedIndex != -1)
        {
            Session["rdoLabs"] = (string)rdoLabs.SelectedValue;
        }
        if (rdoPrimary.SelectedIndex != -1)
        {
            Session["rdoPrimary"] = (string)rdoPrimary.SelectedValue;

        }

    }

以下是用户代码的示例:

此代码永远不会发生,因为此处没有第3个按钮列表数据:

protected void Page_Load(object sender, EventArgs e)
    {
        /////*
        //// *  lab & Primary have been selected... 
        //// */
        int lvl = SetLvlId();
        int par_id = GetParentLvl();
        Lab wuc = GetWuc(lvl);
        if (wuc != null)
        {
            if (CommonUI.strTest(Convert.ToString(Session["rdoLabs"])) && CommonUI.strTest(Convert.ToString(Session["rdoPrimary"]))) 
            { 
                    // data in data base for this user, lab, doc identifier...
                    if (Convert.ToInt32(Session["rdoPrimary"]) > 0
                     {
                        // have user hdr data - see if item data is mapped...
                       // do some stuff here
                    }

            } 
        }
    }

我希望这很清楚。我喜欢

--- * ---自原始发布: 添加了简单的javascript / OnDataBound  功能主要(对象)     {        alert(“Value Clicked:”+ object);     }

                                                                                                                 
                                                                                                                                                                  

protected void rdoPrimary_DataBound(object sender,EventArgs e)     {         RadioButtonList rdlPrimary =(RadioButtonList)sender;         foreach(rdlPrimary.Items中的ListItem li)         {             li.Attributes.Add(“onclick”,“javascript:Primary('”+ li.Value +“')”);         }     }

1 个答案:

答案 0 :(得分:0)

在SaveViewState和LoadViewState方法中存储并检索要保留的值,看看它是否适合您?还要查看用于处理逻辑的早期和晚期生命周期事件 - Init和OnPreRender。这对我来说过去很有用。