ListView selected index - asp.net-4.0

I have a paged multicolumn ListView of items with ImageButton and LinkButton. The delete and select commands are working. I can't get the SelectedIndexChanging event to fire, and the SelectedIndex is always -1 in the Select command handler. I think I have the required select button as per the docs. My ultimate goal is to save the index of the item so when I return to the page I can restore the current ListView pager page so the selected item is visible. But I can't get the item index. This is for asp.net 4.0 webforms.
<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound"
DataKeyNames="ItemID" DataSourceID="ObjectDataSource1"
OnItemCommand="ListView1_ItemCommand" GroupItemCount="2"
onselectedindexchanging="ListView1_SelectedIndexChanging">
<LayoutTemplate>
<table width="100%">
<tr>
<td>
<table class="sample" width="100%">
<asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:ImageButton ID="btnDelete" ToolTip="Delete" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID")%>' CommandName="DeleteItem" Height="12" ImageUrl="resources/delete.gif" Width="12" />
<asp:LinkButton ID="btnSelect" runat="server" CommandName="Select" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ItemID")%>'><%# DataBinder.Eval(Container.DataItem, "ItemName") %></asp:LinkButton>
<asp:Label ID="ccLabel" runat="server"></asp:Label>
</td>
</ItemTemplate>
</asp:ListView>

It would help if you posted your code (SelectedIndexChanging). But in any case, one thought:
SelectedIndexChanging won't give you the selected index, because the index hasn't actually been selected yet.
Use SelectedIndexChanged instead. This occurs after the index has been selected, so can give you a value.

Related

Count items in datasource or inside repeater

I found some other questions about this argument but I can't get anything to work.
I have a repeater with datasource, this is my repeater code:
<div class="container dafareoggi" id="divDaFareOggi">
<div runat="server" id="divSegnaposto"></div>
<asp:Repeater runat="server" ID="rptDaFareOggi" DataSourceID="SqlAttivitaDaFareOggi">
<ItemTemplate>
<div id="<%# Eval("id") %>">
<div class="div-titolo" title="<%# Eval("Titolo") %>"><%# Eval("Titolo") %></div>
<div class="div-testo" title="<%# Eval("Note") %>"><%# Eval("Note") %></div>
<div>
<table style="width: 100%;margin-top:0.5em;padding-right:0.2em;">
<tr>
<td style="width: 50%; text-align: left;">
<asp:ImageButton runat="server" ImageUrl="~/images/gabri.png" Width="2.3em" Height="2.3em" ToolTip='<%#Eval("tecnico")%>' Enabled="false" Visible='<%# IIf(Eval("idutente") = 8, True, False) %>'/>
<asp:ImageButton runat="server" ImageUrl="~/images/giuse.png" Width="2.3em" Height="2.3em" ToolTip='<%#Eval("tecnico")%>' Enabled="false" Visible='<%# IIf(Eval("idutente") = 2, True, False) %>'/>
<asp:ImageButton runat="server" ImageUrl="~/images/robi.png" Width="2.3em" Height="2.3em" ToolTip='<%#Eval("tecnico")%>' Enabled="false" Visible='<%# IIf(Eval("idutente") = 5, True, False) %>'/>
</td>
<td style="width: 50%; text-align: right; ">
<asp:LinkButton CommandName="delAttivita" CommandArgument='<%#Eval("ID")%>' runat="server" ID="lnkDelAtt" CausesValidation="False" OnClientClick="return confirm('Sei sicuro di voler eliminare questa attivita?');"><i class="fa fa-trash fa-lg" title="Elimina attività"></i></asp:LinkButton>
<asp:LinkButton CommandName="editAttivita" CommandArgument='<%#Eval("ID")%>' runat="server" ID="lnkEditAtt"><i class="fa fa-pencil-square fa-lg" title="Modifica attività"></i></asp:LinkButton>
</td>
</tr>
</table>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
In the repeater there is a place card div with id divSegnaposto (2nd row) and then a bunch of div may be loaded accordingly to data read from the db.
What I want is that when there are no data to load, so divSegnaposto is the only item, to show it and, vice versa, when there are 1 ore more other divs, to hide it.
I figure that I should count the number of items inside the repeater or rows inside datasouce (I'm using EF), so I tried different approach in repeater_databound_event, but for example repeater.items.count is always 0.
How can I achieve the goal?
I ended up using a linq query that gets the same as the sqldatasource at itembound event and then I check if it's empty or not and show\hide the placecard accordingly. I don't like it too much because makes me mad: I already have all the information I need inside the repeater or the sql data source, so I guess I thought there was an easier, less code-consuming way to do it. But all the answers I found are more complicated than just re-do the query in code behind...

How to display panel control based on Listview ItemDataBound value

Using asp.net 4.0. I have a page with a listview. I am trying to display a certain pre-defined Panel within the listview itemtemplate based on the current ItemBound value but not on the itemBound event....
For example, if the dataItem.Item("DataGapDesc") value is equal to "A", display Panel pnlPanelA, which will have 2 textboxes. If the dataItem.Item("DataGapDesc") value is equal to "B", display Panel pnlPanelB, which will have 3 textboxes and a checkbox, and so on.
Here's my current listview in the aspx:
<asp:ListView ID="EmployeesGroupedByDataField" runat="server" DataSourceID="sqlDataGapsForSelectedemployees" OnItemBound="employeesGroupedByDataField_ItemDataBound">
<LayoutTemplate>
<table id="Table1" runat="server" class="table1">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table ID="itemPlaceholderContainer" runat="server" >
<tr id="Tr2" runat="server">
<th id="Th1" runat="server" style="text-align:left"><u>
employee</u></th>
<th id="Th2" runat="server" style="width:5%;text-align:center"><u>
# Items Missing</u></th>
<th id="Th3" runat="server"><u>
employee DOB</u></th>
<th id="Th4" runat="server"><u>
Primary Physican</u></th>
<th id="Th6" runat="server" style="width:10%;text-align:center;border-right: thin solid #000000"><u>
Missing Data Item</u></th>
<th id="Th5" runat="server" style="text-align: center;"><u>
Last Known Visit/Service</u></th>
<th id="Th7" runat="server" style="text-align: center;"><u>
Data Entry</u></th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr3" runat="server">
<td id="Td2" runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<%# AddGroupingRowIfemployeeHasChanged()%>
<td style="text-align: right;border-right: thin solid #000000"><%# Eval("DataGapDesc")%>&nbsp</td>
<td style="text-align: left;">
<%#Eval("ServiceDate")%>
&nbsp-&nbsp
<%# Eval("PlaceOfService")%>
</td>
<td>
<%# DisplaySpecificPanel()%>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Im calling the function DisplaySpecificPanel() and this is where I was "trying" to perform this behavior. That function in VB:
Public Function DisplaySpecificPanel() As String
Dim currentEmployeeNameValue As String = Trim(Eval("Employee").ToString().Substring(0, (Eval("Employee").ToString.Length) - 10).ToString())
If currentEmployeeNameValue = "DOE, JOHN" Then
'Panel1.Visible = True
Return String.Format("<asp:Button ID=""Button1"" runat=""server"" Text=""Button"" />")
Else
Return String.Format("<asp:TextBox ID=""TextBox1"" runat=""server""></asp:TextBox>")
End If
End Function
Right now, I'm just trying this functionality out by adding either a textbox or button based on the value but longer term, I wish to add the panels...
Well, my function is being called properly but the controls are not being placed on the rendered listview.
Any ideas? I'm I going about this correct? Many thanks...
Found this from another source. Makes sense but I wasn't completely versed in the lifecycle or behavior of the aspx page.
You will not be able to render server side controls using string
representation of the control. That approach works for plain HTML
controls. But, server side controls need to go through a rendering
mechanism which will not be invoked when you just set text property of
a control. Better alternative would be keeping just one panel in the
item template and adding controls dynamically in the itemdatabound
event based on conditions. Then, You can check if some condition is
true and then add textbox or a button accordingly. If there are too
many controls, you can also use UserControls. But, dynamic controls
will not be retained across page postbacks and you would need to add
them back with the same ID. If you do not want to handle all this
creation mechanism, you could just keep two independent panels and
update the visibility based on some conditions.
within Listview :
<td>
<asp:Panel runat="server" ID="pnlOne" Visible='<%# CanShowFirstPanel()%>'>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
<asp:Panel runat="server" ID="pnlTwo" Visible='<%# CanShowSecondPanel()%>'>
<asp:TextBox runat="server" ID="TextBox1" />
</asp:Panel>
</td>
Code-behind:
Public Function CanShowFirstPanel() As Boolean
'condition to check
Return True
End Function
Public Function CanShowSecondPanel() As Boolean
'condition to check
Return False
End Function

How to check one radio button out of two in vb.net

Suppose I have 2 radio buttons r1 and r2, both the radio buttons ask for your gender, you can be a man or woman.
So what I want: if user checks r1 but then realizes that she is a woman, she then wants to check r2 so the control on r2 gets checked while r1 gets unchecked.
<tr>
<td>
<asp:Label runat="server" text="Chooose Your Category" ID="lblcategory"></asp:Label>
</td>
<td>
<asp:RadioButton runat="server" Text="Male" ID="rbgold" />
</td>
<td>
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" />
</td>
</tr>
What should I do next so as I can choose only one?
Thanks in advance.
Just give the two asp:RadioButtons the same GroupName
As MSDN notes,
Use the GroupName property to specify a grouping of radio buttons to
create a mutually exclusive set of controls. You can use the GroupName
property when only one selection is possible from a list of available
options.
When this property is set, only one RadioButton in the specified group
can be selected at a time.
Example:
<tr>
<td>
<asp:Label runat="server" text="Chooose Your Category" ID="lblcategory">
</asp:Label>
</td>
<td>
<asp:RadioButton runat="server" Text="Male" ID="rbgold" GroupName="GenderGroup" />
</td>
<td>
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" GroupName="GenderGroup" />
</td>
</tr>
You need to put them in the same group so that only one can be selected at a time, something like:
<asp:RadioButton runat="server" Text="Male" ID="rbgold" GroupName="xyzzy" />
<asp:RadioButton runat="server" Text="Female" ID="rbsilver" GroupName="xyzzy" />
Raghav Chopra: There is no need of RadioButtonList for this issue,you only put the radio buttons in same group then only one is selected at a time,and also your problem is solved
I got my answer by using an asp:RadioButtonList
<tr>
<td>
<asp:Label runat="server" text="Chooose Your Category" ID="lblcategory">
</asp:Label>
</td>
<td class="style1">
<asp:RadioButtonList ID="rbgold" runat="server"
RepeatColumns="2"
Width="200px">
<asp:ListItem Text="Silver class" value="1" ></asp:ListItem>
<asp:ListItem Text="Gold class" value="2"></asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>

How to set <ItemTemplate> to a datasource?

How can I set the datasource of an ItemTemplate? Right now I have one that is wrapped by a datarepeater and that is how I databind to it.
<asp:Repeater ID="rptTotal" runat="server">
<ItemTemplate>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
<td>Four</td>
</tr>
</ItemTemplate>
</asp:Repeater>
You cant set the datasource for the item template, you can set it for the repeater in the code behind using
rptTotal.DataSource = datasourcename

VB.Net event handler registration for nested linkbutton

I have a linkbutton that is nested in a datagrid that is nested in a datalist (yes, very strange, but unfortunately it's part of the site which I cannot change). Essentially I want the linkbutton to fire an event handler that calls Response.Redirect(e.CommandArgument)). In other words, I already have the URL that I want to redirect to, but I can't figure out how to get the event to trigger when I click on the linkbutton.
I have tried using the linkbutton OnClick events and the ItemCommand events for the datagrid but I dont think I am registering them correctly.
Here is the HTML for the controls.
<asp:DataList ID="dlstC" BorderWidth="0px" BorderStyle="None" CellPadding="2" CellSpacing="0"
runat="server">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr style="padding-bottom: 4px">
<td style="height: 20px">
<asp:Label runat="server" ID="lblCertNum" Text='<%# "20" + (CStr(Container.DataItem("QuoteID").ToString) + "-" + CStr(Container.DataItem("QuoteRef").ToString)) %>'
Font-Bold="True" Font-Size="8pt"></asp:Label></td>
</tr>
<tr>
<td>
<asp:DataGrid ID="dgd_Certs" runat="server" ShowHeader="False" AutoGenerateColumns="False"
DataSource='<%# GetCert(CInt(Container.DataItem("QuoteRef"))) %>' BorderStyle="None"
BorderWidth="0" BorderColor="#ffffff" CellPadding="4" CellSpacing="0" OnItemCommand="DataGrid_EditItem">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="hlnCert" runat="server" Text='<%# Container.DataItem("CertName").ToString %>' CommandName="RedirectToCert"
CommandArgument='<%# BuildURLToCert(CInt(Container.DataItem("QuoteRef"))) %>' ToolTip="Click to view/edit certificate" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%# "Created - " + CStr(Container.DataItem("DateCreated").ToString)%>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</ItemTemplate>
And in the code behind I have
Public Sub DataGrid_EditItem(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
If e.CommandName = "RedirectToCert" Then
Response.Redirect(e.CommandArgument.ToString)
End If
End Sub
This is my latest attempt where I am trying to get the datagrids ItemCommand to fire when the client clicks on the link, but it's not working at the moment.
Failing this, is there an easier way to redirect the client to the correct page when they click on the linkbutton? I tried using the OnPostBackURL but the issue is that there are objects that need to be carried over that dont seem to be when I do this or when I just use a hyperlink with navigateurl set.
Thanks in advance for any help, this has had me stumped for 2 days straight.
I never managed to get the event handler to fire off of the linkbutton, but I did come up with a work around for the original problem, having variables passed from one page to another. I have used a hyperlink instead of a linkbutton, and used the query strings to pass reference numbers for the objects that I need to access on the new page.