Link to home
Start Free TrialLog in
Avatar of iftech
iftech

asked on

ASP.Net - Repeater control and if statement

Hi, I am trying to set up an ASP.net form that displayes a group of 4 radio buttons, a questions then a group of 3 radio buttons.

Some questions have sub questions, so the sub question will have the radiobuttons and the parent question will not.  How do create an if statement to check and see if the parent should have these shown?  I have a column in my table that says whether it should or not for ease.  Current code attached.
//Repeater code
<table width="100%" cellpadding="0" cellspacing="0" border="0">
    <asp:Repeater runat="server" ID="RepWorkArea" OnItemDataBound="ShowQuestions">
        <ItemTemplate>
            <tr>
                <td colspan="3">
                    <h4><%# ((DataRowView)Container.DataItem)["WorkArea"]%></h4>
                </td>
            </tr>
            <asp:Repeater ID="RepQuestions" OnItemDataBound="ShowSubQuestions" runat="server">
                <itemtemplate> 
                <tr>
                    <td width="90px"><asp:RadioButtonList ID="radQuestionList" runat="server" DataSourceID="SqlDataSource1" DataValueField="SkillImportanceID" OnSelectedIndexChanged="btnViewSkill_Click" RepeatDirection="Horizontal" DataTextField="empty" RepeatLayout="flow"/></td>
                    <td width="*"><b><%# (Container.ItemIndex + 1) %>.</b> <%# ((DataRowView)Container.DataItem)["Question"]%></td>
                    <td width="70px" align="right"><asp:RadioButtonList ID="radSubQuestionList" runat="server" DataSourceID="SqlDataSource2" DataValueField="AnswerID" RepeatDirection="Horizontal" DataTextField="empty" RepeatLayout="flow"/></td>
                </tr>
                <tr>
                    <td colspan="3" align="left">
                        <table width="100%"  cellpadding="0" cellspacing="0" border="0">
                            <asp:repeater id="RepSubQuestions" runat="server"> 
                                <itemtemplate>
                                <tr>
                                    <td width="90px">
                                        <asp:RadioButtonList ID="radQuestionList" runat="server" DataSourceID="SqlDataSource1" DataValueField="SkillImportanceID" OnSelectedIndexChanged="btnViewSkill_Click" RepeatDirection="Horizontal" DataTextField="empty" RepeatLayout="flow"/>
                                    </td>
                                    <td width="*">
                                        <div id="SubQuestions">
                                            <li class="bold"><%# DataBinder.Eval(Container.DataItem, "[\"Question\"]")%></li>
                                        </div>
                                    </td>
                                    <td width="70px" align="right">
                                        <asp:RadioButtonList ID="radSubQuestionList" runat="server" DataSourceID="SqlDataSource2" DataValueField="AnswerID" RepeatDirection="Horizontal" DataTextField="empty" RepeatLayout="flow"/>
                                    </td>
                                </tr>
                                </itemtemplate> 
                            </asp:repeater>   
                        </table>
                    </td>
                </tr>
                </itemtemplate> 
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>
 
//Code Behind
private SqlConnection connString = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());
        
    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql;
        DataSet ds = new DataSet();
        strSql = "select distinct A.* from WorkAreas as A right Join EvaluationQuestions AS B on a.WorkAreaId = B.WorkAreaID AND B.EvaluationID = 1 Order By A.SortOrder";
        SqlDataAdapter daWorkAreas = new SqlDataAdapter(strSql, connString);
        daWorkAreas.Fill(ds, "WorkAreas");
        strSql = "select * from EvaluationQuestions WHERE ParentId = -1 AND Active = 1 AND EvaluationID = 1 Order By EvaluationQuestionID";
        SqlDataAdapter daEvaluationQuestions = new SqlDataAdapter(strSql, connString);
        daEvaluationQuestions.Fill(ds, "EvaluationQuestions");
        strSql = "select * from EvaluationQuestions WHERE Active = 1 AND EvaluationID = 1 Order By EvaluationQuestionID";
        SqlDataAdapter daSubQuestions = new SqlDataAdapter(strSql, connString);
        daSubQuestions.Fill(ds, "SubQuestions");
        DataRelation rel = new DataRelation("QuestionsRel",ds.Tables["WorkAreas"].Columns["WorkAreaID"], ds.Tables["EvaluationQuestions"].Columns["WorkAreaID"],false);
        ds.Relations.Add(rel);
        DataRelation rel2 = new DataRelation("QuestionsRel2", ds.Tables["EvaluationQuestions"].Columns["EvaluationQuestionID"], ds.Tables["SubQuestions"].Columns["ParentID"], false);
        ds.Relations.Add(rel2);
        RepWorkArea.DataSource = ds.Tables["WorkAreas"].DefaultView;
        RepWorkArea.DataBind();
        connString.Close();
 
    }
 
    public void ShowQuestions(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ((Repeater)e.Item.FindControl("RepQuestions")).DataSource =
            ((DataRowView)e.Item.DataItem).CreateChildView("QuestionsRel");
            ((Repeater)e.Item.FindControl("RepQuestions")).DataBind();
        }
    }
 
    public void ShowSubQuestions(Object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ((Repeater)e.Item.FindControl("RepSubQuestions")).DataSource =
            ((DataRowView)e.Item.DataItem).CreateChildView("QuestionsRel2");
            ((Repeater)e.Item.FindControl("RepSubQuestions")).DataBind();
        }
    }

Open in new window

Avatar of RDdice
RDdice
Flag of United States of America image

Only way i can think of to accomplish what you looking for is to do it pragmatically on the backend. You would have to iterate though each radio button, check your conditions and then use the Visible attribute on the TD. Make sure you do a runat="server" and give your TD an instance name if you do this.

You could do this with a Bit of Javascript, but each solution is pretty complex.

I would recommend Divs. Might make your solution a bit more elegant.
ASKER CERTIFIED SOLUTION
Avatar of iftech
iftech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial