How to make checbox checked or uncheked in gridview editmode - c#

i have made a gridview which has many columns and the editmode , some are text and dropdown list are are working perfect .
How can i assign value to a Checkbox to make it Checked or Unchecked?
The value against which i want to do is Yes / No.
So i am having trouble doing this.
My Gridview: Just pasting portion of it as its too big.
<asp:TemplateField HeaderText="CUpdate">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("CUpdate") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCupdate" Text='<%# Eval("CUpdate") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate >
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Email") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList ID="checkEmail" runat="server">
<%--HOW TO MAKE THIS CHECKED OR UNCHECKED ? when i have values Yes / NO--%>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateField>
Update
If i make the above as :
<EditItemTemplate>
<asp:CheckBoxList ID="checkEmail" selectedvalue='<%# Eval("Email") %>' runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
In this case i do get checkbox checked when there us Yes , but in case on NO it wont run possible error as there is no value 'NO' to select in the checboxlist .Any solution.
Thank you

Your CheckBoxList needs to have all the posible values like so..
<asp:CheckBoxList ID="checkEmail" selectedvalue='<%# Eval("Email") %>' runat="server">
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:CheckBoxList>
Although i don't understand why you want to use a CheckBoxList instead of just 1 checkbox like so..
<asp:CheckBox runat="server" ID="checkEmail" Checked = '<%# Eval("Email").ToString().Equals("yes", StringComparison.OrdinalIgnoreCase) %>' />

Related

Gridview event to access controls in ItemTemplate & EditItemTemplate?

Is there one gridview event that can access a control in ItemTemplate and EditItemTemplate without additional code (ie. session, viewstate, etc)?
Let's say my gridview looks like this:
<asp:GridView ID="GridView_Sales" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesId"
OnRowDataBound="OnRowDataBound"
OnRowEditing="GridView_NSB_RowEditing"
OnRowUpdating="GridView_NSB_RowUpdating"
OnRowCommand="GridView_NSB_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView_RowDataBound has access to Label_WasSold in ItemTempplate but not the dropdown in EditItemTemplate. GridView_RowEditing has access to DropDownList_Sold but not to Label_WasSold; the same thing with GridView_RowUpdating.
I want to compare the value in Label_WasSold.Text to the value in DropDownList_Sold.SelectedValue when doing an update without having to add more code or drag session variables from one place to another.
Just add a hidden field to EditTemplate that stores the value of WasSold data item as in code below.
In your RowUpdating event, you can find the hidden field and get its value, then compare it with drop down value.
Markup to include hidden field in EditTemplate
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField id="hdnWasSold" runat="server" Value='<%# Eval("WasSold").ToString() %>' />
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
C# code to get the hidden field value in RowUpdating event
HiddenField hdnWasSold = (HiddenField)GridView_Sales.Rows[e.RowIndex].FindControl("hdnWasSold");
string wasSoldValue = hdnWasSold.Value;

how to make a read only textbox in gridview edit mode?

I want to make one of the data bound in my gridview uneditable or in my case, I want to make the textbox in edit mode in read only. here is what I have tried but not successful:
TextBox ProductImage = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
ProductImage.ReadOnly = true;
and here is the aspx code:
asp:TemplateField HeaderText="ProductImage" SortExpression="ProductImage">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductImage") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ProductImage") %>' />
</ItemTemplate>
<ControlStyle Width="50px" />
</asp:TemplateField>
can someone help me out?
Have you tried setting ReadOnly to true in your aspx code?
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Text='<%# Eval("ProductImage") %>'/>
</EditItemTemplate>
Or you could use a Label instead of a TextBox
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductImage") %>'/>

Checkbox checked issue in grid view

In the below code i have a grid view in which i have a textbox i am passing 0 and 1 to check box if it is 1 the checkbox to be checked.I tried but it is not working.Pls help me to solve the issue.
<asp:TemplateColumn HeaderText="Sales Price Ref" ItemStyle-Width="200px" HeaderStyle-VerticalAlign="Top">
<HeaderTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text="Sales Price"></asp:Label>
<br /><br />
<asp:TextBox runat="server" ID="txtSalesPriceRef" AutoPostBack="true" BorderStyle="Solid" BorderColor="#6495ED" BackColor="#B0C4DE" Height="20px" Width="90px" OnTextChanged="txtItem_TextChanged"></asp:TextBox>
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%#Eval("SalesPriceRef")=="1" ? true:false %>' />
<%-- <asp:TextBox ID="txtSalesPriceRef" Width="90px" runat="server" Text='<%#Eval("SalesPriceRef") %>'></asp:TextBox>--%>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSalesPriceRef" runat="server" Text='<%# Convert.ToString(Eval("SalesPriceRef"))== "1" ? "True" :"False" %>' > </asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
YES you can achieve it by binding it to the checkbox
as
<asp:CheckBox ID="ChSalesPriceRef" runat="server" Checked='<%# Bind("SalesPriceRef")%>' Enabled="false" />
if you allow to change checkbox value then make enable=true

Making only certain cells in a gridview column linkbuttons

I have a gridview which pulls data from a database:
I wan't to make the content within task a linkbutton that can be clicked to show a popup with additional informatiom.
When I make the column linkbuttons it is also making Total: and Subtotal: a link button:
<asp:TemplateField HeaderText="Task" ItemStyle-Width="20%">
<ItemTemplate>
<asp:LinkButton ID="taskLinkButton" runat="server"
Text='<%# Eval("Task")%>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
How do I prevent these specific rows from being link buttons?
A bit dirty but this should do the trick:
So i coulnt get to call Databinder.Eval inside <% %>, I'm not sure if it's actually possible, so I went with a different solution. The following worked for me (and its even shorter than last version):
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text='<%# Eval("task") %>' Visible='<%# Eval("incidentN") != null %>' runat="server" />
<asp:Literal Text='<%# Eval("task") %>' Visible='<%# Eval("incidentN") == null %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
You can use a PlaceHolder with conditional visibility
<asp:PlaceHolder ID="Ok" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="false"?true:false) %>'><%----%>
<asp:Label ID="Label1" Text='<%# Eval("Task")%>' runat="server" />
</asp:PlaceHolder>
<asp:PlaceHolder ID="Ko" runat="server" Visible='<%# (Eval("LoadStatus").ToString()=="false"?false:true) %>'><%----%>
<asp:LinkButton ID="taskLinkButton" runat="server"
Text='<%# Eval("Task")%>'>
</asp:LinkButton>
</asp:PlaceHolder>

DetailsView and EditItemTemplate

me problem is next:
I have a DetailsView control with many editItemTemplates some work fine but i have one dont work's.
code here:
<asp:TemplateField HeaderText="Date" SortExpression="date">
<ItemTemplate>
<%#this.putDate(Eval("fecha")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="date" runat="server" ></asp:TextBox>
<asp:CalendarExtender id="CE" runat="server" Format="dd/MM/yyyy"
PopupButtonID="calImg" TargetControlID="date">
</asp:CalendarExtender>
<asp:ImageButton id="calImg" runat="server" CommandName=""
ImageUrl="img/imgCal.gif" />
</EditItemTemplate>
</asp:TemplateField>
In view mode the information appear right but in edit mode after select new date with calendar and pres accept to update information, the data dont update. Any idea?
Thz!
Replace:
<asp:TextBox ID="date" runat="server" ></asp:TextBox>
With:
<asp:TextBox ID="date" runat="server" Text='<%# Bind("SelectedDate") %>'></asp:TextBox>

Categories