We are using ASP.NET for one of our projects. However, when we are trying to read the SelectedItem or SelectedValue property of the DropDownList we are using in the callback function of a Link click, we are not getting the correct SelectedItem.
<FooterTemplate>
<asp:DropDownList ID="cmbTesters" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID"></asp:DropDownList>
</FooterTemplate>
This is the DropDownList in the aspx file. The drop down is present within a GridView's Footer Row. We are invoking the following set of code on clicking a link.
if (int.TryParse(((DropDownList)dgCreateCPRVerificationResponse.FooterRow.FindControl("cmbTesters")).SelectedValue, out TesterID))
{
TesterID = int.Parse(((DropDownList)dgCreateCPRVerificationResponse.FooterRow.FindControl("cmbTesters")).SelectedValue);
}
The problem we are facing is that whatever value we choose, the SelectedValue is always of the first item in the list. We are using REST based URL defined in the global.asax file. Also note that this is not built on any framework.
Please help as soon as possible
Make sure to put the binding methods of the dropdownlist and the gridview inside if (!IsPostBack)
Related
there. I have a gridview with a column of check boxes which was working when it was a small test project but when adding it to a page on my teams project it stopped firing the checkedChanged event. The check mark still appears or disappears but nothing fires. The only major difference is I was using an sqlDataSource object at first but for the new project i had to bind it to a database in the behind code.
Here's my html:
<asp:TemplateField HeaderText="Create Incident">
<ItemTemplate>
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
and some simple behindcode:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Console.WriteLine("clicked!");
}
set AutoPostBack to True to enable post back
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
I found the solution. I added the conditional the block if(!page.isPostBack) around the stuff in my page load event.
Kept searching for about an hour till I found out that the actual reason why the OnCheckedChanged event was not firing on my page was due to duplication of names. Duh
An input control had a name 'submit' and there also exists a method in my JavaScript that is called submit(). The JavaScript interpreter was confused between the name of the control and a function, every time it needed to fire the event. Changed the name of the control and everything went back to working perfectly.
Im trying to do bind a dropdown list to a details view but keep getting an error about the dropdown list ID field:
<asp:TemplateField HeaderText="Approval">
<ItemTemplate>
<asp:DropDownList ID="Approved" runat="server" DataValueField="Approved" SelectedValue='<%#Bind("Approved") %>'>
<asp:ListItem Text="Approved" Value="Approved" />
<asp:ListItem Text="Denied" Value="Denied"/>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
The error message as follows:
'Approved' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
What is the proper way to attach the value of the dropdownlist to my object so that it can be properly created in the database? Most of my searches keep telling me how to bind a dropdownlist to an object data source, but that is not what I need to do. This is a basic drop down list of 2 items that will never change.
This error happens because you are binding the SelectedValue of the DropDownList to the "Approved" field of your DetailsView's datasource, but the value it's trying to assign isn't one of the two you have listed ("Approved" and "Denied").
I see you have set DataValueField="Approved". Are you setting the Dropdown's Datasource in code-behind? Because that is not going to set your ListItems to the values from the "Approved" column in the DetailsView's datasource, it's setting them to whatever the datasource for the Dropdown is.
Depending on your logic, here are some possibilites:
Make sure your static items match the possible items returned in the "Approved" field of the DetailsView. Or,
Bind the DropDownList to a dataset that includes all the possible values from "Approved", and completely removed your static items. Or,
Set AppendDataBoundItems="True" in your DropDown and have both static and databound items
I have populated my DropDownLists with different columns of items from a database but I'm trying to make it so that the first item in the list has no value with text similar to "Select an Item"
For some reason Even though I add the item to the list and make it the selected item, It gets overridden by the items from the database...
How can I accomplish this?
UPDATE:
Everything is done from the .aspx page designer but here is the generated code-
<asp:DropDownList ID="ddlUnits1" runat="server" DataSourceID="UnitsEDS"
DataTextField="unitId" DataValueField="unitId">
<asp:ListItem Selected="True">Select Units</asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="UnitsEDS" runat="server"
ConnectionString="name=UnitsEntity"
DefaultContainerName="UnitsEntity" EnableFlattening="False" EntitySetName="spillunits">
</asp:EntityDataSource>
Use the ListControl.AppendDataBoundItems Property:
AppendDataBoundItems Documentation
From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items."
I have a custom control which contains a Repeater control. The Repeater has an ItemTemplate. Inside that item template I have a panel which is going to hide something based on "IsEditable" a boolean property of the custom control. What I would like to do is set the panel's visibility once before the Repeater is databound.
I know I could do an onItemDataBound event and use FindControl to get the panel but that seems a little excessive since it will always be either visible or not for all rows and I have no other actions that need to occur on databind.
Is there a way to find the control in the ItemTemplate before the Repeater is databound?
try this:
<ItemTemplate>
<asp:Panel Visible='<%# this.IsEditable %>' runat="server">
editableStuff
</asp:Panel>
</ItemTemplate>
I have a grid view that is data bound to a dataset. In a grid I have a column DefaultValue, which has three controls in it - a dropdownlist, a checkbox and a textbox. Depending on the data that is coming in it can switch to any of these controls. Everything is simple enough when we need just to display data - in gridview_prerender event I simply make one of the controls visible. The control is setup like following:
<asp:TemplateField HeaderText="Default Value" SortExpression="DefaultValue">
<ItemTemplate>
<asp:TextBox ID="txt_defaultValue_view" runat="server" Text='<%# Bind("DefaultValue") %>' Enabled ="false" />
<asp:DropDownList ID="ddl_defaultValue_view" runat="server" Enabled ="false" />
<asp:CheckBox ID="chk_defaultValue_view" runat="server" Enabled ="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_defaultValue_edit" runat="server" Text='<%# Bind("DefaultValue") %>'/>
<asp:DropDownList ID="ddl_defaultValue_edit" runat="server" />
<asp:CheckBox ID="chk_defaultValue_edit" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
My problem starts when I am in edit mode and I need to update the grid with new data. Since only the textbox control is databound, the RowUpdating event can only access data from the textbox column and all of my other data simply gets discarded. I also can't databind with checkbox and dropdownlist control, since they can get an invalid data type exception. So, does anyone know how can I update a column that has three different controls, where each of these three controls might have a valid data?
Thanks
First, i would switch visibility of the controls in RowDataBound of the Grid and not on PreRender, because it can only change on DataBinding and not on every postback.
You can there also bind the Dropdown to its datasource, change the checked-state of the Checkbox and set the Text-property of the Textbox according to the the Dataitem of the Row.
When you update you can find your controls with FindControl in RowUpdating and get their values(f.e. Dropdownlist.SelectedValue).
GridView has pair of events:
RowUpdating/RowUpdated
RowDeleting/RowDeleted
....
In your case your need RowUpdating - is called right before update is performed, So find in row expected control (saying checkbox) and make preparation before pass to database
As another way review possibility to use ObjectDataSource event Updating - it is usual way to specify parameters for query execution.