Exception "The name 'BindItem' does not exist in the current context" - asp.net

I'm recently found out that you can use "BindItem" and "Item" directly in the markup of an aspx page in the context of databound controls like GridView or DetailsView (by specifying the ItemType attribute). What I'm trying to achieve now is inline comparison of members such as:
<asp:RadioButton Text="All Assigned" ID="rb1"
Checked='<%# BindItem.AllAssigned %>'
runat="server" GroupName="AllAssigned" />
<asp:RadioButton Text="Responsible only" ID="rb2"
Checked='<%# !BindItem.AllAssigned %>'
runat="server" GroupName="AllAssigned" />
In this situation I need Two-Way-Binding, so i choose the BindItem expression.
But it seems that expressions like !BindItem.AllAssigned or BindItem.AllAssigned == false are not working in Markup. They are giving me exceptions like
The name 'BindItem' does not exist in the current context
or
DataBinding: DataContext.MyEntity does not contain a property with the name 'false'.
What do I have to write for such expressions?

Since you cannot use logical negation operator with databinding expression, you can use Eval() or DataBinder.Eval() inside data binding expression to use it, like example below:
<%-- alternative 1 --%>
<asp:RadioButton Text="Responsible only" ID="rb2"
Checked='<%# !(bool)Eval("AllAssigned") %>'
runat="server" GroupName="AllAssigned" />
<%-- alternative 2 --%>
<asp:RadioButton Text="Responsible only" ID="rb2"
Checked='<%# !Convert.ToBoolean(Eval("AllAssigned")) %>'
runat="server" GroupName="AllAssigned" />
If you want to enable two-way binding, instead using separate radio buttons with different IDs, use RadioButtonList with Bind() set in SelectedValue property as in example below:
<asp:RadioButtonList ID="rb" runat="server" SelectedValue='<%# Bind("AllAssigned") %>' RepeatDirection="Horizontal" ...>
<asp:ListItem Text="All Assigned" Value="true"></asp:ListItem>
<asp:ListItem Text="Responsible only" Value="false"></asp:ListItem>
</asp:RadioButtonList>
Then you can retrieve selected radio button value using rb.SelectedValue.
Related issue: Databinding of RadioButtonList using SelectedValue...possible?

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.

Asp.net web form model binding "Item" and "BindItem" same time to 1 control's multiple attributes/properties

I am using strongly typed with a Gridview,
This will work (1 BindItem)
<ItemTemplate>
<asp:TextBox runat="server" Text="<%# BindItem.Price %>" />
</ItemTemplate>
This will work (1 Item)
<ItemTemplate>
<asp:TextBox runat="server" Text="" Enabled="<%# Item.Price==5 %>" />
</ItemTemplate>
This will not work (1 BindItem and 1 Item)
<ItemTemplate>
<asp:TextBox runat="server" Text="<%# BindItem.Price %>" Enabled="<%# Item.Price==5 %>" />
</ItemTemplate>
CS0103: The name 'Item' does not exist in the current context
It seems once we use BindItem in 1 place, we can't use binding in with this control again.
I know I can wrap the textbox with a panel to control the "Enabled" property as a fix
Replace Item.Price==5 with BindItem.Price==5.

DropDownList TextValue is 1st in List

I'm a newbie when it comes to asp.net, so grateful for any help.
I have a simple data bound drop down list, with a details view control. I can select a value from the list, hit update and the correct value gets written to the database. Problem is, the control the automatically "resets" to display the 1st value in the list. This would confuse the user and make them think they'd selected the 1st value prior to the update.
The only code-behind code in relation to this drop down list can be found in the ItemUpdating method of the details view control, as follows:
DropDownList ddlLoc = (DropDownList)dvYourProfile.FindControl("ddlLocation");
e.NewValues["PreferredLocation"] = ddlLoc.SelectedValue;
And here's the code form the aspx page
<asp:TemplateField HeaderText="Preferred Location"
SortExpression="PreferredLocation">
<ItemTemplate>
<asp:DropDownList Text='<%# Bind("PreferredLocation") %>' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList DataValueField='PreferredLocation' DataSourceID="dsStaticDate" ID="ddlLocation" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:SqlDataSource ID="dsStaticDate" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT * FROM StaticData" />
You need to bind form the code behind. The out-of-the-box behavior of binding on the aspx page with the SqlDataSource does not allow you to stop the rebinding of the DropDownList after an event occurs.

Set value in gridview for dropdownlist template. 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

Dynamic textbox text - asp.net

I'm trying to separate my presentation and logic as much as I can, but there's one problem.
How can i set the text-property to a dynamic value from my design file? I want to do like this:
<asp:Textbox id="txtUrl" runat="server" Text="<%= user.URL %>" />
But this will fail. Am i forced to do this from code behind?
<asp:Textbox id="txtUrl" runat="server" Text="<%# user.URL %>" />
It's all about the #. but it won't get set till txtUrl.DataBind() or something higher in the object heirarchy (like the Page) calls DataBind().
How about this :
<input type="text"
id="txtUrl" name="txtUrl" runat="server"
value='<%= user.URL %>' />
You can use binding instead of evaluation.
This code binds a text box's Text property to a user's Url property returned by MyData.GetLoggedInUser(). This allows for 2-way binding.
<asp:FormView ID="UserView" runat="server" DataSourceID="LoggedInUser">
<ItemTemplate>
<asp:TextBox ID="tb"
runat="server"
Text='<%# Bind("Url") %>'></asp:TextBox>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="LoggedInUser"
runat="server"
SelectMethod="GetLoggedInUser"
TypeName="MyData">
</asp:ObjectDataSource>

Resources