I have a user control inside of a repeater that is being bound by a sqldatasource. I get the following error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. EDIT: NEVERMIND Egg on my face. I was getting this databind error because I was binding it somewhere else in an effort to troubleshoot my real problem from last friday but I forgot about it.
WHAT MY REAL PROBLEM IS: The usercontrol is getting bound before the properties get set so it appears as if they are never set. When stepping through I see that they get on the property is hit before the set on the property is hit. For example if I put <%# EVal("Address_ID") %> before the user control I will see the ID displayed but then the user control will display an emptydatatemplate because it is being passed the ID of 0.
<asp:SqlDataSource ID="sqlFacilityAddresses" runat="server" DataSourceMode="DataSet" SelectCommandType="StoredProcedure" SelectCommand="SP_Facility_GetAddresses" ConnectionString="<%$ ConnectionStrings:Trustaff_ESig2 %>">
<SelectParameters>
<asp:Parameter Name="Facility_ID" DbType="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="repeaterAddresses" DataSourceID="sqlFacilityAddresses" runat="server">
<ItemTemplate>
<Select:Address ID="AddressControl" runat="server" AddressID='<%# EVal("Address_ID") %>' />
</ItemTemplate>
</asp:Repeater>
You could handle the repeater's ItemDataBound-Event in codebehind, get a reference to the UserControl via Item.FindControl and set the property according to the Item.DataItem object and your column Address_ID.
For example(debug to see if the type of your dataitem is really DataRowView):
Sub repeaterAddresses_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)
' This event is raised for the header, the footer, separators, and items.
' Execute the following logic for Items and Alternating Items.
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim addressControl as AddressControl = DirectCast(e.Item.FindControl("AddressControl", AddressControl)
addressControl.AddressID = DirectCast(e.Item.DataItem, DataRowView)("Address_ID").ToString
End If
End Sub
What exactly does your Address UC look like? You can use your AddressID property to do this: e.g.
private bool _AddressID;
public bool AddressID
{
get { return _AddressID; }
set
{
if (_AddressID != value)
{
//addressid is changed
_AddressID = value;
ReloadMyUC();
}
}
}
The ReloadMyUC method does the job of getting data and rebinding the UC.
I just figured this out. First I changed the repeater event to itemcreated instead of itemdatabound but then the repeater was being databound before the event that was resetting the page was being executed which resulted in a 0 ID being sent to my address user control. What I ended up doing was creating a boolean value for the class page setting it to true on the user control raised event this way when it went through the repeater itemcreated event the first time it wouldn't error out and when it went through the second time it would work correctly. This is probably not a best practices way of accomplishing this result but it works.
Maybe using DataBinder.Eval instead of Eval would help?
Related
I have a Results.aspx page that displays the resulting records queried using a SqlDataSource object via a ListView. I want to add a "View" button that will appear next to each record, and when clicked will take me to a separate page that will display details about that record. How do I accomplish this?
Edit
I have tried what you said, citronas and here's what I've come up with:
<td>
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="ViewButtonClick" CommandArgument='<%# Eval("ServiceId") %>'>View</asp:LinkButton>
</td>
And here is the method that I want to be called:
protected void ViewButtonClick(object sender, CommandEventArgs e)
{
var serviceId = Convert.ToInt32(e.CommandArgument);
ServiceToView = DataAccessLayer.Service.Select(new Service { ServiceId = serviceId });
Server.Transfer("~/ViewService.aspx");
}
Unfortunately nothing actually happens...am I missing something?
Edit -- Fixed
I was missing something! I had CommandName equal to my method name instead of OnCommand. I took out CommandName, kept the argument bit and replaced CommandName with OnCommand. Everything works now, but what would I ever need CommandName for?
You can add a LinkButton into the ItemTemplate of the ListView.
Bind the value that identifies each record to the CommandArgument of the LinkButton.
Subscribe to the Command-Event of the LinkButton. There you have access to CommandEventArgs.CommandArgument
What you wound up doing worked Storm. I decided to go with Citronas' suggestion and share my answer.
FIRST:
On the aspx I added a LinkButton to my ItemTemplate with my own CommandName and CommandArgument. I passed my item's ID as a CommandArgument so I could later use it inside my sub.
<asp:LinkButton ID="lnkBtnAnswers" runat="server" CommandName="Answers"
CommandArgument='<%# Eval("ID")%>'>Answers</asp:LinkButton>
SECOND:
On the codebehind I created a sub that would be called whenever the user conducted an action. As Citronas mentioned normally you use "Select", "Add", "Edit", or "Delete" here. I decided to create "answers".
Note: Handles MyControl.ItemCommand is very important here as it is what subscribes you to the command event.
Protected Sub lvQuestions_Command(sender As Object, e As CommandEventArgs) Handles lvQuestions.ItemCommand
If e.CommandName.ToLower() = "answers" Then
hfSelectedQuestionID.Value = e.CommandArgument
End If
End Sub
Done! Now since every command goes through the new sub, it is important to check for the right commandName so you can conduct the appropriate action. Don't forget to use the CommandArgument to your advantage.
As a workaround for the fact that asp:Checkboxes don't have values, I am attempting to dynamically create the ID's of checkboxes in a DataList so that it inserts the primary keys into the control ID. This is surprisingly difficult.
I have placed a PlaceHolder in my DataList ItemTemplate, then in the ItemCreated I create the checkboxes using string.Format("Checkbox{0}", DataBinder(e.Item.DataItem, "ID")). The problem is that this only works in a non-postback condition, as on postback the DataItem is null. And of course ItemDataBound isn't called on PostBack so that won't work either.
I can't seem to find a good way to handle this short of if (IsPostback) dataList.Bind(), which i don't think is a good way to do it.
Can anyone provide me with any alternatives here?
EDIT:
Some additional information. I just realized that part of the problem was because I actually have a DataList within a DataList. The reason DataItem is null is because there is no databinding on postback, and the child data is not saved to viewstate.
Basically, what i'm doing is This, although it's using a DataList rather than Repeater. So, on postback, the Children collection doesn't get set because ItemDataBound isn't called on postback.
EDIT2: To clarify, the problem is largely because of the nested datalists. I have to set the datasource of the nested datalist to a collection field of the first datalist's individual rows fields. On postback, there is no databinding, so it doesn't work.
You could use a similar technique to the one I wrote up in this answer - add a regular CheckBox, and a HiddenField control in the ItemTemplate, and bind the HiddenField to the primary key value e.g.
<ItemTemplate>
<tr>
<td>
<asp:CheckBox runat="server" ID="MyCheckBox" AutoPostBack="true" oncheckedchanged="MyCheckBox_CheckedChanged" />
<asp:HiddenField runat="server" id="DatabaseKeyHiddenField" Value='<%# Eval("DatabaseKey") %>' />
</td>
</tr>
</ItemTemplate>
protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox selectedCheckBox;
DataListItem selectedDataListItem;
HiddenField databaseKeyHiddenField;
string databaseKey;
// Cast the sender object to a CheckBox
selectedCheckBox = (CheckBox)sender;
// Walk up the tree one level so we get the container for both controls
selectedDataListItem = (DataListItem)selectedCheckBox.Parent;
// Get the HiddenField control ...
databaseKeyHiddenField = (HiddenField)selectedDataListItem.FindControl("DatabaseKeyHiddenField");
// ... and read the value
databaseKey = databaseKeyHiddenField.Value;
// Go off and do a database update based on the key we now have
...
}
It's a bit of a workaround rather than exactly what you want to do, but it works!
I have a asp:ListView control that I bind with a List<CustomObject>.
When Editing records in this ListView control, I can always get the Unique Id of record being edited by using:
int id = Convert.ToInt32(lstView1.DataKeys[e.NewEditIndex].Value);
Is it possible to get the whole object <CustomObject> that is being edited, using any of the ListView properties?
I just figured that out,
We can get the object being edited using following code-
protected void lstView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem objCurrentItem = (ListViewDataItem)e.Item;
**CustomObject obj = (CustomObject)objCurrentItem.DataItem;**
if (objCurrentItem.DisplayIndex == lstView1.EditIndex)
{
TextBox txtTitle = (TextBox)objCurrentItem.FindControl("txtTitle");
txtTitle.Text = obj.Title;
}
}
Here is the answer to your comment to my question:
Yes, the reason it is null in itemcommand and works fine in itemdatabound is that the location itemcommand is not correct for reading this value. You will always get the DataItem null in ItemCommand, no matter what you do. The reason lies in the control life cycle. The control gets initialized, created and then only does any other event related to the control can fire. During control creation the CreateControlHierarchy is called which then uses the DataBind event to create and databind the child controls. At that time the DataItem is live and is not null. Before that and after that it is always null, because its role lies only for that much time span.
By the way the DataItem you are looking at is the item from the related datasource that is being used to databind the listview. The datasource is used only during databinding, hence the DataItem is available only during Item Databound.
Hope this helps !
When you click on the edit for a given item in the listview the ItemCommand event gets fired. The arguments for that event tell that you can get the list item for which that event was fired. You will have to typecast that item properly to get the information you require. The itemcommand event looks like this
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
you have e.Item to use from the ListViewCommandEventArgs.
You item updating you don't have the object available for modifying. You only have the collection of the properties and their values in the new values and old values collections that you get from event arguments. I suppose you can edit the properties of the item over there. Its more or less similar to editing the object itself, because eventually these property values will get transferred to the object using reflection.
<asp:ListView runat="server" ID="list" OnItemCommand="listVideo_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="btDelVideo" runat="server" Text="Delete" OnClientClick="return confirm('Confirm delete ?');" CommandArgument='<%# Eval("KeyID") %>' CommandName="DELETE" />
<asp:LinkButton ID="btEditVideo" runat="server" Text="Edit" CommandArgument='<%# Eval("KeyID") %>' CommandName="EDIT" />
</ItemTemplate>
</asp:ListView>
protected void list_ItemCommand(object sender, ListViewCommandEventArgs e)
{
int videoId = (int)e.CommandArgument;
switch (e.CommandName)
{
case "DELETE":
//Implement Delete event
goto default;
case "EDIT":
//Implement Edit event
goto default;
default:
//Rebind listview
break;
}
}
}
I got a Gridview in an UpdatePanel with this EditTemplate:
<edititemtemplate>
<asp:textbox id="txtDistFrom" runat="server" text='<%# Bind("distFrom") %>' width="30" />
<asp:CustomValidator ID="valDistFrom" ValidateEmptyText="True" OnServerValidate="valDistFromTo_ServerValidate" ControlToValidate="txtDistFrom" Text="Missing" ToolTip="Invalid" Display="Dynamic" runat="server" />
</edititemtemplate>
And a simple Server-side function:
Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim cv As CustomValidator = CType(source, CustomValidator)
Dim gvr As GridViewRow = cv.NamingContainer
Dim tbV As UI.WebControls.TextBox = gvr.FindControl("txtDistFrom")
If tbV.Text <> "" Then
args.IsValid = False
cv.ErrorMessage = "inhalt ist " & tbV.Text
End If
End Sub
But when debugging this code the server-side function is not fired, whatever it does. It seems it has to do with the gridview, so I cannot access the control directly by its id. Any suggestions?
If you modify your VB to:
Protected Sub valDistFromTo_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim cv As CustomValidator = CType(source, CustomValidator)
If args.Value <> "" Then
args.IsValid = False
cv.ErrorMessage = "inhalt ist " & args.Value
End If
End Sub
It should work. Note that I'm using args.Value. I use CustomValidators and TextBox within EditTemplates with ControlToValidate set to the TextBox ID all the time and it works, you just can't get the TextBox object the way you're trying it. I think this is far less of a pain and much cleaner than messing around with RowUpdating Event as suggested in TGnat's answer.
In this case you can use a required field validator. Which should work just fine in a grid.
For server side validation I would move the custom validator outside the grid entirely and leave the ControlToValidate property blank. You can move your validation to the RowUpdating event of the grid and set any error messages on the custom validator. Rmember to set the validators IsValid property appropriately.
The problem is related to the ControlToValidate property, because the ID of your text box is not used in repeating elements like GridView, ListView and Repeater. In other words: You have stumbled upon a limitation in ASP.NET's engine.
I am not sure how to solve this problem, though. You might be able do it, by adding the CustomValidator programmatically by attaching a method to the GridView's OnRowBound method.
This article might provide an answer This article might provide an answer: Integrating Asp.Net Validation Controls with GridView at run-time.
I also tend to think that ControlToValidate is the problem. .NET changes the ID of that control at runtime and the custom validator probably isn't picking it up.
I would try adding the customvalidator on RowCreated or RowDatabound using the FindControl()
I had the same problem. When I explicitly set this property in my customvalidator, the server side code fired:
EnableClientScript="false"
I have an AutoCompleteExtender AjaxControlToolkit control inside a repeater and need to get a name and a value from the web service. The auto complete field needs to store the name and there is a hidden field that needs to store the value. When trying to do this outside of a repeater I normally have the event OnClientItemSelected call a javascript function similiar to
function GetItemId(source, eventArgs)
{
document.getElementById('<%= ddItemId.ClientID %>').value = eventArgs.get_value();
}
However since the value needs to be stored in a control in a repeater I need some other way for the javascript function to "get at" the component to store the value.
I've got some JavaScript that might help you. My ASP.Net AutoComplete extender is not in a repeater, but I've modified that code to detect the ID of the TextBox you are going to write the erturned ID to, it should work (but I haven't tested it all the way through to post back).
Use the value from 'source' parameter in the client side ItemSelected method. That is the ID of the calling AutoComplete extender. Just make sure that you assign an ID the hidden TextBox in the Repeater Item that is similar to the ID of the extender.
Something like this:
<asp:Repeater ID="RepeaterCompareItems" runat="server">
<ItemTemplate>
<ajaxToolkit:AutoCompleteExtender runat="server"
ID="ACE_Item"
TargetControlID="ACE_Item_Input"
...other properties...
OnClientItemSelected="ACEUpdate_RepeaterItems" />
<asp:TextBox ID="ACE_Item_Input" runat="server" />
<asp:TextBox ID="ACE_Item_IDValue" runat="server" style="display: none;" />
</ItemTemplate>
</asp:Repeater>
Then the JS method would look like this:
function ACEUpdate_CustomerEmail(source, eventArgs) {
UpdateTextBox = document.getElementById(source.get_id() + '_IDValue');
//alert('debug = ' + UserIDTextBox);
UpdateTextBox.value = eventArgs.get_value();
//alert('customer id = ' + UpdateTextBox.value);
}
There are extra alert method calls that you can uncomment for testing and remove for production. In a simple and incomplete test page, I got IDs that looked like this: RepeaterCompareItems_ctl06_ACE_Item_IDValue (for the text box to store the value) and RepeaterCompareItems_ctl07_ACE_Item (for the AC Extender) - yours may be a little different, but it looks practical.
Good Luck.
If I understand the problem correctly, you should be able to do what you normally do, but instead of embeding the ClientId, use the 'source' argument. That should allow you to get access to the control you want to update.
Since you are using a Repeater I suggest wiring the OnItemDataBound function...
<asp:Repeater id="rptResults" OnItemDataBound="FormatResults" runat="server">
<ItemTemplate>
<asp:PlaceHolder id="phResults" runat="server" />
</ItemTemplate>
</asp:Repeater>
Then in the code behind use something like
`Private Sub FormatResults(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
Dim dr As DataRow = CType(CType(e.Item.DataItem, DataRowView).Row, DataRow) 'gives you access to all the data being bound to the row ex. dr("ID").ToString
Dim ph As PlaceHolder = CType(e.Item.FindControl("phResults"), PlaceHolder)
' programmatically create AutoCompleteExtender && set properties
' programmatically create button that fires desired JavaScript
' use "ph.Controls.Add(ctrl) to add controls to PlaceHolder
End Sub`
Voila