How to set visibility of ASP.net hyperlinkfield dynamically - c#

I have a DataGridView with ASP.net hyperlink fields inside of it. What I'm trying to do is not display a certain hyperlink based on a condition. I have SQL that determines if the hyperlink should be hidden or not, but I'm having trouble getting this to work in the hyperlinks.
I tried <asp:HyperLinkField....Visible="<%= Eval(Condition) %>" /> where Condition is True or False from my SQL query.
Which of course throws the error Cannot create an object of type 'System.Boolean' from its string representation '<%= Eval(Condition)%>' for the 'Visible' property.
So I understand this from Why will <%= %> expressions as property values on a server-controls lead to a compile errors? and other similar questions.
My question now is: what is the workaround? How can I get the hyperlink to display or not based on my condition?

You cannot change HyperLinkField's Visible on runtime, because it does not have a DataBinding event.
Instead, you should not be changing HyperLinkField's Visible value. The problem is the rest of the column will not be aligned properly if you hide a single cell.
Instead, you want to use TemplateField and HyperLink, and hide a link only (leave the table cell by itself). For example,
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="ConditionHyperLink" runat="server"
Visible='<%# Convert.ToBoolean(Eval("Condition")) %>'
Text="Link" />
</ItemTemplate>
</asp:TemplateField>
FYI: Your syntax is not correct; it should be ='<%# Eval("") %>'

My recommendation would be to handle it in the codebehind, most likely handling the RowCreated event, and setting the Visible property of the control there. There might be more context to your application that I'm missing, but that seems like the simplest way.
This is the event: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated(v=vs.110).aspx
Other events on the gridview in case that one doesn't meet your needs: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events%28v=vs.110%29.aspx

Related

Click on LinkButton in Gridview: why is the Grid reloaded most of the times but not always?

I´m trying to understand the annoying refreshment of the GridView/Repeater that occurs most of the times I click on a LinkButton within that element. If this is caused by the Postback, then why doesn't it happen all the times?
And if it is not caused always, do I might even get rid of it?
I usually do this to get detailed information on a cell clicked in a Grid or repeater:
<asp:LinkButton ID="ButtonSelect" runat="server" CommandName ="Select" CommandArgument = '<%# Eval("date") %>' Text='<%# Bind("TAG") %>' OnClick="GetDetails"/>
The data processed in the "GetDetails" method will then be displayed in some other element. There wouldn't be any need to refresh the Grid.
Is this the normal behavior for any LinkButton click in a GridView?
Martin
It's the normal action of a server button within a gridview. It will cause a postback. A postback will force the Page LifeCycle. The entire page will be recreated and Databinding may or may not occur depending on your cache options and programming.
"Fixing" that really depends what you are trying to accomplish. If it's simply to stop the "screen flicker" due to the postback, consider using AJAX calls or <asp:UpdatePanel> server controls.
If you are trying to work with JS on the clientside and just wish to defer the postback until later, convert the button to a template field and replace the <asp:LinkButton> with a basic html control <a href="javascript:void();" ...>, <button type='button'>, <input type='button' ..., etc...

How to Bind database integer value to TextBox in FormView control?

On my page I have FormView control, and I am binding Integer database field to the TextBox that's residing inside a FormView's EditItemTemplate.
<FormView ...>
<EditItemTemplate>
<dx:ASPxTextBox ID="txtDiameter" runat="server" Text='<%# Bind("Diameter") %>' />
...
<EditItemTemplate>
...
</FormView>
My problem is that when Diameter field is null, the txtDiameter gets value of empty string. When I click Update command (if I didn't provide any numerical value in txtDiameter), a client error is raised
Sys.WebForms.PageRequestManagerServerErrorException: is not a valid value for Int32.
I found some post from 2005 that claims that this is happening due to some bug. Well, now is 2012. The only way I figured how to deal with this is by using FormView_ItemUpdating event, to circle through all problematic values and convert them from String.Empty to null.
I am just little suspicious that it's maybe not necessary. Is there another way to deal with this problem?
Bind the editor's Value property instead:
<dx:ASPxTextBox ID="txtDiameter" runat="server" Value='<%# Bind("Diameter") %>' />
Does this work?

Tie a custom savebutton to itemtemplate textbox

using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
using Northwind;
N00b here,
Trying to customize a asp.net gridview, and I'm stuck at this part...
In the ItemTemplate, I've got a text-box that has a class applied to it where on .keypress(Clientside JS), a asp:LinkButton appears next to it that's formatted like a Jquery UI icon. I'm stuck at this part. I know that I can "enable editing" for the gridview, but I want to make an edit button only appear on .keypress, which I’ve already achieved. The park I’m stuck at is trying to get the click of that asp:LinkButton to send an update command to the SQLserver to update that db entry accordingly . So far, I’ve got the textbox to be created dynamically for each db item in that column.
Is there a more practical way of doing this while achieving the same results? To be more specific, I'm not really looking for code, cause I'm trying to learn, but more for methods I should take a look at, or other tutorials that I missed on the web.
I would also appreciate some advice, thanks in advance: )
You can try using GridView_RowCommand event to fetch the Command event bubbled by Linkbutton.
You can use CommandName Property of the LinkButton and set it to Update.
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:linkbutton ID="Button1" text="TextBox1" runat="server" CommandName="Update" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>

Insert and Edit Template show in Devexpress in aspxGridview

In Insert Mode I want to show one kind of Template and Edit Mode I want to show another Kind of Template in Devexpress Control in C#.NET
This can be implemented using the following approach:
You should define the EditFormTemplate so that it contains a different set of editors for the Insert and Edit functionality. Handle the HtmlRowCreated event to hide non required editors based on the ASPxGridView's IsNewRowEditing property value.
Check out the sample project on this issue which demonstrates the solution based on user controls and binding expressions. In this solution, the EditForm template contains two user controls with the Visible property bound to the ASPxGridView.IsNewRowEditing property:
[HTML]
<uc1:Edit id="Edit1" runat="server" Visible="<%# !Container.Grid.IsNewRowEditing %>"></uc1:Edit>
<uc2:Insert id="Insert1" runat="server" Visible="<%# Container.Grid.IsNewRowEditing %>"></uc2:Insert>
Btw, this issue might also be helpful. If you need more help, please contact the DevExpress support team here.
Use an ASP template field and individual item templates inside your gridview
<asp:TemplateField id="test" runat="server">
<ItemTemplate>
<ItemTemplate>
test
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Text="test" runat="server"/>
</EditItemTemplate>
</ItemTemplate>
</asp:TemplateField>
I did the above from memory. Heres a link to an example i found as well:
http://programming.top54u.com/post/ASP-Net-GridView-Edit-ItemTemplate-Mode.aspx

How to set DropDownList's selected value inside a Repeater?

I need to create a group of DropDownLists to show and allow change of a property group for an item.
I have the following code on my ASP page.
<asp:Repeater runat="server" ID="repeaterProperties">
<ItemTemplate>
<p><asp:DropDownList runat="server" ID="ddProperty" OnInit="ddProperty_OnInit" /><p>
</ItemTemplate>
</asp:Repeater>
The ddProperty_OnInit populates the DropDownList with all the possible values with a database query.
How can I set the selected value of each created DropDownList according to the Repeater's source data?
Let's say, for example, that we have the possible property values of A, B and C.
If the database output for the Repeater contains two of those values, A and B, the Repeater outputs two DropDownLists, both with all 3 values availabla and the first one with A as selected value and the second one with B as selected value.
Edit:
It seems that adding OnItemDataBound="repeater_ItemDataBound" to the Repeater and selecting the appropriate value in there is not the way to go in my case. This is because I also need to save the possibly changed values to a database.
The ItemDataBound event of the Repeater is fired before the OnClick event on a Button and changes the selected values to their old values before the new selections can be saved.
Any suggestion on how to work around this?
Current code:
<asp:Repeater runat="server" ID="repeaterJako" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<asp:DropDownList id="ddJako" runat="server" OnInit="ddJako_OnInit">
</asp:DropDownList><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" id="updateButton" Text="Save" OnClick="update_OnClick" />
In the code-behind, ddJako_OnInit populates the drop down list with all the possible choises, while the repeater_ItemDataBound uses the method suggested by Bryan Parker to select the proper value.
Maybe I'm misunderstanding something about your question... but it seems like this is exactly what OnItemDataBound is for. :)
Use FindControl to get a reference to your DropDownList in the event handler. Also check to make sure the item is not the header/footer. The example from MSDN does both these things:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx
In regard the problem I specified in my edit, the time of the DataBind plays an important role. I used to do the databinding in the Page_Init event, which caused the repeater_ItemDataBound event to be fired before the button_OnClick event.
The solution was to move the databinding to the Page_PreRender event.
The population of the DropDownList with all the choises is still done in its OnInit event.

Categories