How to set <ItemTemplate> to a datasource? - vb.net

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

Related

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>

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.

Eval ID on radiobutton in Datalist

my code gota datalist with radio button and iv made it single selectable onitemdatabound....now im trying to evaluate a hiddenfield on basis of selected radio button
my code goes like this
aspx code
<asp:DataList ID="DataList1" runat="server" RepeatColumns = "4" CssClass="datalist1"
RepeatLayout = "Table" OnItemDataBound="SOMENAMEItemBound"
CellSpacing="20" onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<br />
<table cellpadding = "5px" cellspacing = "0" class="dlTable">
<tr>
<td align="center">
<a href="<%#Eval("FilePath")%>" target="_blank"><asp:Image ID="Image1" runat="server" CssClass="imu" ImageUrl = '<%# Eval("FilePath")%>'
Width = "100px" Height = "100px" style ="cursor:pointer" />
</td>
</tr>
<tr >
<td align="center">
<asp:RadioButton ID="rdb" runat="server" OnCheckedChanged="rdb_click" AutoPostBack="True" />
<asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
code behind
protected void SOMENAMEItemBound(object sender, DataListItemEventArgs e)
{
RadioButton rdb;
rdb = (RadioButton)e.Item.FindControl("rdb");
if (rdb != null)
rdb.Attributes.Add("onclick", "CheckOnes(this);");
}
protected void rdb_click(object sender, EventArgs e)
{
for (int i = 0; i < DataList1.Items.Count; i++)
{
RadioButton rdb;
rdb = (RadioButton)DataList1.Items[i].FindControl("rdb");
if (rdb != null)
{
if (rdb.Checked)
{
HiddenField hf = (HiddenField)DataList1.Items[i].FindControl("HiddenField1");
Response.Write(hf.Value);
}
}
}
}
the javascript im using...
function CheckOnes(spanChk){
var oItem = spanChk.children;
var theBox= (spanChk.type=="radio") ?
spanChk : spanChk.children.item[0];
xState=theBox.unchecked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm[i].type=="radio" &&
elm[i].id!=theBox.id)
{
elm[i].checked=xState;
}
}
iam getting an error like this
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'pload Demonstration|'.
is there any other way to do this or can nyone plz help to get rid of this problem
The problem is probably your Response.Write() call. Take a look at this blog post. It outlines the reasons why this particular exception shows up, and how to prevent it. Also take a look at this StackOverflow thread.
Okey lets just remove response write...now i have following in my codebehind...rest is same
label5.text=hf.value.ToString();
now i am able to evaluate Label when i use update panel nd nested updatepanel like this
<asp:UpdatePanel ID="UpdatePanel9" runat="server" >
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatColumns = "4"
CssClass="datalist1" OnItemDataBound="SOMENAMEItemBound"
CellSpacing="10" onselectedindexchanged="DataList1_SelectedIndexChanged"
HorizontalAlign="Center" Width="500px">
<ItemTemplate>
<br />
<table cellpadding = "5px" cellspacing = "0" class="dlTable">
<tr>
<td align="center">
<a href="<%#Eval("FilePath")%>" target="_blank"><asp:Image ID="Image1" runat="server" CssClass="imu" ImageUrl = '<%# Eval("FilePath")%>'
Width = "100px" Height = "100px" style ="cursor:pointer" />
</td>
</tr>
<tr >
<td align="center">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="rdb" />
</Triggers>
<ContentTemplate>
<asp:RadioButton ID="rdb" runat="server" OnCheckedChanged="rdb_click" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
is there a way to to get this done without full page postback....asyncpostback trigger doesnt seem to work

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.