How to make a checkbox checked in a gridview using DataBinder.Eval - c#

I am trying to make a checkbox checked if the value is 1 or 0 basically in my database i have
a field called Active (bit, not null) and i can pass the value to the gridview.. but now i am trying to make it checked if the bit is 1 or not checked if the bit is 0 but its not working.. it just shows unchecked but the bit is 1.
<ItemTemplate>
<asp:CheckBox ID="ItemCheck" runat="server"
Enabled='<%# (DataBinder.Eval(Container.DataItem, "Active")) %>' />
</ItemTemplate>
Any help would be much appreciated

Give this a shot:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Convert.ToBoolean(Eval("Active"))%>' .. />
You can probably do it this way too:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#((bool)Eval("Active"))%>' .. />

You can use a CheckBoxField which will do this for you automatically and is a default subcontrol of a GridView
<asp:GridView ......>
<Columns>
<asp:CheckBoxField DataField="Active" SortExpression="Active" />
</Columns>
</asp:GridView>
This is all a matter of style but I prefer to use a RadioButtonList since it's usually more intuitive for a user
<asp:TemplateField ....>
<ItemTemplate>
<asp:RadioButtonList ID="rblActive" runat="server"
SelectedValue='<%# Bind("Active") %>'
RepeatDirection="Horizontal">
<asp:ListItem Value="1">Enabled</asp:ListItem>
<asp:ListItem Value="0">Disabled</asp:ListItem>
</asp:RadioButtonList>
<ItemTemplate>
</asp:TemplateField>

Related

Set Item limit to be display in ajaxToolkit ComboBox or set the Combobox position downwards in asp.net

I have a ajaxtoolkit combobox inside a gridview and it works perfectly, but the problem is when I click the dropdown button the list overlaps the page so I want to limit the number to be displayed to 10 items per scroll. Next problem is when I try to add an update panel to the page, It destroys the rendering of the combobox. How can I solve this? Thanks in advance
here is my code for the combobox inside the gridview
<asp:GridView CssClass="pad" ID="dgvOrder" runat="server" BorderStyle="Double" AutoGenerateColumns="False" OnRowDataBound="dgvOrder_RowDataBound" OnRowDeleting="dgvOrder_RowDeleting" HorizontalAlign="Center" >
<Columns>
<asp:TemplateField HeaderText="No." AccessibleHeaderText="No.">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Description" AccessibleHeaderText="Description">
<ItemTemplate >
<ajaxToolkit:ComboBox ID="ddlItems" AutoPostBack="True" AppendDataBoundItems="True" onmouseover="this.size=4;" onmouseout="this.size=1;" DataTextField="item_desc" DataValueField="item_id" runat="server" AutoCompleteMode="Suggest" ItemInsertLocation="Prepend" OnSelectedIndexChanged="ddlItems_SelectedIndexChanged" DropDownStyle="DropDown" BorderStyle="Double" ></ajaxToolkit:ComboBox> <br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please pick an item" ControlToValidate="ddlItems"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Inventory Code" HeaderText="Inventory Code">
<ItemTemplate>
<asp:Label ID="lblInvCode" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Qty" HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" OnTextChanged="txtQty_TextChanged" AutoPostBack="True" onkeyup ="ValidateText(this);"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator23" runat="server" ErrorMessage="Please input qty" ControlToValidate="txtQty"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="UOM" HeaderText="UOM">
<ItemTemplate>
<asp:Label ID="lblUOM" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="SOQ" HeaderText="SOQ">
<ItemTemplate>
<asp:Label ID="lblSOQ" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField AccessibleHeaderText="Reason" HeaderText="Reason">
<ItemTemplate>
<asp:TextBox ID="txtReason" runat="server" Enabled="False"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
and here is my code for the updatepanel
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
<Triggers>
(whole page inside)
<asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="txtIdNo" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
Long Item Lists / Multiple Input Controls
All of the items in a ComboBox's list will be rendered to the web page it exists in. The AutoCompleteExtender, on the other hand, retrieves items from its ServiceMethod after the page is rendered. When your ComboBox contains a rather long list of items, or when you have a relatively large number of ComboBoxes on the same page (or within the same UpdatePanel), load times could be slowed down significantly. When ComboBoxes perform slowly because of the amount of markup they must render to the browser, an AutoCompleteExtender can be used instead to increase performance.
<ajaxToolkit:ComboBox ID="ComboBox1" runat="server"
DropDownStyle="DropDown"
AutoCompleteMode="None"
CaseSensitive="false"
RenderMode="Inline"
ItemInsertLocation="Append"
ListItemHoverCssClass="ComboBoxListItemHover"
<asp:ListItem>...</asp:ListIem>
...
For more information your can look it in this link.

Bind checkbox with Images and Dropdownlist in Grid view

I have a gridview which contains images (populated dynamically from database) and dropdownlist which contains two values. First column contains checkbox. I want to insert selected checkbox's images and dropdown values to a new table on button click. What may be the suitable way?
Here is the grid view:
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" AllowPaging="true"
EmptyDataText="No images found" OnPageIndexChanging="gvDetails_PageIndexChanging" PageSize="5">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckUncheckAll"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID ="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="imgPreview" ImageUrl='<%#
"ImageHandler.ashx?imgID="+ Eval("ID") %>' runat="server"
Height="80px" Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dropdown" ItemStyle-Width="50px">
<ItemTemplate>
<asp:DropDownList ID="dpdListEstatus" runat="server" OnSelectedIndexChanged="dpdListEstatus_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
Loop with gridview rows
Find for the checkbox control
Check if its Checked property is true
If yes, call insert statement
Set the values you want to get from image and dropdownlist. Of course you need to use findcontrol on it too.
Dim cbSelect As CheckBox, imgToInsert As Image, ddlStatus As DropDownList
For Each r As GridViewRow In gvDetails.Rows
cbSelect = r.Cells(0).FindControl("CheckBox2")
If cbSelect.Checked Then
imgToInsert = r.Cells(1).FindControl("imgPreview")
ddlStatus = r.Cells(2).FindControl("dpdListEstatus")
'Insert statement goes here...
End If
Next r

Radio Buttons in Gridview

I have a list of games. Each game has a visiting team and a home team. I would like to allow the user to choose either the visiting team or the home team. So I converted to template fields and put radio buttons in. But clearly that isn't right. I need a radio button list, I think. Because with just radio buttons, they can select both. is this possible with a gridview? most tutorials I see only have one radio button in the row, so this isn't the issue. here is my gridview:
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id_game" DataSourceID="sqlPopulateGames">
<Columns>
<asp:BoundField DataField="id_game" HeaderText="id_game" InsertVisible="False" ReadOnly="True" SortExpression="id_game" />
<asp:TemplateField HeaderText="visitor" SortExpression="visitor">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("visitor") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<br />
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="home" SortExpression="home">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("home") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton2" runat="server" Text='<%# Eval("home") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="game_time" HeaderText="game_time" SortExpression="game_time" />
</Columns>
</asp:GridView>
because with just radio buttons, they can select both.
Not if those radio buttons are grouped in the markup. Radio buttons are designed to be mutually-exclusive, but they need to know which other radio buttons to be exclusive with. The GroupName property in ASP.NET is used to set this.
Something like this:
<asp:RadioButton GroupName="SomeGroup" ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
With that set, this would be mutually exclusive for any other RadioButton with the GroupName set to "SomeGroup".
Since this is happening inside a repeated control, you probably want a separate group for each iteration. So you can bind it to some value in the backing data:
<asp:RadioButton GroupName='<%# Eval("someValue") %>' ID="RadioButton1" runat="server" Text='<%# Eval("visitor") %>' />
If someValue is unique for each record in the result, then you would end up with a grouped pair of mutually exclusive radio buttons for each row.

DropDownList event within Accordian and GridView

An event bound to a DropDownList in a standalone GridView obviously would work in this fashion , but things are bit more complicated in this scenario.
The event does not fire for the DropDownList. What's interesting is the event bound to the Button Does fire. Not sure what the difference would be between the DropDownList and TextBox.
I've tried both OnSelectedIndexChanged and OnTextChanged - neither work.
The nesting is as follows:
GridView A
Ajax Accordion
GridView B (With DropDownList)
<AjaxToolkit:AccordionPane ID="AccordionPane1" runat="server">
<Header>
</Header>
<Content>
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="false"
BorderWidth="0" AlternatingRowStyle-BorderStyle="None" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" Text='<%# Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type">
<ItemTemplate>
<asp:Label runat="server" ID="lblType"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList runat="server" ID="ddlType" OnTextChanged="ddlType_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
<asp:Button runat="server" ID="btnTest" OnClick="btnTest_Click" Text="TEST" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</Content>
Thank you!
UPDATE
Turns out this had nothing to do with the nested GridViews or Accordion.
After adding the following, the event now successfully fires:
if (!Page.IsPostBack)
Populate(object);
Turns out this had nothing to do with the nested GridViews or Accordion.
After adding the following, the event now successfully fires:
if (!Page.IsPostBack)
Populate(obj);

How To change Command Text and ImageButton of ItemTemplate in TemplateField

I have a column that has an ImageButton. my database field has bit data type. I want when my record has true value in that column show True.jpg and my command become MakeFalse and when it has false value show False.jpg and my command become MakeTrue. How I can do this?Is it possible to do it with one TemplateField?
thanks
You could include two ImageButtons in a TemplateField and evaluate Visible from your bit_field
<asp:TemplateField HeaderText="YourField">
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="True.jpg" Visible='<%# (bool)Eval("bit_field") %>' />
<asp:ImageButton runat="server" ImageUrl="False.jpg" Visible='<%# !(bool)Eval("bit_field") %>' />
</ItemTemplate>
</asp:TemplateField>
I'm not sure how you'd want your Command to tie in.
This is the remaining part of the above Brissles code
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Select" Text='<%#(bool)Eval("bit_field")? "Make False":"Make True" %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Categories

Resources