datalist项目访问文件后面的代码 [英] datalist items access in code behind file

查看:70
本文介绍了datalist项目访问文件后面的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的情况下,我想根据问题的答案类型显示radiobuttonlist或checkboxlist。但我的代码总是进入第三个块我无法找到错误PLZ帮助我检测问题

asp.net页面如下:

< pre lang =xml> < asp:内容 ID = 主要内容 runat = server ContentPlaceHolderID = 主要内容 >
< style = width:100% >
< tr >

< / tr >
< tr >
< td >
< asp:DataList ID = DataList1 runat = server DataKeyField = QuestionID

< span class =code-attribute> DataSourceID = SqlDataSource1 onitemdatabound = DataList1_ItemDataBound1 >
< ItemTemplate >
& nbsp; < asp:标签 ID = QuestionIDLabel runat = 服务器

< span class =code-attribute> < span class =code-attribute> 文字 =' <% #Eval( QuestionID%> ' / >
< ; br / >
& nbsp; < asp:标签 ID = QuestiontextLabel runat = < span class =code-keyword> server

< span class =code-attribute> < span class =code-attribute> 文字 =' <% #Eval( Questiontext%> ' / >
< br / >
& nbsp; < span class =code-keyword>< asp:Label ID = HelptextLabel runat = server 文字 =' <% #Eval( Helptext%> ' / >
< br / >
& nbsp ; < asp:标签 ID = AnswertypeLabel < span class =code-attribute> runat = server

< span class =code-attribute> < span class =code-attribute> 文字 =' <% #Eval( Answertype%> ' / >
< asp:RadioButtonList ID = RadioButtonList1 runat = 服务器

< span class =code-attribute> < span class =code-attribute> DataSourceID = SqlDataSource1 DataTextField = Choicetext

< span class =code-attribute> < span class =code-attribute> DataValueField = Choicetext >
< / asp:RadioButtonList >
< asp:CheckBoxList ID = CheckBoxList1 runat = server

< span class =code-attribute> DataSourceID = SqlDataSource1 DataTextField = Choicetext

< span class =code-attribute> DataValueField = Choicetext >
< / a sp:CheckBoxList >
< asp:TextBox ID = TextBox1 runat = server TextMode = MultiLine 可见 = False > < / asp:TextBox >
< asp:SqlDataSource ID = SqlDataSource1 runat = 服务器

< span class =code-attribute> ConnectionString = <% $ ConnectionStrings:ConnectionString %>

< span class =code-attribute> SelectCommand = SELECT [Choicetext] FROM [Choices] WHERE([QuestionID] = @QuestionID) >
< SelectParameters >
< asp:ControlParameter ControlID = QuestionIDLabel 名称 = QuestionID

PropertyName = 文字 类型 = Int32 / >
< / SelectParameters >
< / asp:SqlDataSource < span class =code-keyword>>
< br / >
< span class =code-keyword>< br / >
< / ItemTemplate >
< / asp:DataList >
< asp:SqlDataSource < span class =code-attribute> ID = SqlDataSource1 runat = 服务器

< span class =code-attribute> ConnectionString = <% $ ConnectionStrings:ConnectionString %>

< span class =code-attribute> SelectCommand = SELECT [QuestionID],[Questiontext],[Helptext],[Answertype] FROM [Questions] WHERE([SurveyId] ] = @SurveyId) >
< SelectParameters >
< asp:QueryStringParameter 名称 = SurveyId QueryStringField =Surveyid\"
$b$ b 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:

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!

解决方案

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\"

ConnectionStri ng=\"<%


ConnectionStrings:ConnectionString %>\"

SelectCommand=\"SELECT [QuestionID], [Questiontext], [Helptext], [Answertype] FROM [Questions] WHERE ([SurveyId] = @SurveyId)\">
<SelectParameters>
<asp:QueryStringParameter Name=\"SurveyId\" QueryS tringField=\"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:

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!


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
{}
}
}


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:

<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:

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!

解决方案

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:

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!


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
      {}
   }
}


这篇关于datalist项目访问文件后面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆