How to Set the Checkbox value inside GridView? - asp.net

How to set the CheckBox value, which is located inside a Gridview ?
<asp:GridView ID="gviewPermission" runat="server"
onrowdatabound="gviewPermission_RowDataBound"
onrowupdated="gviewPermission_RowUpdated"
onrowupdating="gviewPermission_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Allow" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Check_Allow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deny" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Check_Deny" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The check box value has to set based upon some condition....

In the gviewPermission_RowDataBound function do:
if(e.Row.RowType == DataControlRowType.DataRow)
((CheckBox)e.Row.FindControl("Check_Allow")).Checked = SomeCondition;
Or if the condition is coming directly from the datasource you can do:
<ItemTemplate>
<asp:CheckBox ID="Check_Allow" runat="server"
Checked='<%# Eval("ConditionFromDs") %>' />
</ItemTemplate>

If the value of column is boolean then. Try the below code
<ItemTemplate>
<center>
<asp:CheckBox ID="chkSelect" Checked='<%#Convert.ToBoolean(Eval("isChecked"))%>' runat="server"></asp:CheckBox></center>
</ItemTemplate>
Where "isChecked" is the column name.

the CheckBox control has an attribute called Checked that works similarly to the html counterpart attribute. So set this attribute either in the aspx markup:
<asp:CheckBox ID="Check_Allow" runat="server" Checked='<%= someCondition == true %>' />
or in your code-behind.

<ItemTemplate>
<asp:CheckBox runat="server" checked='<%# bool.Parse(Eval("check").ToString()) %>' ID="chkselet" />
</ItemTemplate>
check value must be true or false

Related

How to validate a textbox value based on another textbox value inside gridview in jquery

I have two textbox inside gridview. If 1st text contains any data then 2nd textbox should not be empty. If 1st textbox is null then no validation for 2nd textbox. Only the alert will come after filling the 1st textbox 2nd textbox shouldn't empty. How to do that?
Any idea? Please help me out.
<asp:GridView runat="server" Width="980px" ID="grdResUpdate" AutoGenerateColumns="false"
CssClass="TopMargin10 borderClass gridwrap" OnRowDataBound="grdResUpdate_RowDataBound" ShowHeader="true">
<Columns>
<asp:TemplateField HeaderText="SO #" ItemStyle-Width="70px">
<ItemTemplate>
<asp:Label runat="server" Width="70px" ID="lblSOName" CssClass="gridwrap" Text='<%# Eval("SOName")%>' />
<asp:HiddenField runat="server" ID="hdnFldSOId" Value='<%#Eval("SOId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Type" ItemStyle-Width="300px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:HiddenField runat="server" ID="testTypeIdHdnFld" Value='<%#Eval("TestTypeId") %>' />
<asp:Label runat="server" Width="150px" ID="lblTestTypeName" CssClass="gridwrap" Text='<%# Eval("TestTypeName")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Case Count" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtTestCaseCount" runat="server" HeaderText="Test Case Count" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Environment" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtEnvironment" runat="server" HeaderText="Environment" Width="80px" onkeydown="return isDigit(event)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Passed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPassed" runat="server" HeaderText="Passed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Failed" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtFailed" runat="server" HeaderText="Failed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Of Defects" ItemStyle-Width="70px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtDefects" runat="server" HeaderText="No Of Defects" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hours" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtHours" runat="server" HeaderText="Hours" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test Case Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtTCComp" runat="server" HeaderText="Test Case Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Percentage Completed" ItemStyle-Width="100px" ItemStyle-VerticalAlign="Top">
<ItemTemplate>
<asp:TextBox ID="txtPercComp" runat="server" HeaderText="Percentage Completed" Width="80px" onkeydown="return isDigit(event)"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerTop" />
<AlternatingRowStyle CssClass="rowbgblue" />
<RowStyle CssClass="rowbginwhite" />
</asp:GridView>
I want when the user fills the txtTestCaseCount then he must have to fill txtHours
can check with onfoucsOut event
$("textbox2id").focusOut(function(){
if(!($("textbox2id").val() == ""))
{
alert("please enter a value as required");
$(this).focus();
}
})
First access gridview text boxes and then place checking code inside change event of txtTestCaseCount textbox
var textBoxOne = '#<%=grdResUpdate.ClientID%> input[id*="txtTestCaseCount"]';
var textBoxTwo = '#<%=grdResUpdate.ClientID%> input[id*="txtHours"]';
$(textBoxOne).on('change', function () {
if($(textBoxTwo).val()==""){
alert("Please fill hours textbox");
}
});
this checks if txtTestCaseCount value is filled then if textbox txtHours is not filled then alert message is shown.
Every textbox in gridview has an id generated at runtime. Use that id as a selector, like
$("#textboxid").val()
.This will give u the content of the textbox. Use it to validate the other textbox.
If suppose the id generated of txtTestCaseCount is like grdResUpdate_txtTestCaseCount then
if($("#grdResUpdate_txtTestCaseCount").val().length < 0){//Code}

how to bind checkbox value from datatable in gridview

I need to figure out how to bind a CheckBox value in a GridView, I have written CheckBox.Checked= DataBinder.Eval(Container.DataItem, "IsSubscribed") in GridView, but the CheckBox is always checked, even when IsSubscribed is false.
I have bound the grid in Page_Load, before the page has posted back. Here is my code:
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox
ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%# DataBinder.Eval(Container.DataItem, "IsSubscribed") %>'/>
</ItemTemplate>
</asp:TemplateField>
Thanks.
Put this code as your Item Template element:
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%#bool.Parse(Eval("IsSubscribed").ToString())%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox
ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%#Convert.ToBoolean(Eval("IsSubscribed")) %>'/>
</ItemTemplate>
</asp:TemplateField>
please use this......
Eval() give an object type.
So you have to use Eval(..).ToString() if you want to compare it...
Like:
<asp:TemplateField HeaderText="Actif">
<ItemTemplate><asp:CheckBox ID="chkIsACTIF" runat="server" Enabled="false" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' /></ItemTemplate>
<EditItemTemplate><asp:CheckBox ID="chkACTIF" runat="server" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' Enabled="true" /></EditItemTemplate>
<FooterTemplate><asp:CheckBox ID="chkNewACTIF" runat="server" Checked="true" /></FooterTemplate>
</asp:TemplateField>

Merge datagrid cells

i have to merge the cells from the cell that does not contain the radio button, to the cell that contains the radio button.
here the link for the interface
http://i839.photobucket.com/albums/zz316/girish_kolte/untitled.jpg
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwAirportSchedule" runat="server">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Airport">
<asp:RadioButton ID="rbAirport" runat="server" Visible='<%# (bool)Eval("IsDestination") %>' />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("Airport") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>
good answer from David.
One could reduce, by omitting Lable like below
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Airport") %>' />

GridView CheckBox

How do I check the checkbox in gridview on the database bit '1' and uncheck on '0' automatically and then submit the checked values into database? please help me.
I tried this and it simply did the job:
<asp:TemplateField HeaderText="Is Active">
<ItemTemplate>
<asp:CheckBox ID="Chk" runat="server" Checked='<%# Bind("fieldname") %>' />
</ItemTemplate>
</asp:TemplateField>
This should get you going
http://www.asp.net/Learn/data-access/tutorial-52-vb.aspx
A templated field may work:
<asp:TemplateField HeaderText="Serial Number" SortExpression="SERIALNUMBER" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# iif(Bind("FIELD")=0,"False","True") %>' />
</ItemTemplate>
</asp:TemplateField>

Simple databinding to gridview columns

I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).

Resources