I need to create a group of DropDownLists to show and allow change of a property group for an item.
I have the following code on my ASP page.
<asp:Repeater runat="server" ID="repeaterProperties">
<ItemTemplate>
<p><asp:DropDownList runat="server" ID="ddProperty" OnInit="ddProperty_OnInit" /><p>
</ItemTemplate>
</asp:Repeater>
The ddProperty_OnInit populates the DropDownList with all the possible values with a database query.
How can I set the selected value of each created DropDownList according to the Repeater's source data?
Let's say, for example, that we have the possible property values of A, B and C.
If the database output for the Repeater contains two of those values, A and B, the Repeater outputs two DropDownLists, both with all 3 values availabla and the first one with A as selected value and the second one with B as selected value.
Edit:
It seems that adding OnItemDataBound="repeater_ItemDataBound" to the Repeater and selecting the appropriate value in there is not the way to go in my case. This is because I also need to save the possibly changed values to a database.
The ItemDataBound event of the Repeater is fired before the OnClick event on a Button and changes the selected values to their old values before the new selections can be saved.
Any suggestion on how to work around this?
Current code:
<asp:Repeater runat="server" ID="repeaterJako" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<asp:DropDownList id="ddJako" runat="server" OnInit="ddJako_OnInit">
</asp:DropDownList><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" id="updateButton" Text="Save" OnClick="update_OnClick" />
In the code-behind, ddJako_OnInit populates the drop down list with all the possible choises, while the repeater_ItemDataBound uses the method suggested by Bryan Parker to select the proper value.
Maybe I'm misunderstanding something about your question... but it seems like this is exactly what OnItemDataBound is for. :)
Use FindControl to get a reference to your DropDownList in the event handler. Also check to make sure the item is not the header/footer. The example from MSDN does both these things:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx
In regard the problem I specified in my edit, the time of the DataBind plays an important role. I used to do the databinding in the Page_Init event, which caused the repeater_ItemDataBound event to be fired before the button_OnClick event.
The solution was to move the databinding to the Page_PreRender event.
The population of the DropDownList with all the choises is still done in its OnInit event.
Related
I have a DataGridView with ASP.net hyperlink fields inside of it. What I'm trying to do is not display a certain hyperlink based on a condition. I have SQL that determines if the hyperlink should be hidden or not, but I'm having trouble getting this to work in the hyperlinks.
I tried <asp:HyperLinkField....Visible="<%= Eval(Condition) %>" /> where Condition is True or False from my SQL query.
Which of course throws the error Cannot create an object of type 'System.Boolean' from its string representation '<%= Eval(Condition)%>' for the 'Visible' property.
So I understand this from Why will <%= %> expressions as property values on a server-controls lead to a compile errors? and other similar questions.
My question now is: what is the workaround? How can I get the hyperlink to display or not based on my condition?
You cannot change HyperLinkField's Visible on runtime, because it does not have a DataBinding event.
Instead, you should not be changing HyperLinkField's Visible value. The problem is the rest of the column will not be aligned properly if you hide a single cell.
Instead, you want to use TemplateField and HyperLink, and hide a link only (leave the table cell by itself). For example,
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="ConditionHyperLink" runat="server"
Visible='<%# Convert.ToBoolean(Eval("Condition")) %>'
Text="Link" />
</ItemTemplate>
</asp:TemplateField>
FYI: Your syntax is not correct; it should be ='<%# Eval("") %>'
My recommendation would be to handle it in the codebehind, most likely handling the RowCreated event, and setting the Visible property of the control there. There might be more context to your application that I'm missing, but that seems like the simplest way.
This is the event: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcreated(v=vs.110).aspx
Other events on the gridview in case that one doesn't meet your needs: https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview_events%28v=vs.110%29.aspx
I am using CreateUserWizard and added additional WizardStep for user additional details.
I am using one dropdownlist. value has been filled from DB in dropdownlist.
Now, when I will change the value in dropdownlist. I want to chekc/uncheck the one checkbox based on it's value in DB for selected dropdownlist value.
For that i want to call onselectedindexchanged event of dropdownlist.
How to fire onselectedindexchanged event?
Please help me to solve this problem?
Dont add onselectedindexchanged manually
All event for element are listed in event tab on right hand side
Your code should look like
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
In cs file
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Important Note
Remove the ViewStateMode="Disabled" in the CreateUserWizard control
Try this.
drag a dropdownlist control on your aspx page and add selectedindexchanged event, test it properly, now cut the control and paste it inside WizardStep.
I hope it works.
What is the trick to implement the following interface in ASP.NET?
I think the screenshot should explain everything.
Both dropDownLists work on SQL tables. [Categories],[Items] bound with ID_category PK/FK
Number of rows variable (by default 1 row with "Add" button)
Choosing category changes only the content of the dropDownList next to it and the content stays the same when other rows get added/deleted
"Add" button saves current setup and adds new row with either "Select value" or eventually 1st category/1st item selected
How to store previously selected categories/items and the dropDown filtering by category, while adding, changing, deleting items or some other PostBack on website?
What I have tried so far is to use Repeater with DataSet, however I have encountered several different problems:
Was unable to preserve the relations between previous dropDowns
DropDowns resetting one another
2nd dropDown losing filtering when adding new row
I can post some code but since it doesn't fully work, perhaps a totally different approach would be better. Basically I started with this tutorial. The repeater currently looks like:
<repeater>
<itemtemplate>
<dropDown DataSource="categoriesDS"
value='<%# DataBinder.Eval(Container.DataItem, "Category") %>'.../>
<dropDown DataSource="itemsDS"
value='<%# DataBinder.Eval(Container.DataItem, "Item") %>'.../>
<button CommandName='<%# DataBinder.Eval(Container.DataItem, "Button") %>'
Text='<%# DataBinder.Eval(Container.DataItem, "Button") %>' .../>
</itemtemplate>
</repeater>
Thanks in advance for any suggestions or solutions and I hope it'll be helpful for others.
Ajax Cascading DropDownlist
It is very simple to use.
I have a list view binded to a data source.
How can I enable sorting by column?
So when clicking the column title once it sorts by ascending, click it again it sorts by descending.
Is there anything in the wizard that can be setup or it all needs to be done programatically?
If you are using a data source control then sorting can be implemented in the ListView control without having to write a single line of code - it is all handled automatically by the ListView and its data source control. Of course, in more advanced scenarios we may need to manually sort the data or programmatically.
In the following example, ListView will be sorted by ProductName and UnitPrice. simply add two LinkButtons in the ListView's LayoutTemplate with appropriate CommandName and CommandArgument property values.
<asp:ListView ID="ProductList" runat="server" DataSourceID="ProductDataSource">
<LayoutTemplate>
<h3>Product Listing</h3>
<asp:LinkButton runat="server" ID="SortByName" CommandName="Sort"
CommandArgument="ProductName">Sort by Name</asp:LinkButton>
| <asp:LinkButton runat="server" ID="SortByPrice" CommandName="Sort"
CommandArgument="UnitPrice">Sort by Price</asp:LinkButton>
<blockquote>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</blockquote>
</LayoutTemplate>
...
</asp:ListView>
If a user clicks the "Sort by Price" link twice in a row, the first time the products will be sorted in ascending order (from cheapest to most expensive), but the second time the sort order will reverse.
That depends on the DataSource you are using. If the Data Source allows sorting, it can be set up in the way described here http://msdn.microsoft.com/en-us/library/sa88ktts.aspx . Otherwise you have to handle sorting yourself programatically. Usually that is even easier than the automatical approach.
I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created every time, even after postback. From there I moved the function to bind the ListView outside of the if (!Page.IsPostBack) conditional. Now I get the dynamic controls values but the static controls I have are set to their defaults. Here is a sample of what I am trying to accomplish:
For brevity, I left some obvious things out of this example.
<asp:ListView runat="server" ID="MyList" OnItemDataBound="MyList_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</LayoutTemplate>
<ItemTemplate>
<asp:PlaceHolder runat="server" ID="ProductPlaceHolder">
<asp:TextBox runat="server" ID="StaticField" Text="DefaultText" />
<asp:PlaceHolder ID="DynamicItems" runat="server" />
</asp:PlaceHolder>
</ItemTemplate>
</asp:ListView>
and here is the codebehind:
protected void MyList_ItemDataBound(object sender, System.Web.UI.WebControls.ListViewItemEventArgs e) {
PlaceHolder DynamicItems = (PlaceHolder)e.Item.FindControl("DynamicItems");
DynamicItems.Controls.Add(textbox);
}
So, like I said, if I only databind when Page != PostBack then I cant find my dynamic controls on postback. If I bind every time the page loads then my static fields get set to their default text.
Try moving the data binding of the ListView into the OnInit() event.
Very similar question (instead of populating a ListView the guy is generating a set of buttons). Briefly, you'll find that you have to store the items in the list in your Viestate - than fish it out on Postback and re-populate the list.
Note that this solutions implies dropping data-binding (which you might not wanna do for others reasons).
Hope it helps.