Checkbox in the template field of a GridView - Need help

Posted by Gayathri under ASP.NET on 4/6/2010 | Views : 4194 | Status : [Member] | Replies : 1
hello,

This is my .aspx code.

I am not able to update the checkbox value while clicking update button.

Please help.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowMovies.aspx.cs" Inherits="ShowMovies" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="srcMovies" DataKeyNames ="ID" Width="544px"
AutoGenerateEditButton ="true" Height="224px" >
<Columns>
<asp:BoundField DataField ="Id" ReadOnly ="True" HeaderText ="Id" SortExpression ="Id" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Director" HeaderText="Director" SortExpression="Director" />
<asp:TemplateField HeaderText="InTheatres">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("InTheatres").ToString().Equals("Y")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Bind("InTheatres").ToString().Equals("Y")) %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DateReleased" HeaderText="DateReleased" SortExpression="DateReleased" />
</Columns>

</asp:GridView>
<asp:SqlDataSource ID="srcMovies" runat="server" ConnectionString="Data Source=COMPAQ\SQLEXPRESS;Initial Catalog=Movies;Integrated Security=True"
SelectCommand="SELECT * FROM [movies]"
UpdateCommand ="UPDATE movies SET Id = @ID, Title = @title,
Director = @director,InTheatres= @intheatres,DateReleased=@date
WHERE (Id = @ID)" ProviderName="System.Data.SqlClient"></asp:SqlDataSource>

</div>
</form>
</body>
</html>




Responses

Posted by: Vuyiswamb on: 4/6/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
First always when you post a question that has a code, use the code tags to format your code. Please Read this

http://www.dotnetfunda.com/forums/thread1503-forum-guidelines-before-posting-or-answering-a-question.aspx
Secondly you have a template field that has two checkboxes defined with the same name as depicted below


<asp:TemplateField HeaderText="InTheatres">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("InTheatres").ToString().Equals("Y")) %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Bind("InTheatres").ToString().Equals("Y")) %>' />
</EditItemTemplate>
</asp:TemplateField>


You said I am not able to update the checkbox value while clicking update button. , but your update statement has nothing to do with the value of the checkbox. Please elaborate on that one.

UPDATE movies SET Id = @ID, Title = @title,

Director = @director,InTheatres= @intheatres,DateReleased=@date
WHERE (Id = @ID)



Thank you for posting at Dotnetfunda
[Administrator]

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response