[RESOLVED] is it possible using if in datarepeater?
hi all, is it possible using conditional in data repeater?
i have problem when generate data repeater.
Repeater will generate some Pivot table like this:
Quote:
Type | January | February | March
========================================
xxxx |1500 | 0 |1500
yyyy |1800 | 1200 |1230
zzzzz |1200 | 0 |1400
the number will have hyperlink to detail report.
i know how to pass parameter from hyperlink like this;
Quote:
<asp:Repeater ID="ROwnRiskCustomer" runat="server">
<HeaderTemplate>
<table width="60%" border="1">
<tr>
<th>Jenis</th>
<th>Januari</th>
<th>Februari</th>
<th>Maret</th>
</tr>
<ItemTemplate>
<tr>
<td>
<%#DataBinder.Eval(Container.DataItem, "Jenis")%>
</td>
<td>
<a href="OwnRiskCustomerDetail.aspx?th=<%#DataBinder.Eval(Container.DataItem, "Tahun") %>&bl=01"> <%#DataBinder.Eval(Container.DataItem, "01")%></a>
</td>
<td>
<a href="OwnRiskCustomerDetail.aspx?th=<%#DataBinder.Eval(Container.DataItem, "Tahun") %>&bl=02"> <%#DataBinder.Eval(Container.DataItem, "02")%></a>
</td>
<td>
<a href="OwnRiskCustomerDetail.aspx?th=<%#DataBinder.Eval(Container.DataItem, "Tahun") %>&bl=02"> <%#DataBinder.Eval(Container.DataItem, "02")%></a>
</td>
</tr>
this code is succes,but i want add some conditional .
so if the "01","02","03" return number zero then no hyperlink.
how can i add in asp.net??
i am using :
Quote:
<td>
<%
If Eval("01") = 0 Then
%>
0
<%Else%>
<a href="OwnRiskCustomerDetail.aspx?th=<%#DataBinder.Eval(Container.DataItem, "Tahun") %>&bl=01"> <%#DataBinder.Eval(Container.DataItem, "01")%></a>
<% End If
%>
</td>
but this don't work because get error message
"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control" :sick:
any one can give me clue?
before this ,i have do it in Crystal report to show this master detail report,but i getting stuck when passing parameter for hyperlink. so i make backup plan if still don't get it in crystal report
Re: is it possible using if in datarepeater?
Hello,
I would suggest that you take a look at the ItemDataBound event of the Repeater control:
http://msdn.microsoft.com/en-us/libr...databound.aspx
This provides you with Item level access to the rows of the repeater once they have been data bound, at which point, you can put your conditional logic. You could also do all the work that you are doing in the ASPX markup with regard to binding the values.
Gary
Re: is it possible using if in datarepeater?
Handle the Row_Databound Event. And one hyper link control by removing this one and give name
Code:
<td>
<a href="OwnRiskCustomerDetail.aspx?th=<%#DataBinder.Eval(Container.DataItem, "Tahun") %>&bl=01"> <%#DataBinder.Eval(Container.DataItem, "01")%></a>
</td>
And find the hyperlink field using e.findcontrol("ControlName")
And here check your condition , if your condition satisfy set the navigateurl property to the link, else give the text only.
If you give the NavigateURL property then the hyperlink will be displayed else only text will be displayed
Re: is it possible using if in datarepeater?
wow that's really quick reply.
i will try it . thx
Re: is it possible using if in datarepeater?
i still don't understand the explanation in msdn.
in itemdatabound code,because i using a href.
what should i fill in this code
Quote:
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
End If
Re: is it possible using if in datarepeater?
So, rather than use an HTML anchor tag, drop in a Hyperlink control, with a runat server id, and then do a FindControl on that control.
The rest should be very similar to the example included in MSDN.
Gary
Re: is it possible using if in datarepeater?
ok,i will turn into hyperlink control. thx for explanation.
Re: is it possible using if in datarepeater?
Hi Gary and danasegarane.
i am succes make conditional in data repeater . thanks for your help and advice.
please accept my greatful thanks.
step by step:
1. rather than use an HTML anchor tag,use Hyperlink control.
so i change all my a href to hyperlink control. and if i need parameter for my hyperlink and not need to show, i just bind to hiddden field control.
Quote:
<td>
<asp:HiddenField ID="hdThn" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "Tahun")%>' />
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "01")%>'></asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLink2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "02")%>'></asp:HyperLink>
</td>
<td>
<asp:HyperLink ID="HyperLink3" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "03")%>'></asp:HyperLink>
</td>
<td>
2. in event data repeater ItemDataBound,
declare your hyperlink and hidden field.
this example:
Quote:
Dim bln1, bln2, bln3 as HyperLink
Dim hiddenthn As HiddenField
bln1 = TryCast(e.Item.FindControl("HyperLink1"), HyperLink)
bln2 = TryCast(e.Item.FindControl("HyperLink2"), HyperLink)
bln3 = TryCast(e.Item.FindControl("HyperLink3"), HyperLink)
hiddenthn = TryCast(e.Item.FindControl("hdThn"), HiddenField)
3. add conditional statement in code like this(in my case,if the value 0,no hyperlink)
Quote:
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
If bln1.Text <> "0" Then
bln1.NavigateUrl = "OwnRiskCustomerDetail.aspx?th=" & hiddenthn.Value & "&bl=01"
End If
If bln2.Text <> "0" Then
bln1.NavigateUrl = "OwnRiskCustomerDetail.aspx?th=" & hiddenthn.Value & "&bl=03"
End If
If bln3.Text <> "0" Then
bln1.NavigateUrl = "OwnRiskCustomerDetail.aspx?th=" & hiddenthn.Value & "&bl=03"
End If
End IF
This is work, and many thanks!!
Re: is it possible using if in datarepeater?
Hey,
I am glad to hear that you got it working.
The only thing that I would suggest you change would be to do an additional check on bln1, bln2, bln3 and hiddnthn to make sure that they are not Nothing. You are using TryCast, which will not throw an exception, rather if it fails, it will return Nothing. You should check to see whether this is the case before accessing the members of that variable.
Also, can you remember to mark your thread as resolved?
Gary
Re: is it possible using if in datarepeater?
Happy to hear its working...
Re: is it possible using if in datarepeater?
Quote:
Originally Posted by
gep13
Hey,
I am glad to hear that you got it working.
The only thing that I would suggest you change would be to do an additional check on bln1, bln2, bln3 and hiddnthn to make sure that they are not Nothing. You are using TryCast, which will not throw an exception, rather if it fails, it will return Nothing. You should check to see whether this is the case before accessing the members of that variable.
Also, can you remember to mark your thread as resolved?
Gary
i see, thx for your advice. i will add some conditional statement. :thumb:
Re: is it possible using if in datarepeater?
Quote:
Originally Posted by
wilang
i see, thx for your advice. i will add some conditional statement. :thumb:
Good stuff!!
And mark your thread resolved? :)
Re: [RESOLVED] is it possible using if in datarepeater?
sry,forget to mark resolved.