How to display panel control based on Listview ItemDataBound value - vb.net

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

Related

Disabling many drop down list all at once using javascript

<table align="center" border="0" id="typeTable" >
<%for(int i=1;i<=count;i++){%>
<tr id="a">
<td align="left" valign="top">
<p>Problem Type <%=i+1 %></p>
</td >
<td align="left" valign="middle">
<p>Number of question to generate: </p>
</td>
<td align="left" valign="middle" >
<select name="type<%=i %>" id="mySelect" >
<option>0</option>
<option>5</option>
<option>10</option>
<option>20</option>
</select>
</td>
</tr>
<%}%>
<input type="hidden" name="totalNumOfType" value="<%=count%>"/>
</table>
Hi, I have the code above to do a for loop for the table row when I get a count from the database to show how many types of problems in math topic.
The drop down menu list name I have put the int I for the name to have each row an individual name so that I can pass the value of each drop down list selected to the next page which I can do successfully.
The issue now is I have problem disabling all the drop down menu list at onece using the javascript as it will not know how many count there will be.
I have used the following code to disable the drop down menu for example.
<script type="text/javascript">
function disable()
{
document.getElementById("mySelect").disabled=true;
}
function enable()
{
document.getElementById("mySelect").disabled=false;
}
</script>
I have been thinking but no avail. any help would be much appreciated. Thank you!
did you tryed jquery prop function : $("#mySelect").prop("disabled",true) ?

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>

ListView selected index

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.

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.