2014年7月13日 星期日

[RESOLVED] Display or Hide Asp.Net Repeater column depend on specific value in the Item value in item template


Hi All,


I want to diaply or Hide Repeater column depend on the repeater item value. here is my code.


<asp:Repeater
ID="rptSearchResults"
runat="server"
DataSourceID="xmlSearchResults"
OnItemDataBound="rptSearchResults_OnItemDataBound"
EnableViewState="false">


<HeaderTemplate><thead>     


<th
id="PFacility"
runat
="server"
visible="false"
align="middle">Preferred<br/>Facility?th>


<th
align="middle">ETIN/NPIth>


<th
align="middle">Provider
Name
th>               


<th
align="middle">Addressth>


<th
align="middle">Cityth>


<th
align="middle">Stateth>


<th
align="middle">ZIP
Code
th>


<th
align="middle">Countyth>


<th
align="middle">Phone
Number
th>


<th
align="middle">Specialtyth>


tr>


thead>


 HeaderTemplate><ItemTemplate>


 <tr
<%
#
RenderTableRowOnClick(Container) %> <%
#
!IsRowSelectable(Container) ? "class=\"Inactive\"" : "" %>
>


 <td
id="Image"
runat
="server"
visible="false"
align
="center">


 <asp:HiddenField
Value='<%#
XPath("./NetworkCode")%>
'
ID="hiddenNetworkCode"
runat="server"
/>


 td>


 <td><asp:LinkButton
ID="lnkDetail"
CssClass="RowSelector"
runat="server">asp:LinkButton><%#
FormattedEtinOrNpi(Container)%>
td>


 <td><%#
FormattedName(Container)%>
td>


 <td><%#
FormattedStreetAddress(Container)%>
td>


 <td><%#
XPath(
"./ContactInformation/City")%>td>


 <td><%#
XPath(
"./ContactInformation/State")%>td>


 <td><%#
XPath(
"./ContactInformation/ZipCode")%>td>


 <td><%#
XPath(
"./ContactInformation/County")%>td>


 <td><%#
FormattedPhone(Container)%>
td>


 <td><%#
XPath(
"./Specialty")%>td>


tr>


ItemTemplate>asp:Repeater>


In code behind if I have data in
 <asp:HiddenField
Value='<%#
XPath("./NetworkCode")%>
'
ID="hiddenNetworkCode"
runat="server"
/>
value I need to display the Column i.e.
 
<asp:HiddenField
Value='<%#
XPath("./NetworkCode")%>
'
ID="hiddenNetworkCode"
runat="server"
/>.
How can I achive this in my code behind. Below is my code behind in .aspx.cs page


protected
void

rptSearchResults_OnItemDataBound(
object
sender,
RepeaterItemEventArgs
e)


 {


HiddenField
hiddenEtin = e.Item.FindControl(
"hiddenNetworkCode")
as
HiddenField;


if
(e.Item.ItemType ==
ListItemType.Header
&&
hdnHeader.Value.Contains("ADVN")
&&
                     WorkflowState.StateData.SearchParameters.ProviderType.Value =="Facility")


 {


               TierIndicator.Visible =true;


       HtmlTableCell
thTierFacility = (
HtmlTableCell)e.Item.FindControl("PFacility");


      
if
(thTierFacility !=
null)
{ thTierFacility.Visible =true; 
}


 }


 if
(hiddenEtin !=
null)


  {


 
string
strNetworkCode = hiddenEtin.Value;


 
if
((e.Item.ItemType ==
ListItemType.Item
|| e.Item.ItemType ==
ListItemType.AlternatingItem)                        
&& WorkflowState.StateData.SearchParameters.ProviderType.Value =="Facility") 
{


HtmlTableCell
tdTierImage = (
HtmlTableCell)e.Item.FindControl("Image");


if
(!
string.IsNullOrEmpty(strNetworkCode)
&& strNetworkCode.Contains(
"ADVN")) 
{


                      TierIndicator.Visible =true;


     
HtmlTableCell
thTierFacility = (HtmlTableCell)e.Item.FindControl("PFacility");


     
if
(thTierFacility !=
null) 
{    thTierFacility.Visible =true;   
}


     
if
(tdTierImage !=
null) 
{ tdTierImage.Visible =true;


     
HtmlImage
tImage =
new
HtmlImage();  
tImage.ID =Guid.NewGuid().ToString().Substring(0,
6);


            tImage.Alt ="";


                           tdTierImage.Controls.Add(tImage);                       }                    }


       
else
   {if
(tdTierImage !=
null)


                      {    tdTierImage.Visible =false;


                           tImage.ID =Guid.NewGuid().ToString().Substring(0,
6);


                            tImage.Alt ="";              
tImage.Src ="";


                            tdTierImage.Controls.Add(tImage);


                        }                   }                }        


           }return;


           }


   return;           
}


 


In the above code if I have data in Item template then I need to dispaly Image in the Item and same time need to display the


Preferred
<
br/>Facility?
Column Header.


Thanks,


Sureya



          





 






Ok, here you need to take a look from a little bit different angle of vision. There is model that you retrieved from database and you bind it to UI in order to display model's data in some special way. The UI may be changed as much as needed, i.e. replaced
by GridView or splitted up to several controls, hence you should not rely on it. This means you do not need to search for hidden controls (markup-specific things) in item template, rather than that use the data in your model:


protected void outerRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
//extract NetworkCode value from underlying model
IXPathNavigable x = (IXPathNavigable)e.Item.DataItem;
XPathNavigator nav = x.CreateNavigator();
XmlElement root = (XmlElement)nav.UnderlyingObject;
string networkCode = root.SelectSingleNode("NetworkCode").InnerText;
//hide/display the column in header
var isVisible = networkCode.ToUpper().Contains("ADVN");
var column = e.Item.FindControl("PFacility") as Control;
if (column != null)
{
column.Visible = isVisible;
}
}
}

沒有留言:

張貼留言