ASP.NET Databinding a DropDownList - c#

Greetings Gurus,
I have an ASP.Net app that I'm working on with a editable detailsview.
There is a boundfield in the detailsview called Status which I turned into a template field.
This templatefield (specifically the editItemTemplate) was changed to a dropdownlist with a unique datasource. How do I Bind this dropdownlist to the StatusField so my update query picks up it's value when I click update?
<EditItemTemplate>
<asp:DropDownList ID="DropDownListStatus" runat="server"
DataSourceID="Status_DataSource" DataTextField="Status" DataValueField="Status">
</asp:DropDownList>
</EditItemTemplate>

You cannot bind to controls from a template field. You need to read the value (using FindControl to find your DropDownListStatus and read its value) and set it for the update query manually in your update method.

Related

asp.net c# gridview label value disappear when sorting

I have a gridview that contain label, the value of this label is assigned in code behind,
I enabled sorting in my gridview and when I click the header to sort the value of the label disappear but the other DataFields doesnt.
<asp:TemplateField HeaderText="<%$Resources:mj.resource, req_category%>" SortExpression="category_id">
<ItemTemplate>
<asp:Label ID="category_id" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Have you set EnableViewState="false" on the GridView or at the page level?
Are you using DataSourceControl (like SqlDataSource) to bind data to the GridView?
If so, you'd have to manually sort and bind data again in the Sorting event.

Telerik RadGrid Get Client ID of Control for each GridItem or Row inline

i have a RadGrid and it has a template column which contains a asp:CheckBox and a label , so label has a "for" Attribute which the value is equal to the ID of the CheckBox so i want to set the CheckBox ID to the "for" attribute for the label
this scenario is to change the style of checkbox to Material Design of Google,
and my asp.net is C# ,
i did this scenario in VB language i want to do it in C#
and that's the code that i have did in VB and Works Great ==>
<telerik:GridTemplateColumn AllowFiltering="false">
<ItemTemplate>
<asp:CheckBox ID="cbxSelect" runat="server" onClick="checkboxClicked(event, 'cbxSelect')"></asp:CheckBox>
<label for="<%# DirectCast(Container, GridViewRow).FindControl("cbxSelect").ClientID %>"></label>
</ItemTemplate>
</telerik:GridTemplateColumn>
Any Idea?
This may also work: ((GridViewRow)Container).FindControl("cbxSelect").ClientID

Put a DropDownList instead of TextBox in DetailsView control

I have an EntityDataSource control that is bind to an edmx file.
I create a detailsview and set it's DataSource property to entitydatasource control.
now I want to put a DropDownList instead of TextBox for "no_region_code" in Inserting mode.
Here is what I found from internet but it does not insert the contents of dropdownlist to the table when i click on insert button of detailsview.
<asp:TemplateField HeaderText="no_region_code" SortExpression="no_region_code">
<InsertItemTemplate>
<asp:DropDownList ID="drp_region_list" runat="server" DataMember="no_region_code">
<asp:ListItem Value="41">tabriz</asp:ListItem>
<asp:ListItem Value="21">tehran</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
How can I put a dropdownlist instead of these textboxes?
You didn't bind any property on that DropDownList. Try something like this
<asp:DropDownList ID="drp_region_list" runat="server" SelectedValue='<%# Bind("no_region_code")%>' DataMember="no_region_code">
<asp:ListItem Value="41">tabriz</asp:ListItem>
<asp:ListItem Value="21">tehran</asp:ListItem>
</asp:DropDownList>
EDIT : bind represent a way to link your property from the code behind (or model) to an asp control. This binding is bidirectional. For example, if you already have a value for no_region_code property in the code behind, the dropdown will already have that value selected on the view. Google asp.net data binding and you'll find a lot of examples and some more in depth explanations.

Retain form data while paging through a GridView - ASP.NET

I have a GridView control on my ASP.NET page that binds to result set when the user executes a search. I create an additional TemplateField column with a CheckBox control to allow the user to select a sub set of records from the result set. I have implemented paging in the GridView control and when the user checks the checkbox control and pages through the result set it does not retain any of the checked checkboxes.
<asp:GridView ID="MyGridView" runat="server" AllowPaging="true" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="MyCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Whaat is the best way to retain the checked checkboxes while paging through the GridView?
You have to maintain the state yourself. This Thread shows how this can be done in VB. just use this VB to C# converter to get your desired code
you'll need to loop over the rows and save the checked state of the check boxes (along with a datakey) during the gridview paging event. Likewise you will need to read from your saved check state list to re-check the boxes during the gridview rowdatabound event.

How to set DropDownList's selected value inside a Repeater?

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.

Categories