Reputation: 184
I want to visible div if my condition is satisfied. I use various way but it's give me an error. "Cannot convert type 'string' to 'bool'" what should i do for it?
I am working with Gridview
It's give me build time error.
<asp:TemplateField HeaderText="Stock"> <ItemTemplate> <div id="divfake" visible='<%# ((Convert.ToInt32(Eval("AlertQty")) < Convert.ToInt32(Eval("InHandStock"))) ? "true" : "False") %>' runat="server"> <%# Eval("InHandStock")%> </div> </ItemTemplate> </asp:TemplateField>
Upvotes: 0
Views: 196
Reputation: 1079
Place your values in HiddenFields
and change your div
into a Panel
control.
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfAlertQty" runat="server" Value='<%# Eval("AlertQty") %>' />
<asp:HiddenField ID="hfInHandStock" runat="server" Value='<%# Eval("InHandStock") %>' />
<asp:Panel ID="divfake" runat="server">
<%# Eval("InHandStock")%>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
Then on your codebehind,
foreach(GridViewRow row in GridView1.Rows) //check condition for each row and set visibility of the Panel
{
if(row.RowType == DataControlRowType.DataRow) //do the following for rows that only have the data
{
HiddenField hfAlert = (HiddenField)row.FindControl("hfAlertQty"); //looks for the value of alert in HiddenField
HiddenField hfStock = (HiddenField)row.FindControl("hfInHandStock"); //looks for the value of stock in HiddenField
Panel div = (Panel)row.FindControl("divFake"); //looks for the Panel to hide / show
if(Convert.ToInt32(hfAlert.Value) < Convert.ToInt32(hfStock.Value)) //if alert is lesser than the stock hide, else show
{
div.Visible = false;
}
else
{
div.Visible = true;
}
}
}
Upvotes: 1
Reputation: 6999
try this
<asp:TemplateField HeaderText="Stock">
<ItemTemplate>
<div id="divfake" visible='<%#GetDivVisibility(Eval("AlertQty"),Eval("InHandStock"))%>'>
</div>
</ItemTemplate>
</asp:TemplateField>
Just add this function, inside C# with public access level
public bool GetDivVisibility(object alertQty, object inHandStock)
{
return Convert.ToInt32(alertQty) < Convert.ToInt32(inHandStock) ? true : false;
}
Upvotes: 1
Reputation: 1721
Try:
<div id="divfake" visible='<%= ((Convert.ToInt32(Eval("AlertQty")) <
Convert.ToInt32(Eval("InHandStock"))) ? "visible" : "hidden") %>' runat="server">
I guess that your Eval and Convert.ToInt32 are returning correct values.
Upvotes: 0