I am new to ASP.Net DataGrid, I have a grid which has linkbutton on each row!!
On Itembound event I am getting the object which I am binding to the grid!! On button click, I need to send this object data to webservice!!
How do I get object on button click? CommandArgument is of string type, is there a way to pass object to CommandArgument?
No, there is no such way. This object basically exists only during the data binding period. And button click handling happens on a next request, so this object does not even exist anymore.
Your options are:
Best and most common. Pass the object id as an argument, and in the button click handler do a query (to the database, I presume) to get the object by id.
No so common and weird. Implement serialization and deserialization of your object. This way you can pass the whole serialized object string as an argument, and deserialize it on button click.
Related
I am building a Xamarin Forms Application and using XAML that is databound to a ViewModel that contains the model that is stored in Realm datastore.
When Two Way databinding it turned on the view will throw the error
Cannot set values outside transaction
Which includes
InnerException {Realms.RealmOutsideTransactionException: Cannot set
values outside transaction at
Realms.RealmObje…} Realms.RealmOutsideTransactionException
I'm not sure why the data binding is wanting to set the value back on the RealmObject when its loading the View that shows the Entry object that its databound to.
By Default the Mode=TwoWay. I have to change it to Mode=OneWay to get the view to load the databound data.
Is this a bug?
According to this article, this should work.
https://blog.xamarin.com/cross-platform-development-with-xamarin-forms-and-realm/
When you have two-way binding then the Realm needs to have an active Transaction as shown in the QuickJournal sample's JournalEntryDetailsPage.
I'm not sure why the data binding is wanting to set the value back on
the RealmObject when its loading the View that shows the Entry object
that its databound to.
This puzzles me too. It is a quirk of the way Xamarin Forms implement two-way bindings. If a string property is blank, then it doesn't trigger the setter. However, if there is a value in the RealmObject's property, it seems to fire a Xamarin.Forms.Platform.IOS.EntryRender:OnEditingChanged and attempt to set the same value back again. I consider this a bug in Xamarin Forms. It should not be propagating the unchanged value back as far as the viewmodel.
Assuming I have access to the object that would be databound, and the settings for the databinding DataField, FormatString, etc...
How would I programattically get the resulting string value of a databinding without actually databinding to a control?
Context: This is in the overloaded InitializeCell event of a Telerik (Telerik.Web.UI) GridDropDownColumn that I am inheriting. I want to cache the resulting string value, but I need the value before the normal databinding event fires.
Just do a separate database query early in the page life cycle (pre-init) and cache the value manually...
While going through some of the code, i found there is written like this:
ListViewDataItem dataItem = (ListViewDataItem)(((ImageButton)sender).Parent.Parent);
int noteId = Convert.ToInt32(((HiddenField)dataItem.FindControl("hidNoteId")).Value);
Please explain the meaning of these two line.
There is a control (typed to hidden field) that is being used to track the identity of item within a ListView (http://msdn.microsoft.com/en-us/library/bb398790.aspx)
And it's code that shouldn't be written, IMO. It's inefficient and brittle. Also, the identity of the item is stored in plain text in the source of the page.
This code appears to be getting the identity of a databound item when a button is clicked. A better way would be to simply set the command arguments of the button, like so:
<asp:ImageButton runat="server" CommandArgument="[Binding Expression]" />
In the event handler for the button, the CommandArgument can be retrieved and converted to an Int32. With this methodology, you shouldn't even need that hidden field.
Firstly, both the lines seem a bit crazy in their use of brackets and casting. There's probably a better way to do what they're doing, but without more code it's difficult to offer any suggestions.
Anyway, the first line is going to be in an event handler for an event raised by an ImageButton. The event handler method will have a sender argument; this is being cast to an ImageButton. Then Parent.Parent is called on this ImageButton; this will give the object two levels up in the control heirarchy. The developer is obviously quite sure this is a ListViewDataItem, so it's being cast to one of these. Therefore the variable dataItem now contains an instance of a ListViewDataItem.
In the second line, the FindControl method is being called on dataItem. There is presumably a control under this ListViewDataItem with an ID of "hidNoteId". FindControl returns a Control; but the developer knows this control is actually a HiddenField, so there's a cast. The Value property of this hidden field is then passed into the Convert.ToInt32 method to give an integer - this is then stored in the noteId variable.
So at the end of it all, there's a ListView, in which each data item contains a hidden field that contains the value of some ID. This code is getting the ID.
dataItem is a bound control, which when clicked is being used to represent a placeholder. This is used to identify which line is being executed within in the control.
By looking for a parent's parent of this control, it's giving the code a starting point to navigate to the correct row to extract a value from.
After the row is found, the noteId is being assigned a hidden value and cast into an integer.
When items are bound to a grid/repeater, with a button as the post-back control, a way is needed to identify which row is being executed. All the code is doing above is navigating the control and extracting a value from a set of values within the row.
I have a list that I need to bind to a List I get from an API. The list looks like this:
struct DataItem { int level; string name; Guid key };
List<DataItem> myList = API.GetList();
ListView1.DataSource = myList;
ListView1.DataBind();
All this works fine for display. However, the table must edit the level value. I am unsure how to make that happen. I have tried event handlers on the listView, but they are never called. I have tried a text box for the level field (with both Bind and Eval) and an event handler OnTextChanged, but the event handler is never called. (I have tried with various combiniations of AutoPostBack and ViewState enabled.)
How can I programatically edit this data structure?
Two way data binding you are trying to implement here won't work like this - List doesn't implement INotifyPropertyChanged (someone correct me if I'm wrong).
You may consider using a plain old DataTable which can be two-way-bound out-of-the-box. If performance is not a highly critical issue, converting your List to a DataTable (and back, depending on what you want to do with the modified data) is simple enough, rather than struggling with custom implementations of list types.
On page1.aspx I have a button tied to a buttonOnClick event in my server side.
Clicking the button runs buttonOnClick method which generates an object (local to buttonOnclick method) with data I would like to use in the construction of a new page.
Basically, I want to open a new (different) page (page2.aspx) in a new tab, but as this page loads, I want it to render based on the contents of the object generated during buttonOnClick.
How one might go about this properly? Up until now I have been passing URL arguments to the new popup (page2.aspx) and I have it build the object, but I would rather do this properly and generate it buttonOnclick and then load page2.aspx in a popup based on what is the object built in the buttonOnclick method.
Store the object in your Session on the first page and then retrieve it on your second page.
On first page:
Session["myobject"] = myObject;
On second page:
MyObject object = (MyObject) Session["myobject"];
The easiest thing to do would be to store the object in the user's Session.
Session["myObject"] = myObject;
To get it out, you will need to cast it back to the type of MyObject, since the Session cache is simply a hashtable of objects.
if (Session["myObject"] != null)
{
MyObject myobj = (MyObject)Session["myObject"];
}
You can access it from both pages this way. Generally, if multiple pages are going to be accessing the same object in Session, I usually store the name of the session key in a centralized location so that it only has to be updated once should you ever decide to change it. Usually I add a resource file to my application and put it in the Properties folder, so I can use it this way:
string key = WebApplication1.Properties.MyObjectSessionKey;
Session[key] = myObject;
Use session state or store the object in a database and pass the id to the second page. The object will need to be serializable for this.