本文介绍了数据列表查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在DataList1中有一个数据列表控件调用DataList1和另一个数据列表控件调用DataList3.现在我在datalist3中有一张图像,必须在运行时为其设置可见性.我在PageLoad上编写了以下代码:
// 在DataList3中搜索记录 如果((((DataList)(DataList1.Items [ 0 ].FindControl(" DataList3")))).Items.Count < = 0 ) { // ,如果没有找到记录,则显示Image20而不是DataList3 (((DataList)(DataList1.Items [ 0 ].FindControl(" DataList3" )))).可见= false ; (((System.Web.UI.WebControls.Image)(DataList1.Items [ 0 ].FindControl(" Image40"))))).Visible = true ; } 其他 { // 如果找到的记录比显示DtaList3而不是Image20 (((DataList)(DataList1.Items [ 0 ].FindControl(" DataList3" )))).可见= true ; (((System.Web.UI.WebControls.Image)(DataList1.Items [ 0 ].FindControl(" Image40"))))).可见= = false ; }
但我找不到任何需要的结果.图像和数据列表都同时显示.
任何指导都会有所帮助.
我的整个页面加载量
使用系统; 使用 System.Data; 使用 System.Configuration; 使用 System.Collections; 使用 System.Web; 使用 System.Web.Security; 使用 System.Web.UI; 使用使用System.Web.UI.WebControls; 使用 System.Web.UI.WebControls.WebParts; 使用 System.Web.UI.HtmlControls; 使用 System.Data.SqlClient; 使用 System.Data.Sql; 使用 System.IO; 公共 部分 类 free_user_ReceivedInterestPending:System.Web .UI.页面 { 受保护的 无效 Page_Load(对象发件人,EventArgs e) { // 在DataList3中搜索记录 如果((((DataList)(DataList1.Items [ 0 ].FindControl(" DataList3")))).Items.Count < = 0 ) { // ,如果没有找到记录,则显示Image20而不是DataList3 (((DataList)(DataList1.Items [ 0 ].FindControl(" DataList3" )))).可见= false ; (((System.Web.UI.WebControls.Image)(DataList1.Items [ 0 ].FindControl(" Image40"))))).Visible = true ; } 其他 { // 如果找到的记录比显示DtaList3而不是Image20 (((DataList)(DataList1.Items [ 0 ].FindControl(" DataList3" )))).可见= true ; (((System.Web.UI.WebControls.Image)(DataList1.Items [ 0 ].FindControl(" Image40"))))).可见= = false ; } } }
datalist1绑定
< asp:DataList ID = " DataList1" runat = " 服务器" DataKeyField = " sr" DataSourceID = " SqlDataSource9" Width = " 100%" onload = " DataList1_Load" > < itemtemplate> < table 类 = " style472" > 样式:" 边框:中号#669999;字体系列:Verdana;字体大小:10pt;高度:281px; " width = " 100%" > < tr> < td bgcolor = " #669999" 类 = " style697" colspan = " 4" style = " vertical-align:top" > < span class = " style696" > < table 类 = " style472" > < tr> < td 类 = " style793" > </ td > < td class = " style790" > < asp:标签ID = " UserFromLabel" runat = " 服务器" style = " font-重量:700英寸 Text = ' <%#Eval("UserFrom")%>'/> </ td > < td 类 = " style797" > < asp:FormView ID = " FormView1" runat = " 服务器" DataKeyNames = " sr" DataSourceID = " SqlDataSource15" > < edititemtemplate> SR: < asp:标签ID = " srLabel1" runat = " 服务器" Text = ' < %#Eval("sr")%>'/> < br/> < asp:LinkButton ID = " UpdateButton" runat = " 服务器" CausesValidation = " True" CommandName = " 更新" 文本= " 更新"/> < asp:LinkButton ID = " UpdateCancelButton" runat = " 服务器" CausesValidation = " False" CommandName = " 取消" Text = " 取消"/> </ edititemtemplate > < insertitemtemplate> < asp:LinkButton ID = " InsertButton" runat = " 服务器" CausesValidation = " True" CommandName = " 插入" Text = " 插入"/> < asp:LinkButton ID = " InsertCancelButton" runat = " 服务器" CausesValidation = " False" CommandName = " 取消" Text = " 取消"/> </ insertitemtemplate > < itemtemplate> < span class = " style696" > UserId:</ span > 跨度> < asp:标签ID = " srLabel2" runat = " 服务器" CssClass = " style696" Text = ' <%#Eval("sr")%>'/> < br/> </ itemtemplate > </ td > < td class = " style794" > </ td > < td 类 = " style792" > < asp:CheckBox ID = " CheckBox5" runat = " 服务器" CssClass = " style791" Text = " 书签" /> </ td > < td class = " style698" > < asp:CheckBox ID = " CheckBox6" runat = " 服务器" CssClass = " style791" Text = " 发送个人资料给朋友" /> </ td > < td class = " style678" > < asp:CheckBox ID = " CheckBox7" runat = " 服务器" CssClass = " style791" Text = " 块配置文件" /> </ td > </ tr > </ 表 > </ span > </ td > </ tr > < tr> < td class = " style718" style = " 垂直对齐:顶部" >> </ td > < td 类 = " style801" style = " 垂直对齐:顶部" >> < asp:TextBox ID = " TextBox22" runat = " 服务器" > </ td > < td class = " style718" style = " 垂直对齐:顶部" >> </ td > < td class = " style718" style = " 垂直对齐:顶部" >> </ td > </ tr > < tr> < td 类 = " style713" style = " 垂直对齐:顶部" >> </ td > < td class = " style802" style = " 垂直对齐:顶部" >> < asp:图片ID = " Image40" runat = " 服务器" Height = " 144px" ImageUrl = " 〜/images/contactus1.png" Width = " 148px" /> < asp:DataList ID = " DataList3" runat = " 服务器" DataKeyField = " sr" DataSourceID = " SqlDataSource14" Width = " 150px" > < itemtemplate> < asp:图片ID = " Image39" runat = " 服务器" Height = " 144px" ImageUrl = ' <%#"ImageHandler.ashx?ImID =" + Eval("sr")%>" Width = " 148px" /> < br/> </ itemtemplate > < asp:SqlDataSource ID = " SqlDataSource14" runat = " 服务器" ConnectionString = " <%$ ConnectionStrings:ConnectionString%>" SelectCommand = " SELECT * FROM [tbl_Image1]在哪里[[UserName] = @UserName)" > < SelectParameters> < asp:ControlParameter ControlID = " UserFromLabel" Name = " 用户名" PropertyName = " 文本" Type = " 字符串"/> </ SelectParameters > < asp:SqlDataSource ID = " SqlDataSource15" runat = " 服务器" ConnectionString = " <%$ ConnectionStrings:ConnectionString%>" SelectCommand = " 在[tbl_FreeUserProfile]中选择[sr]([UserName] = @UserName)" > < SelectParameters> < asp:ControlParameter ControlID = " UserFromLabel" Name = " 用户名" PropertyName = " 文本" Type = " 字符串"/> </ SelectParameters > </ td > < td class = " style714" style = " 垂直对齐:顶部" >> </ td > < td 类 = " style716" style = " 垂直对齐:顶部" >> < asp:DataList ID = " DataList4" runat = " 服务器" DataKeyField = " sr" DataSourceID = " SqlDataSource10" Width = " 100%" > < itemtemplate> < table 类 = " style472" > style = " 字体家族:'Trebuchet MS';字体大小:10pt;高度:44px;" > < tr> < td class = " style719" colspan = " 3" > </ td > </ tr > < tr> < td class = " style805" > 名称</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " NameLabel" runat = " 服务器" CssClass = " style750" Text = ' <%#Eval("Name")%>'/> < asp:标签ID = " MiddleNameLabel0" runat = " 服务器" CssClass = " style750" Text = ' <%#Eval("MiddleName")%>'/> < asp:标签ID = " LastNameLabel1" runat = " 服务器" CssClass = " style750" Text = ' <%#Eval("LastName")%>'/> </ td > </ tr > < tr> < td class = " style805" > 年龄</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " AgeLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval("Age")%>'/> </ td > </ tr > < tr> < td class = " style805" > 宗教</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " ReligionLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval(宗教")%>'/> </ td > </ tr > < tr> < td class = " style805" > 种姓 </ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " CasteLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval("Caste")%>'/> </ td > </ tr > < tr> < td class = " style805" > 权重</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " WeightLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval("Weight")%>'/> </ td > </ tr > < tr> < td class = " style805" > 身高</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " HeightLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval("Height")%>'/> </ td > </ tr > < tr> < td class = " style805" > 教育</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " EducationLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval("Education")%>'/> </ td > </ tr > < tr> < td class = " style805" > 职业</ td > < td 类 = " style694" > :</ td > < td class = " style720" > < asp:标签ID = " OccupationLabel" runat = " 服务器" CssClass = " style710" style = " 字体大小:9pt" Text = ' <%#Eval(职业")%>'/> </ td > </ tr > < tr> < td class = " style805" > 位置</ td > < td 类 = " style694" > :</ td > < td class = " style721" > < asp:标签ID = " CountryLabel" runat = " 服务器" Text = ' < %#Eval(国家/地区")%>'/> ,< asp:标签ID = " stateLabel" runat = " 服务器" Text = ' < ;%#Eval("state")%>'/> ,< asp:标签ID = " cityLabel" runat = " 服务器" Text = ' < ;%#Eval("city")%>'/> </ td > </ tr > < tr> < td 类 = " style705" colspan = " 3" > < table 类 = " style472" > style = " 字体家族:'Trebuchet MS';字体大小:10pt;高度:6px;" > < tr> < td 类 = " style810" style = " 文本对齐:左" >> < asp:HyperLink ID = " HyperLink34" runat = " 服务器" Font-Underline = " 假" NavigateUrl = ' <%#Eval("UserName",〜/付费用户/viewFullProfile.aspx?UserName= { 0})%>' style = " 颜色:#003399" >查看完整的配置文件 </ td > < td class = " style700" style = " 文本对齐:左" >> < asp:HyperLink ID = " HyperLink33" runat = " 服务器" Font-Strikeout = " 假" NavigateUrl = ' <%#Eval("UserName",〜/付费用户/sendMessage.aspx?UserName= { 0})%>' style = " 颜色:#CC6600" >个人消息 </ td > < td class = " style704" style = " 文本对齐:左" >> </ td > </ tr > </ 表 > </ td > </ tr > </ 表 > </ itemtemplate > < br/> < asp:TextBox ID = " TextBox21" runat = " 服务器" > </ td > </ tr > < tr> < td 类 = " style713" style = " 垂直对齐:顶部" >> </ td > < td 类 = " style712" colspan = " 3" > style = " 垂直对齐:顶部; border-top-style:虚线; border-top-width:薄;边框-top-color:#808080;" > < span 类 = " style813" > 消息:</ span > < asp:标签ID = " MessageLabel" runat = " 服务器" Text = ' < %#Eval(消息")%>' style = " 字体家族:'Trebuchet MS'" /> </ td > </ tr > < tr> < td class = " style807" style = " 垂直对齐:顶部" >> </ td > < td 类 = " style808" colspan="3"> </ td > </ tr > </ 表 > <asp:SqlDataSource ID="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tbl_FreeUserProfile] WHERE ([UserName] = @UserName)"> < SelectParameters> <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" PropertyName="Text" Type="String"/> </SelectParameters> </itemtemplate>
解决方案
i have one datalist control call DataList1 and another datalist control call DataList3 inside DataList1. Now i have one image inside datalist3 for which i have to set visibility at runtime.
i have written the following code on PageLoad:
//search for records in DataList3 if (((DataList)(DataList1.Items[0].FindControl("DataList3"))).Items.Count <= 0) { //if no records found than display Image20 instead of DataList3 ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = false; ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = true; } else { //if records found than display DtaList3 instead of Image20 ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = true; ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = false; }
but i don''t find any required results. both image and datalist are displaying in both case.
any guidance will be helpful.
my whole pageload
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Data.Sql; using System.IO; public partial class free_user_ReceivedInterestPending : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //search for records in DataList3 if (((DataList)(DataList1.Items[0].FindControl("DataList3"))).Items.Count <= 0) { //if no records found than display Image20 instead of DataList3 ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = false; ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = true; } else { //if records found than display DtaList3 instead of Image20 ((DataList)(DataList1.Items[0].FindControl("DataList3"))).Visible = true; ((System.Web.UI.WebControls.Image)(DataList1.Items[0].FindControl("Image40"))).Visible = false; } } }
datalist1 binding
<asp:DataList ID="DataList1" runat="server" DataKeyField="sr" DataSourceID="SqlDataSource9" Width="100%" onload="DataList1_Load"> <itemtemplate> <table class="style472"> style="border: medium solid #669999; font-family: Verdana; font-size: 10pt; height: 281px;" width="100%"> <tr> <td bgcolor="#669999" class="style697" colspan="4" style="vertical-align: top"> <span class="style696"> <table class="style472"> <tr> <td class="style793"> </td> <td class="style790"> <asp:Label ID="UserFromLabel" runat="server" style="font-weight: 700" Text='<%# Eval("UserFrom") %>' /> </td> <td class="style797"> <asp:FormView ID="FormView1" runat="server" DataKeyNames="sr" DataSourceID="SqlDataSource15"> <edititemtemplate> sr: <asp:Label ID="srLabel1" runat="server" Text='<%# Eval("sr") %>' /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </edititemtemplate> <insertitemtemplate> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /> </insertitemtemplate> <itemtemplate> <span class="style696">UserId: </span> <asp:Label ID="srLabel2" runat="server" CssClass="style696" Text='<%# Eval("sr") %>' /> <br /> </itemtemplate> </td> <td class="style794"> </td> <td class="style792"> <asp:CheckBox ID="CheckBox5" runat="server" CssClass="style791" Text="Bookmark" /> </td> <td class="style698"> <asp:CheckBox ID="CheckBox6" runat="server" CssClass="style791" Text="Send Profile to Friend" /> </td> <td class="style678"> <asp:CheckBox ID="CheckBox7" runat="server" CssClass="style791" Text="Block Profile" /> </td> </tr> </table> </span> </td> </tr> <tr> <td class="style718" style="vertical-align: top"> </td> <td class="style801" style="vertical-align: top"> <asp:TextBox ID="TextBox22" runat="server"> </td> <td class="style718" style="vertical-align: top"> </td> <td class="style718" style="vertical-align: top"> </td> </tr> <tr> <td class="style713" style="vertical-align: top"> </td> <td class="style802" style="vertical-align: top"> <asp:Image ID="Image40" runat="server" Height="144px" ImageUrl="~/images/contactus1.png" Width="148px" /> <asp:DataList ID="DataList3" runat="server" DataKeyField="sr" DataSourceID="SqlDataSource14" Width="150px"> <itemtemplate> <asp:Image ID="Image39" runat="server" Height="144px" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("sr") %>' Width="148px" /> <br /> </itemtemplate> <asp:SqlDataSource ID="SqlDataSource14" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tbl_Image1] WHERE ([UserName] = @UserName)"> <SelectParameters> <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" PropertyName="Text" Type="String" /> </SelectParameters> <asp:SqlDataSource ID="SqlDataSource15" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [sr] FROM [tbl_FreeUserProfile] WHERE ([UserName] = @UserName)"> <SelectParameters> <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" PropertyName="Text" Type="String" /> </SelectParameters> </td> <td class="style714" style="vertical-align: top"> </td> <td class="style716" style="vertical-align: top"> <asp:DataList ID="DataList4" runat="server" DataKeyField="sr" DataSourceID="SqlDataSource10" Width="100%"> <itemtemplate> <table class="style472"> style="font-family: 'Trebuchet MS'; font-size: 10pt; height: 44px;"> <tr> <td class="style719" colspan="3"> </td> </tr> <tr> <td class="style805"> Name</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="NameLabel" runat="server" CssClass="style750" Text='<%# Eval("Name") %>' /> <asp:Label ID="MiddleNameLabel0" runat="server" CssClass="style750" Text='<%# Eval("MiddleName") %>' /> <asp:Label ID="LastNameLabel1" runat="server" CssClass="style750" Text='<%# Eval("LastName") %>' /> </td> </tr> <tr> <td class="style805"> Age</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="AgeLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Age") %>' /> </td> </tr> <tr> <td class="style805"> Religion</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="ReligionLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Religion") %>' /> </td> </tr> <tr> <td class="style805"> Caste </td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="CasteLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Caste") %>' /> </td> </tr> <tr> <td class="style805"> Weight</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="WeightLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Weight") %>' /> </td> </tr> <tr> <td class="style805"> Height</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="HeightLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Height") %>' /> </td> </tr> <tr> <td class="style805"> Education</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="EducationLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Education") %>' /> </td> </tr> <tr> <td class="style805"> Occupation</td> <td class="style694"> :</td> <td class="style720"> <asp:Label ID="OccupationLabel" runat="server" CssClass="style710" style="font-size: 9pt" Text='<%# Eval("Occupation") %>' /> </td> </tr> <tr> <td class="style805"> Location</td> <td class="style694"> :</td> <td class="style721"> <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' /> , <asp:Label ID="stateLabel" runat="server" Text='<%# Eval("state") %>' /> , <asp:Label ID="cityLabel" runat="server" Text='<%# Eval("city") %>' /> </td> </tr> <tr> <td class="style705" colspan="3"> <table class="style472"> style="font-family: 'Trebuchet MS'; font-size: 10pt; height: 6px;"> <tr> <td class="style810" style="text-align: left"> <asp:HyperLink ID="HyperLink34" runat="server" Font-Underline="False" NavigateUrl='<%# Eval("UserName","~/paid user/viewFullProfile.aspx?UserName={0}") %>' style="color: #003399">View Full Profile </td> <td class="style700" style="text-align: left"> <asp:HyperLink ID="HyperLink33" runat="server" Font-Strikeout="False" NavigateUrl='<%# Eval("UserName","~/paid user/sendMessage.aspx?UserName={0}") %>' style="color: #CC6600">Personal Message </td> <td class="style704" style="text-align: left"> </td> </tr> </table> </td> </tr> </table> </itemtemplate> <br /> <asp:TextBox ID="TextBox21" runat="server"> </td> </tr> <tr> <td class="style713" style="vertical-align: top"> </td> <td class="style712" colspan="3"> style="vertical-align: top; border-top-style: dashed; border-top-width: thin; border-top-color: #808080;"> <span class="style813">Message:</span> <asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Message") %>' style="font-family: 'Trebuchet MS'" /> </td> </tr> <tr> <td class="style807" style="vertical-align: top"> </td> <td class="style808" colspan="3"> </td> </tr> </table> <asp:SqlDataSource ID="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tbl_FreeUserProfile] WHERE ([UserName] = @UserName)"> <SelectParameters> <asp:ControlParameter ControlID="UserFromLabel" Name="UserName" PropertyName="Text" Type="String" /> </SelectParameters> </itemtemplate>
解决方案
这篇关于数据列表查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!