Update listview on dropdownlist change - using causes validations and commandName - asp.net

I have a list of ListView Items of type Place{Id,Label}. In the last column, I have a DropDownList that helps me to select/set a new Label with the selected value.
Actualy I'm using a DropDownList and a Button
<asp:DropDownList CssClass="edit_place_combobox"
ID="CollectiveBanksDropDownList3" runat="server" AutoPostBack="False"
DataSourceID="BanksObjectDataSource1" DataTextField="PlaceLabel"
DataValueField="PlaceID" Height="22px" Width="217px" />
<asp:Button runat="server" CausesValidation="true" CommandName="updatelabel" />
This works but I want to delete the button and trigger update when the DropDownList changes.
Is this possible in webform ?

Drop down lists have "OnSelectedIndexChanged" which allows you to call a function in your backend code.
If your listview is wrapped with an update panel. All you need to do is call UpdatePanelName.Update() in the method you declared for "OnSelectedIndexChanged" in your dropdown list.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.onselectedindexchanged%28v=vs.110%29.aspx

Related

How to filter second GridViewDataComboBoxColumn dropdown item from selection of first GridViewDataComboBoxColumn inside ASPxGridView

There are two GridViewDataComboBoxColumn columns inside my ASPxGridView, Need to know that Selection of first GridViewDataComboBoxColumn dropdown item should filter the second GridViewDataComboBoxColumn item before pupulating data.
For example, If I choose 'India' from first GridViewDataComboBoxColumn then only India's cities must be populated in another GridViewDataComboBoxColumn dropdown using SqlDataSource.
My sample code:
<dx:ASPxGridView runat="server" ClientInstanceName="grid" AutoGenerateColumns="False" ID="ASPxGridView1" DataSourceID="DSEXTagList" OnCellEditorInitialize="GridViewEXTags_CellEditorInitialize" KeyFieldName="ID;" OnRowInserting="GridViewEXTags_RowInserting" Width="100%">
<Columns>
<dx:GridViewDataComboBoxColumn Caption="System" FieldName="System" VisibleIndex="1">
<PropertiesComboBox EnableCallbackMode="true" DataSourceID="DsSystem" TextField="SystemWithDescription" ValueField="System" NullDisplayText="Select System">
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataComboBoxColumn Caption="Sub System" FieldName="SubSystem" VisibleIndex="2">
<PropertiesComboBox DataSourceID="DsSubSystem" TextField="SubSystemWithDescription" ValueField="SubSystem" NullDisplayText="Select Sub System">
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
</Columns>
</dx:ASPxGridView>
Actually I'm new in Asp.net DevExpress technologies. Please somebody let me know what should I do in this case and fire event on selection of GridViewDataComboBoxColumn item in code side.
Check out this demo to see (play) this approach in action. See the demo's description for more information:
For the "parent" combobox column: handle the client-side "SelectedIndexChanged" event,
Call the "child: combobox column/editor callback via the client-side "PerformCallback" method,
Subscribe and Handle the "child" comobobox "Callback" event and reload its data.

Dropdownlist bind with parameterized datasource don't keep value after postback

There is a drop down list bind with datasource -> Called This DropDownList A
This datasource get another parameter from another dropdownlist -> Called This DropDownList B
Everytime DropDownList B Change the selected value, Datasource of Dropdownlist A will receive the parameter and change dropdownlist A.
Whenever DropDownlist change its index, it will cause postback and the selected value will be reset to the first index (default).
After Switching DropDownList A to use a datasource with no parameter. This problems is not occur.
After DropDownList A has changed its value and postback, the recenly changed value still there.
I have tried using update panel but it still is not work either.
This is DropDownList B, Add On Type
<asp:DropDownList ID="ddlAddOnType" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" ClientIDMode="Static" CssClass="controlOrdinary"
DataSourceID="odsAddOnType" DataTextField="AddOnTypeName"
DataValueField="AddOnTypeId"
onselectedindexchanged="ddlAddOnType_SelectedIndexChanged" Width="208px">
<asp:ListItem Value="0">--Please Select--</asp:ListItem>
</asp:DropDownList>
This is datasource for dropDownlist A
<asp:ObjectDataSource ID="odsAddon" runat="server"
SelectMethod="SelectAddOnThatIsNotInCurrentPromotionWithAddOnByAddOnTypeId"
TypeName="BackOfficeLib.SupportAdmin.PromotionBinder">
<SelectParameters>
<asp:ControlParameter ControlID="ddlAddOnType" Name="pIntAddOnTypeId"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
This is dropdownlist A, when this one change the value, the page being post back suddenly and the index is reseted.
<asp:DropDownList ID="ddlAddOn" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" ClientIDMode="Static" CssClass="controlOrdinary"
DataTextField="AddOnName" DataValueField="AddOnId"
onselectedindexchanged="ddlAddOn_SelectedIndexChanged" Width="208px">
<asp:ListItem Value="0">--Please Select--</asp:ListItem>
</asp:DropDownList>
The solution i use is keeping value in hidden field by javascript, using onchange event ( change the first drop down list)
This asp.net ajax control provide some help.
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx

Changing WebControl ID Inside of a Repeater

<ItemTemplate>
<asp:Label runat="server"><%#DataBinder.Eval(Container.DataItem, "Question")%></asp:Label>
<asp:DropDownList runat="server" id="<%#DataBinder.Eval(Container.DataItem, "QuestionID")%>">>
<asp:ListItem value="1" text="Yes" />
<asp:ListItem value="0" text="No" />
</asp:DropDownList>
<ItemTemplate>
This is roughly what I'm trying to do. Obviously, the implementation is faulty, but I can't find any information on how I'd go about this in practice. Any help is appreciated.
Edit: What I'm trying to do exactly is add a DropDownList for each item in this Repeater, and upon submission of the form, use the ID's of each Yes/No answer to input into a database. The SqlDataReader that I'm using has two fields: The question content and the questionID.
I think you'd be better off using the built in support for IDs inside a Repeater. If the goal is to assign it an ID to make it easy to find the proper control after the data has been bound you might try something like this:
<asp:Repeater ID="Repeater1" runat="server>
<ItemTemplate>
<asp:Label ID="QuestionID" Visible="False" Runat="server"><%#DataBinder.Eval(Container.DataItem, "FieldContent")%></asp:Label>
<asp:DropDownList ID="MyDropDownList" Runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:Repeater>
Then, in your code you can loop through the items in the Repeater until you find the label you're looking for:
foreach (RepeaterItem curItem in Repeater1.Items)
{
// Due to the way a Repeater works, these two controls are linked together. The questionID
// label that is found is in the same RepeaterItem as the DropDownList (and any other controls
// you might find using curRow.FindControl)
var questionID = curRow.FindControl("QuestionID") as Label;
var myDropDownList = curRow.FindControl("MyDropDownList") as DropDownList;
}
A Repeater basically consists of a collection of RepeaterItems. The RepeaterItems are specified using the ItemTemplate tag. Each RepeaterItem has its own set of controls that are, by the very nature of a Repeater, associated with each other.
Say you're pulling the Repeater data from a database. Each Repeater item represents data from an individual row in the query results. So if you assign the QuestionID to a label and the QuestionName to a DropDownList, the ID in the label would match up with the name in drop down.
Could you remove the control from the markup file, and hook the repeater's onItemDataBound event. In that event, you should be able to create the dropdown control "manually", assigning whatever ID you want.

How to find checked RadioButton inside Repeater Item?

I have a Repeater control on ASPX-page defined like this:
<asp:Repeater ID="answerVariantRepeater" runat="server"
onitemdatabound="answerVariantRepeater_ItemDataBound">
<ItemTemplate>
<asp:RadioButton ID="answerVariantRadioButton" runat="server"
GroupName="answerVariants"
Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"/>
</ItemTemplate>
</asp:Repeater>
To allow select only one radio button in time I have used a trick form this article.
But now when form is submitted I want to determine which radio button is checked.
I could do this:
RadioButton checkedButton = null;
foreach (RepeaterItem item in answerVariantRepeater.Items)
{
RadioButton control=(RadioButton)item.FindControl("answerVariantRadioButton");
if (control.Checked)
{
checkedButton = control;
break;
}
}
but hope it could be done somehow simplier (maybe via LINQ to objects).
You could always use Request.Form to get the submitted radio button:
var value = Request.Form["answerVariants"];
I think the submitted value defaults to the id of the <asp:RadioButton /> that was selected, but you can always add a value attribute - even though it's not officially an <asp:RadioButton /> property - and this will then be the submitted value:
<asp:RadioButton ID="answerVariantRadioButton" runat="server"
GroupName="answerVariants"
Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"
value='<%# DataBinder.Eval(Container.DataItem, "SomethingToUseAsTheValue")%>' />
Since You are using javascript already to handle the radio button click event on the client, you could update a hidden field with the selected value at the same time.
Your server code would then just access the selected value from the hidden field.
I'm pretty sure that the only thing you could use LINQ to Objects for here would be to take the conditions from within the foreach loop and move them to a where clause.
RadioButton checked =
(from item in answerVariantRepeater.Items
let radioButton = (RadioButton)item.FindControl("answerVariantRadioButton")
where radioButton.Checked
select radioButton).FirstOrDefault();

Formview Dropdown 2 depends on Dropdown 1

I have a form view, in the edit template I have two drop downs.
Drop down 1 is explicitly set with a list of allowed values. It is also set to autopostback.
Drop down 2 is databound to an objectdatasource, this objectdatasource uses the first dropdown as one of it's parameters. (The idea is that drop down 1 limits what is shown in drop down 2)
On the first view of the edit template for an item it works fine. But if drop down 1 has a different item selected it post back and generates an error
Databinding methods such as Eval(),
XPath(), and Bind() can only be used
in the context of a databound control.
Here is the drop down list #2:
<asp:DropDownList ID="ProjectList" runat="server" SelectedValue='<%# Bind("ConnectToProject_ID","{0:D}") %>' DataSourceID="MasterProjectsDataSource2" DataTextField="Name" DataValueField="ID" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="{No Master Project}" Selected="True" />
</asp:DropDownList>
And here is the MasterProjectDataSource2:
<asp:ObjectDataSource ID="MasterProjectsDataSource2" runat="server"
SelectMethod="GetMasterProjectList" TypeName="WebWorxData.Project" >
<SelectParameters>
<asp:ControlParameter ControlID="RPMTypeList" Name="RPMType_ID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Any help on how to get this to work would be greatly appriciated.
I had a similar problem with bound dropdownlists in a FormView. I worked around it by setting the selected value manually in the formview's "OnDataBound".
(don't know where you get ConnectToProject_ID from)
FormView fv = (FormView)sender;
DropDownList ddl = (DropDownList)fv.FindControl("ProjectList");
ddl.SelectedValue = String.Format("{0:D}", ConnectToProject_ID);
When you ready to save, use the "OnItemInserting" event:
FormView fv = (FormView)sender;
DropDownList ddl = (DropDownList)fv.FindControl("ProjectList");
e.Values["ConnectToProject_ID"] = ddl.SelectedValue;
or "OnItemUpdating"
When you ready to save, use the "OnItemInserting" event:
FormView fv = (FormView)sender;
DropDownList ddl = (DropDownList)fv.FindControl("ProjectList");
e.NewValues["ConnectToProject_ID"] = ddl.SelectedValue;
Sounds like the controls aren't being databound properly after the postback.
Are you databinding the first dropdown in the page or in the codebehind?
If codebehind, are you doing it in on_init or on_load every time?
There might be an issue of the SelectedValue of the second drop down being set to a non-existent item after the postback.
Unless your 2nd dropdown is in a databound control (say, a Repeater) - I'm not sure what you're trying to bind SelectedValue to. Apparently, neither is .NET - since that's probably where the error is occurring.
Where's Connect_ToProjectId supposed to come from?

Resources