Set value in gridview for dropdownlist template. Asp.Net - asp.net

I have problem with DropDownList template in Grid View, after I go in edit mode drop down have to be selected by current item instead of default value.
<asp:TemplateField HeaderText="Tour Type">
<EditItemTemplate>
<asp:DropDownList AppendDataBoundItems="true" DataSourceID="dropDownListSqlDataSource" runat="server" DataValueField="idTypetour" DataTextField="title"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<%#Eval("typeTitle")%>
</ItemTemplate>
</asp:TemplateField>
I tried to use SelectedValue="<%#Eval("typeTitle")%>", but has no results.

Try to use Bind instead of Eval:
SelectedValue='<%# Bind("idTypetour") %>'
The DataValueField is "idTypetour" and the DataTextField is "title", you have tried to use Eval("typeTitle"). What column is typeTitle? I assume that this must be idTypetour because you want to set the SelectedValue, what normally is the ID.
Here are informatons on the differences:
http://msdn.microsoft.com/en-us/library/ms178366.aspx

Related

how to use select2 in asp gridview

$("#ddl_subject").select2();
how to use javascript call ID in asp gridview ItemTemplate.
<asp:TemplateField>
<ItemStyle Width="24%" BackColor="#f8f8f8" />
<ItemTemplate>
<asp:DropDownList Style="width: 100%"
ID="ddl_subject" runat="server" EnableViewState="true" AutoPostBack="True" >
<asp:ListItem Value="">-- Please Select Value --</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
It's hard to say from the amount of code posted but a control within a gridview will not have the simple id of "ddl_subject", it will be prefixed with other things generated by asp .net.
Try doing the following selector instead:
$("[id$='ddl_subject']").select2();
This says to select any elements ending in "ddl_subject" which should find the drop down control on every row.

DropdownList in Gridview not saving values

I have a gridview with a dropdownlist in one column in editing mode
The problem is that I can't save the selected value after edit and I can show the data from the field when I press edit.
I show some examples doing SelectedValue='<%# Bind("Category") %>' but the SelectedValue is unrecognizable.
The page is
<asp:TemplateField HeaderText="Category" SortExpression="Category">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataTextField="Category" >
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
When I use a textbox instead of the dtopdown then the values after editing are saved correctly
Any ideas?
Finally I found a solution to my problem.
In the templatefield I've select edit databindings and then in the SelectedValue I've select Custom Binding and put Bind("category")
Now it works

Eval and Bind doesn't work for dropdownlist in repeater

Am having a DropDownlist inside a repeater control.Initially the dropdown list should have the value from the database.And in my case its the userStatus property of my object.Eval and Bind is not working in this and When i use
<asp:DropDownList ID="DropDownList1" runat="server" Enabled="true" DataSource='<%#Eval("userStatus") %>' CssClass="box1" AppendDataBoundItems="true">
am getting the value of dropdown as characters.Suppose if the value is Admin its coming as A d m i n as each option.Is there any way to do it apart from the code behind page.
userStatusplease set "DataTextField" and "DataValueField" of dropdown.
<asp:DropDownList ID="DropDownList1" runat="server" Enabled="true" DataSource='<%# (string)DataBinder.Eval(Container.DataItem, "userStatus") %>' CssClass="box1" AppendDataBoundItems="true">

How to set ItemTemplate text value to combobox DataTextField instead of DataValueField

I have a telerik radgrid with a GridTemplateColumn as seen below in a C# ASP.NET 4.0 webform.
As you can see, in the EditItemTemplate I am using a RadComboBox with an id number for DataValueField and a human readable text for DataTextField.
My question is, how can I change the Text in my ItemTemplate to show the human readable value instead of the Id? The value Alias1 comes from the grid datasource and it is the Id.
Thank you for any help you can provide!
<telerik:GridTemplateColumn UniqueName="Alias1" Display="true" DataField="Alias1" HeaderText="Alias1" SortExpression="Alias1">
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Bind("Alias1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadComboBox ID="RadComboBox16" runat="server" Skin="Outlook" Height="150" DataSourceID="SqlDataSourceAliasOptions" DataTextField="aliasText" DataValueField="aliasid" SelectedValue='<%#Bind("Alias1") %>'>
</telerik:RadComboBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
You need to change ItemTemplate binding like this:
<ItemTemplate>
<asp:Label ID="lblField30" CssClass="text" runat="server" Text='<%# Eval("aliasText") %>'></asp:Label>
</ItemTemplate>
Of course your binded entity must have "aliasText" property. If you are binding something like DataTable make sure that contains "aliasText" column.

ASP.NET GridView - some questions about the LinkButton column

I have an LinkButton column in my GridView:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
let's assume that I need to bound to that GridView a list of some items, where some of them shoud have visible that LinkButton, some not. So that is the question: how - while bounding/after bound - can I realize that scenario, I mean show LinkButtons (with different CommandArgument) where there are needed ?
You have 3 options:
Handle the RowDataBound event: find the button in the row and set the visible property
Bind a property/column in the datasource: <asp:LinkButton Visible='<%# Bind("Editable") %>'
create a method returning a boolean in the page and use it: <asp:LinkButton Visible='<%# IsButtonVisible(DataBinder.Eval(Container.DataItem, "ID")) %>'

Resources