I want to store the value of a databound repeat to a session state.
This is my code so far:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<h1 id="price" class="QPrice">$<%# Eval("Price")%></h1>
</ItemTemplate>
</asp:Repeater>
Code Behind"
Session("Qprice") = ?
How do I retrive the value of the h1 element or retrive a specific value of SqlDatasource1, from the codebehind?
Session("Qprice"+ID) = (decimal)drv.Row["Price"];
In the end you will have all prices in session, and you can access by the id's of the data that has bound to the list. But you are storing information that you have access.
In the repeater1 define the object DatakeyNames="Price" and specify the Item Data Bound event
then in code behind
protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
|| e.Item.ItemType == ListItemType.AlternatingItem)
{
Button b = e.Item.FindControl("myButton") as Button;
DataRowView drv = e.Item.DataItem as DataRowView;
Session("Qprice") = (decimal)drv.Row["Price"];
}
}
Here you can save the value or do whatever you want with price.
I would store it to strongly type List first and store it in Session
Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
Dim repeaterItems As New List(Of Decimal)()
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim b As Button = TryCast(e.Item.FindControl("myButton"), Button)
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
repeaterItems.Add(CDec(drv.Row("Price")))
End If
Session("Qprice") = repeaterItems
End Sub
So I can access it again later like
Dim repeaterItemsFromSession As List(Of Decimal) = DirectCast(Session("Qprice"), List(Of Decimal))
Related
I'm new to ASP and was hoping to get some guidance on how to make my literal accessible in my Code Behind and then change it to the text of a passed in parameter.
I have an resources.ascx file that displays a list of people (pulled from a database). That is working fine and it looks something like this:
Full name
T: (888-888-8888)
F: (888-888-8888)
The problem, however, is that I now want it conditionally say "Toll Free" instead of "F:" for one page.
In the people.aspx page, I'm passing in "Toll Free" to the resource:
<%# Register Src="~/UserControls/resources.ascx" TagName="Resources" TagPrefix="ucResources" %>
<ucResources:Resources ID="Resources1" FaxNumberAlias="Toll Free" runat="server" />
resources.ascx The repeater outputs all the people from the database to the page.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<div class="sectioncontent">
<b><%#Eval("EmployeeFirstName")%> <%#Eval("EmployeeLastName)%></b>
T: <%#Eval("Phone")%>
<br>
<asp:Literal runat="server" ID="FaxNumberLabel">F:</asp:Literal> <%#Eval("Fax")%><br>
</div>
<br />
</ItemTemplate>
In the resources.ascx.vb file, I wanna do something like this but FaxNumberLabel (the literal I declared in resources.ascx) isn't accessible or hasn't been declared.
Public Property FaxNumberAlias() As String
Get
Return _FaxNumberAlias
End Get
Set(ByVal value As String)
_FaxNumberAlias = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(_FaxNumberAlias) Then
FaxNumberLabel.Text = _FaxNumberAlias
End If
PopulateRepeater()
End Sub
What am I missing that connects the literal to the code behind?
The problem is that the Literal is inside a Repeater so you may potentially have lots of them. The best way is to access them inside the OnDataItemBound event of your repeaters:
Protected Sub Repeater1_OnDataItemBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles Repeater1.OnDataItemBound
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim litFaxNumberLabel As Literal = e.Item.FindControl("FaxNumberLabel")
litFaxNumberLabel.Text = _FaxNumberAlias
End If
End Sub
Note: Excuse any bad syntax, it's been over 4 years since I touched VB!
You can simply use like this -
protected void DataDisplay_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
LinkButton lit = (LinkButton)e.Item.FindControl("LinkButton2");
if (lit.Text == "0")
{
int a = Convert.ToInt32(lit.Text);
if (a == 0)
{
if (a == 0)
{
lit.Text = "Your Text";
}
}
}
}
}
In my case, I am using a button (btnRedirect) in each row.
RepeaterItem item = btnRedirect.NamingContainer as RepeaterItem;
var type = item.FindControl("IdUsed") as Literal;
var literalValue = type.Text;
I have two DropDownLists per item on a Repeater.
I am binding both lists to two different lists on the repeater DataBound.
Both lists have an OnSelectedIndexChanged event handler which does some calculations based on the selections in both DropDownLists. Both lists also have AutoPostBack="True".
I need the calculation to be updated immediately. So I added another data binding for the repeater - on the lists' event handler.
This is the problem however - the repeater "resets" the selections to -1 and eventually the first items in both DropDownLists are displayed.
How can I make sure the selections remain after the data binding?
Here is the repeater structure:
<asp:Repeater runat="server" ID="rptCart">
<ItemTemplate>
<tr>
<td class="size"><div><asp:DropDownList runat="server" ID="_selectSize" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="<%#Container.ItemIndex%>"></asp:DropDownList></div></td>
<td class="material"><div><asp:DropDownList runat="server" ID="_selectMaterial" AutoPostBack="true" OnSelectedIndexChanged="selectChange" EnableViewState="true" TabIndex="<%#Container.ItemIndex%>"></asp:DropDownList></div></td>
</tr>
</ItemTemplate>
</asp:Repeater>
And the repeater DataBound:
Protected Sub rptCart_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCart.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
Dim sizeSelect As DropDownList = CType(e.Item.FindControl("_selectSize"), DropDownList)
Dim materialSelect As DropDownList = CType(e.Item.FindControl("_selectMaterial"), DropDownList)
sizeSelect.DataSource = sizeList
sizeSelect.DataBind()
materialSelect.DataSource = materialList
materialSelect.DataBind()
End If
End Sub
And finally the DropDownLists event handler:
Protected Sub selectChange(ByVal sender As DropDownList, ByVal e As System.EventArgs)
Dim listing As New PriceListing
Dim ddl As DropDownList
Dim selectedIndex As Integer
If sender.ID = "_selectSize" Then
For Each rptrItem As RepeaterItem In rptCart.Items
ddl = CType(rptrItem.FindControl("_selectMaterial"), DropDownList)
If ddl.TabIndex = sender.TabIndex Then Exit For
Next
For Each listing In artDecoPricing
If listing.Size = sender.SelectedValue Then Exit For
Next
selectedIndex = ddl.SelectedIndex
ElseIf sender.ID = "_selectMaterial" Then
For Each rptrItem As RepeaterItem In rptCart.Items
ddl = CType(rptrItem.FindControl("_selectSize"), DropDownList)
If ddl.TabIndex = sender.TabIndex Then Exit For
Next
For Each listing In artDecoPricing
If listing.Size = ddl.SelectedValue Then Exit For
Next
selectedIndex = sender.SelectedIndex
End If
Select Case selectedIndex
Case 0
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Canvas
Case 1
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Acrylic
Case 2
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Framed
Case 3
Cart.Order.Items(sender.TabIndex).PriceUnit = listing.Framed
End Select
Cart.SaveOrder()
rptCart.DataSource = Cart.Order.Items
rptCart.DataBind()
End Sub
Thank you very much in advance!
You could store the old selection:
Dim sizeSelect As DropDownList = CType(e.Item.FindControl("_selectSize"), DropDownList)
Dim materialSelect As DropDownList = CType(e.Item.FindControl("_selectMaterial"), DropDownList)
Dim sizeSelectedIndex = sizeSelect.SelectedIndex
Dim materialSelectedIndex = materialSelect.SelectedIndex
' do the databinding ... '
sizeSelect.SelectedIndex = If(sizeSelectedIndex < sizeSelect.Items.Count -1, sizeSelectedIndex, -1)
materialSelect.SelectedIndex = If(materialSelectedIndex < materialSelect.Items.Count -1, materialSelectedIndex, -1)
I have a datalist, and each item in the datalist has a checkbox and disabled button, I want when the checkbox is checked, the button will be enabled .
How to access the check box event for an item in the datalist ?
Please follow the steps given below:
Define OnCheckedChanged method of CheckBox at design time
In the OnCheckedChanged method find DataListItem
Find the required control in the DataList; you can find it in the current row as well using index of the current DataListItem
Change the enable property of the control you found
It'll look something like this:
protected void CheckedChangeMethod(object sender, EventArgs e)
{
CheckBox checkBox = ((CheckBox)sender);
DataListItem item = ((DataListItem)CheckBox.NamingContainer);
if (checkBox.Checked)
{
((Button)dataList.Items[item.ItemIndex].FindControl("btnControl")).Enabled = true;
}
}
I hope this helps.
First you have to declare a Private WithEvents chk As CheckBox (WithEvents will let you have a reference for the checkbox inside the datalist)
Then inside the DataList1_ItemCreated you should do the following:
Private Sub DataList1_ItemCreated(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
chk = CType(e.Item.FindControl("CheckBox1"), CheckBox)
AddHandler chk.CheckedChanged, AddressOf chk_CheckedChanged
End If
End Sub
Make the chk a reference for the checkbox inside the datalist
finally you have to handle the checkbox check/uncheck event (P.S. it is importnant to have your checkbox that is inside the datalist to have the property AutoPostBack set to true):
Private Sub chk_CheckedChanged(sender As Object, e As System.EventArgs) Handles chk.CheckedChanged
Dim cb As CheckBox = CType(sender, CheckBox)
Dim item As DataListItem = DirectCast(cb.NamingContainer, DataListItem)
If cb.Checked Then
DirectCast(DataList1.Items(item.ItemIndex).FindControl("Button1"), Button).Enabled = True
Else
DirectCast(DataList1.Items(item.ItemIndex).FindControl("Button1"), Button).Enabled = False
End If
End Sub
Private Sub chk_CheckedChanged(sender As Object, e As System.EventArgs) Handles chk.CheckedChanged
Dim cb As CheckBox = CType(sender, CheckBox)
Dim item As DataListItem = DirectCast(cb.NamingContainer, DataListItem)
If cb.Checked Then
DirectCast(DataList1.Items(item.ItemIndex).FindControl("Button1"), Button).Enabled = True
Else
DirectCast(DataList1.Items(item.ItemIndex).FindControl("Button1"), Button).Enabled = False
End If
End Sub
I can access the text within a textbox within my repeater, but I am unable to pull the text value from a label within my repeater.
The repeater is populated from a datatable with row(x) being filled by sqlreader(x), I don't know if that makes a difference.
I cannot use javascript for this. I need to access the label value from the codebehind.
<asp:label id="weiLabel" runat="server">
<%#DataBinder.Eval(Container, "DataItem.weiLabel")%>
</asp:label>
is the markup
I can access a textbox on the same row using:
featTable.Controls(1).Controls(1).FindControl("costText")
and retrieve the textbox.text, but using the same statement for the label gives me {text=""}.
I have verified that the clientID of control that is returned with findcontrol is correct (featTable__ctl1_weiLabel)
Thanks for any help
Can you try declaring your label like this:
<asp:label id="weiLabel" runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.weiLabel")%>' / >
You can also try putting in the value into your label from the code behind using the databound method. I find it a bit easier to debug and cleaner then putting it in the html
Private Sub repPoliList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repPoliList.ItemDataBound
If (e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim weiLabel As System.Web.UI.WebControls.Label= CType(e.Item.FindControl("weiLabel"), System.Web.UI.WebControls.Label)
weiLabel.text= dr("ColumnFromDatabase").toString
End If
End Sub
I am trying to access a control inside a Repeater. The control is inside the <ItemTemplate> tag. I am using FindControl but it's always coming out Null.
What am I doing wrong?
My guess is that FindControl can only be used in record-level events such as ItemDataBound:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
(ControlTypeCast) e.Item.FindControl("myControl")).SomeProperty = "foo";
}
I'm guessing that you're trying to find a control at the wrong point in the page lifecycle. The ItemDataBound event is where you need to look for it.
This example is in vb.net, but I'm sure you get the idea.
Protected Sub rp_items_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rp_items.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim someLiteral As Literal = e.Item.FindControl("someliteral")
End If
End Sub
In most cases, spelling the control name wrong :) It may also be that you are searching for a control that exists within another container. Can you post your code?
for (int i = 0; i <= repeater1.Items.Count - 1; i++)
{
Button delete = (Button)repeater1.Items[i].FindControl("btnDelete");
delete.Visible = true;
Button edit = (Button)repeater1.Items[i].FindControl("btnEdit");
edit.Visible = true;
}
Vb.net
For i As Integer = 0 To Repeater1.Items.Count - 1
Dim CmbTyp As DropDownList = DirectCast(Repeater1.Items(i).FindControl("DropDownList1"),DropDownList)
Dim SeatN As Label = DirectCast(Repeater1.Items(i).FindControl("label1"), Label)
styp = CmbTyp.SelectedItem.Text.Trim
sNo = SeatN.Text
Next
Try This
For vb.net
CType(e.Item.FindControl("myControl"), Literal).Text = "foo"
For c#
[Literal]e.item.FindControl["myControl"].Text="foo";