I have this type of code in aspx but wish to generate it in code behind. What would be the equivalent?
<uc1:editformcontrol ID="EditFormControl1" runat="server" CategoryID=' <%#Bind("CategoryID") %>' />
The question pertains mainly to the binding
It's difficult to say without knowing your data model. What kind of data source are you using? You could try adding this line to the Page_Load method:
int categoryId;
categoryId = WHEREEVER YOU'RE STORING THIS VALUE...
EditFormControl1.CategoryID = categoryId;
Usually, you override the OnDataBound event on your control and bind the data there.
See here one example.
Related
I am using .NET 4.5 and want to add paging to my ListView which is using model binding.
I have Googled and found the answer:
http://dotnet.learningtree.com/2012/11/30/efficient-paging-with-model-binding-in-web-forms/
However as the code snippets are done as images where the author says "When I add a new GetData() method, it creates a stub in the code-behind with an IQueryable..." I cannot see where that is added.
Can anyone point me in the right direction? All I am trying to do is add paging to my ListView that is model bound in an efficient fashion.
Inside you <asp:ListView... tag type SelectMethod and you will get a method stub generator helper. If you press TAB it will create a method ListViewId_GetData in your code behind. The whole picture:
<asp:ListView SelectMethod="ListView1_GetData" ID="ListView1" runat="server">
</asp:ListView>
Code-behind:
public IQueryable ListView1_GetData()
{
return null;
}
I have a Repeater with a list of Customers. Against each customer there is a delete link button. As part of the linkbutton I want to pass the Customer object to the Command Arguement as follows (where Container.DataItem is the customer object):
<asp:LinkButton ID="lnkDelete"
OnClientClick="return confirmDelete();"
OnClick="Customer_OnDelete"
CommandArgument="<%# Container.DataItem %>"
CommandName="Delete"
runat="server"></asp:LinkButton>
When I do this:
var button = (((LinkButton) sender));
var customer= button.CommandArgument;
button.CommandArguement is a string. I need all the object properties as we are using Nhibernate so everything needs to be set, the ID of the deleted record is not enough. I have seen examples online regarding passing a comma seperated list of values into the command arguement but want to avoid doing that. Is this possible?
Any ideas?
Thanks
In my opinion the best way for this case is:
Get the ID from CommandArgument
Get the Customer by ID
Delete the Customer Entity
Use the Repeater event OnItemCommand. This event contains RepeaterCommandEventArgs. You cant get the CommandArgument this way:
protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int customerID= Convert.ToInt32(e.CommandArgument.ToString());
}
At your asp:LinkButton tag use:
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'
The issue you are running into here is that you repeater has to get translated into HTML. Therefore you are constrained to the limits of what is allowed by the specification in an element attribute.
On the server side CommandArgument will always be a string, so you cannot do what you want as you have it coded.
Now... there are several hacks you could implement to get around this, like the aforementioned CSV, or you could use binary serialization and Base64 encode the result. However, these are all terrible solutions!
What you need is to re-think how you are doing this. I promise there is an easier way.
I feel a little embarassed posting two questions relating to the same problem, but the first one ended up answering a question that I believe is unrelated to the solution so I'm leaving it up and outlining what I'm trying to accomplish with the hopes that someone can help out a .Net noob.
What I need to be able to do is create a field in my gridview that contains a link that passes two variables. One is pulled from within the gridviews datasource and the other needs to be pulled from a textbox control outside the gridview.
From what I've read so far you cannot use a hyperlinkfield for this as the datanavigateurlfields cannot be set to pull from anything but the gridview's data source.
What I attempted to do was create a template field where in the itemtemplate I called:
Test
That comes back with an error like this:
DataBinding: 'System.Data.DataRowView' does not contain a property with the value 'TestData'
Any clues to make this happen would be appreciated, like I said I'm pretty new to .Net so please be gentle. I tried to do my homework before posting this.
How about putting a hyperlink server control in your GridView template column as below.
<asp:Hyperlink id="hyperlink" runat="server" onDataBinding="hyperlink_DataBinding" text="Click ME" />
Then in your code behind add this data binding event for the hyperlink.
protected void hyperlink_DataBinding(object sender, EventArgs e) {
HyperLink link = (HyperLink) sender;
string param1 = Eval("field").ToString();
string param2 = ExampleList.SelectedItem.Value;
link.NavigateUrl = "example.aspx?e=" + param1 + "&f=" + param2;
}
I need to expose some input fields based on what properties I find for particular types in an assembly.
I'm not sure how common an approach like that is. Perhaps there are easier ways. Maybe on the client side instead of server.
If anyone knows of a good way of doing something like this, I would appreciate the help.
Create input controls accordingly and simple add control to some div container? I'm not sure if it would be more complex than that.
I'll need to somehow add css classes to the controls as I build them so they get placed nicely; that might get tricky.
This all sounds like standard asp.net development. Any good tutorial should be able to help you. For the asp server controls, you use the CssClass property to set the class for the control.
Here is the asp.net tutorial from the W3C Schools.
I assume you will use reflection to figure out what properties entity has, then you would based on the type of the property create an input field. You would have to dynamically create control to handle input in code behind. Make sure you give that control and id. You will have to recreate these controls on the post back. This looks to me like dynamic property editor. There might be some free ones, google for it.
If the UI doesn't have to be completely dynamic you could include all the controls in the markup with any optional ones set to Visible="false". Then, selectively enable the appropriate controls in your code-behind. For example:
Default.aspx
<asp:Button ID="EvenButton" runat="server" Text="Even" Visible="false" />
<asp:Button ID="OddButton" runat="server" Text="Odd" Visible="false" />
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
String msg = "A message to count";
if (msg.Length % 2 == 0)
{
// Enable the Even Button
EvenButton.Visible = true;
}
else
{
OddButton.Visible = true;
}
}
The advantage of this method is that you can lay things out with the appropriate CSS easily in the markup. If, on the other hand, your UI is much more dynamic than this, you'll probably have to resort to dynamically creating controls in the code-behind and adding them to the page via calls to Controls.Add(). This way, however, is harder to layout. And you have to deal with things like re-wiring any event handlers on each postback.
Hope that helps.
I ended up leveraging jQuery.
I laid out a simple markup with the basic layout I would need.
For creating controls dynamically, I did it all in javascript using jQuery methods.
This of course requires that you return some data set to the UI intelligently enough to render it.
I have the following code in one of my aspx pages:
<% foreach (Dependency dep in this.Common.GetDependencies(this.Request.QueryString["Name"]))
{ %>
<ctl:DependencyEditor DependencyKey='<%= dep.Key %>' runat="server" />
<% } %>
When I run it, I get the following error: Parser Error Message: Cannot create an object of type 'System.Guid' from its string representation '<%= dep.Key %>' for the 'DependencyKey' property.
Is there any way that I can create a control and pass in a Guid in the aspx page? I'd really hate to have to loop through and create these controls in the code behind just because of that...
NOTE: The Key property on the Dependency object is a Guid.
The key property of the Dependency object may be a Guid, but is the DependencyKey Property of the DependencyEditor a Guid too? If not it should be, otherwise the correct TypeConverter won't be invoked upon assignment.
If I'm not mistaken, you could also use dep.Key.ToString() also.
is your control taking the value and assuming its a GUID? Have you tried instantiating a GUID with the value? Looks like this is a cast problem
ok, here's the deal...try using the # symbol instead of the = symbol. I replicated your problem and that gets past the compile issue.
It should look like this "<%# dep.Key %>"
good luck!
Assuming dep.Key is a string representation of a guid... and DependencyKey is a property of type Guid
<ctl:DependencyEditor DependencyKey="<%= new Guid(dep.Key) %>" runat="server" />