Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in my case i want to display radiobuttonlist or checkboxlist on basis of answer type of the question. but my code always gets in the third block i am not able to find the error plz help me to detect the problem
the asp.net page is as follow:
XML
<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="MainContent">
 <table style="width: 100%">
            <tr>

            </tr>
            <tr>
                <td>
                    <asp:DataList ID="DataList1" runat="server" DataKeyField="QuestionID"
                        DataSourceID="SqlDataSource1" onitemdatabound="DataList1_ItemDataBound1">
                        <ItemTemplate>
                            &nbsp;<asp:Label ID="QuestionIDLabel" runat="server"
                                Text='<%# Eval("QuestionID") %>' />
                            <br />
                            &nbsp;<asp:Label ID="QuestiontextLabel" runat="server"
                                Text='<%# Eval("Questiontext") %>' />
                            <br />
                            &nbsp;<asp:Label ID="HelptextLabel" runat="server" Text='<%# Eval("Helptext") %>' />
                            <br />
                            &nbsp;<asp:Label ID="AnswertypeLabel" runat="server"
                                Text='<%# Eval("Answertype") %>' />
                            <asp:RadioButtonList ID="RadioButtonList1" runat="server"
                                DataSourceID="SqlDataSource1" DataTextField="Choicetext"
                                DataValueField="Choicetext" >
                            </asp:RadioButtonList>
                            <asp:CheckBoxList ID="CheckBoxList1" runat="server"
                                DataSourceID="SqlDataSource1" DataTextField="Choicetext"
                                DataValueField="Choicetext" >
                            </asp:CheckBoxList>
                            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Visible="False"></asp:TextBox>
                            <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                                ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                SelectCommand="SELECT [Choicetext] FROM [Choices] WHERE ([QuestionID] = @QuestionID)">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="QuestionIDLabel" Name="QuestionID"
                                        PropertyName="Text" Type="Int32" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                            <br />
                            <br />
                        </ItemTemplate>
                    </asp:DataList>
                    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                        SelectCommand="SELECT [QuestionID], [Questiontext], [Helptext], [Answertype] FROM [Questions] WHERE ([SurveyId] = @SurveyId)">
                        <SelectParameters>
                            <asp:QueryStringParameter Name="SurveyId" QueryStringField="Surveyid"
                                Type="Int32" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                </td>
            </tr>
            <tr>
                <td align="center">
                    &nbsp;</td>
            </tr>
        </table>
</asp:Content>


and the codebehind file code is like this one:
C#
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
  {
       rbl= (RadioButtonList)e.Item.FindControl("RadioButtonList1");
       cbl= (CheckBoxList)e.Item.FindControl("CheckBoxList1");
       lb = (Label)e.Item.FindControl("AnswertypeLabel");
       tb = (TextBox)e.Item.FindControl("TextBox1");

       string s = lb.Text;


          if  (s == "M")
          {
              rbl.Visible = true;
              cbl.Visible = false;

          }


          if (s == "C")
          {
              cbl.Visible = true;
              rbl.Visible = false;

          }
          else
          {

              cbl.Visible = false;
              rbl.Visible = false;
              tb.Visible = true;
          }
      }

always third block is executed i.e a textbox is shown even if values of s is "M" or "C"
where is the error i have spend a lot of time to find it out but not succeeded!
Posted
Comments
irfanniazi 3-Mar-14 6:57am    
i want that if answer type is "M" radiobuttonlist should be shown or if it is "C" checkboxlist should appear!!

C#
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
   if (e.CommandName == "GetText")
   {
      DataListItem item=(DataListItem)(((Button)(e.CommandSource)).NamingContainer);
      string text = ((Label)item.FindControl("AnswertypeLabel")).Text.Trim();
      if(text == "M")
      {}
      else if(text == "C")
      {}
      else
      {}
   }
}
 
Share this answer
 
Comments
irfanniazi 3-Mar-14 9:10am    
what is the benifit of using item Comand event here how it is different from my approach
??
PJ003 4-Mar-14 4:55am    
The reason you are geting null value is because the value is still not been set to the control.
C#
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
  {
       rbl= (RadioButtonList)e.Item.FindControl("RadioButtonList1");
       cbl= (CheckBoxList)e.Item.FindControl("CheckBoxList1");
       lb = (Label)e.Item.FindControl("AnswertypeLabel");
       tb = (TextBox)e.Item.FindControl("TextBox1");

       string s = lb.Text;


          if  (s == "M")
          {
              rbl.Visible = true;
              cbl.Visible = false;

          }


          else if (s == "C")
          {
              cbl.Visible = true;
              rbl.Visible = false;

          }
          else
          {

              cbl.Visible = false;
              rbl.Visible = false;
              tb.Visible = true;
          }
      }


If you're not selecting M, then control would go to C, and there you have not selected C as well. Therefore it always excutes the third -- else block. Try with if...else if...else.
-KR
 
Share this answer
 
Comments
irfanniazi 3-Mar-14 10:00am    
S is the text of label and it is M but still not enetring in this block
Krunal Rohit 3-Mar-14 10:01am    
What is text of "lb" ?

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900