how to use select2 in asp gridview - asp.net

$("#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.

Related

Exception "The name 'BindItem' does not exist in the current context"

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?

Is It Possible To Display DetailsView Inline

That's it, really. I have a DetailsView and a button on my ASP.NET page, and the button always appears beneath the DetailsView. Using floating DIVs breaks things in other ways, so is there any method to suppress the line break after the DetailsView, and have it display inline with the button?
I've tried applying Display:Inline CSS to the DetailsView, but it didn't work.
EDIT - CODE ADDED BELOW
<asp:DetailsView ID="dvPremisesYardName" runat="server" datasourceid="SQLGeneralDetails" DefaultMode="Edit" AutoGenerateRows="False" FieldHeaderStyle-CssClass="fieldtitleyardname"
GridLines="None" onchange="hideControl('imgGeneralDetailsTick');" >
<Fields>
<asp:TemplateField HeaderText="Yard Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlPremisesYardName" runat="server" DataSourceID="SQLPremisesLookup" DataTextField="PremisesYardName" DataValueField="PremisesID" AutoPostBack="True"
SelectedValue='<%# Bind("AdmissionPremisesID")%>' AppendDataBoundItems="True" >
<asp:ListItem Text="Unknown" Value="" />
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:Button ID="btnPremisesAdd" runat="server" Text="Add New Premises" />
<asp:Button ID="btnPremisesEdit" runat="server" Text="Edit" />
As you stated, you do not want to use float div. The easiest way will be to use table.
Although we do not like to use table for page layout, I cannot think of other solution.
<table>
<tr>
<td><asp:DetailsView .../></td>
<td><asp:Button.../></td>
<td><asp:Button.../></td>
</tr>
</table>

save current page of gridview when page dropdownlist selected index change

I am using a multi page gridview to display bunch of data. Here is the code of the gridview.
<asp:GridView ID="unverifiedlist" runat="server" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" OnRowDataBound="unverifiedlist_RowDataBound" style="font-size:12px" >
<Columns><asp:BoundField HeaderText="Surname" DataField="Surname" ReadOnly="true"/></Columns>
<Columns><asp:BoundField HeaderText="Firstname" DataField="Givenname" ReadOnly="true"/></Columns>
......
<Columns><asp:TemplateField HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server" AutoPostBack="true" OnSelectedIndexChanged="options_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="2">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Reason">
<ItemTemplate>
<asp:TextBox ID="reason" runat="server" OnTextChanged ="reason_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Visible="false"/>
</asp:GridView>
<asp:DropDownList ID="PageSelect" AutoPostBack="true" runat="server" OnSelectedIndexChanged="PageSelect_SelectedIndexChanged"></asp:DropDownList>
there is a column called "option" which contains a dropdownlist, and it will postback everytime when the selected index was changed to save the edited row. I am trying to avoid this frequent postback. So i think if I can save the whole page before user turn to the next page.
The problem is the "PageSelect" dropdownlist's postback will trigger the page reload first, then onselectedindexchanged event, at this point, the gridview already turned to the next page. So can anyone give me some advice?
You can set EnableSortingAndPagingCallBacks property of the grid view to true , but that wont work if you are using template fields in your gridview, for maintaining the state of the page, you will have to keep the value of current page in session and on page load you can check the session..
I used the gridview itself's paging navigation control instead of my dropdownlist at last. Then I am able to use the pageindexchanging event to save the current page into a session before it goes to the next page

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

Resources