change textbox value in asp.net - c#

I am using this gridview, I have one header checkbox and one column containing textbox. I want that when i check the header checkbox the value of every textbox changes from 0 to 1.
<asp:GridView ID="grdData" runat="server" style="Text-align:center;">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" OnClick="CheckAllEmp(this)"/>
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Status_Header" runat="server" Text="Status" />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

You can do this by using Jquery.
Not tested with the actual data, but hope this should work for you.
$(function () {
$('#CheckBox2').change(function () {
$("#TextBox1").val(($(this).is(':checked')) ? "1" : "0");
});
});
Also you must need to add jquery plugin right before the javascript code which I gaved u. Below is one of the plugin which you can use
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
Do let us know if it works or not

Related

ASP.NET repeater get all items and checkbox if user has right

I have an ASP.NET application and I would like to display all roles and add a checkbox which is checked if the user has the right.
I use an ASP repeater to get all roles but how can I check the checkbox in the same repeater to get the user's role ?
Here is my code:
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked="False" />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
<asp:Repeater ID="RepeaterRole" runat="server" DataSourceID="ObjectDataSource2">
<ItemTemplate>
<div>
<asp:CheckBox runat="server" Checked='<%# Convert.ToBoolean(Eval("role")) %>' />
<asp:Label CssClass="lbl" ID="Label1" runat="server" Text='<%# Eval("RoleLabel")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllRolesToCollection" TypeName="Business.BusinessObject.Role"></asp:ObjectDataSource>
Make sure you get 'role' field 1 or 0 from DB
Thanks Hardik,
based on your solution, I created a similar method like this: public Dictionary GetAllRolesFromUserToDictionary() so the checkbox will bind to the boolean value
Then the ASP code is like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource3">
<ItemTemplate>
<div>
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("Value")%>' />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Key")%>'></asp:Label>
</div>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetAllRolesFromCurrentUserToDictionary" TypeName="BusinessObject.RoleCollection"></asp:ObjectDataSource>

Value insertion in database from GridView

I have one column of TextBox and one column of CheckBox in ItemTemplate in a GridView and number of rows are generating dynamically.
When I click on CheckBox the value of TextBox is changing but when I am inserting the values into database, default value which I have given in TextBox it is saving that.
<asp:GridView ID="grdData" runat="server" Style="text-align: center;">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="changeTextValue(this)" />
</ItemTemplate>
<HeaderTemplate>
<!-- <asp:CheckBox ID="CheckBox2" runat="server"OnClick="CheckAllEmp(this)" />-->
</HeaderTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Status_Header" runat="server" Text="Status" />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Enabled="false" Text="1" ClientIDMode="Static"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I found solution, remove Enabled="false" property from TextBox1 and check.
<asp:TextBox ID="TextBox1" runat="server" Enabled="false" Text=1 ClientIDMode="Static"></asp:TextBox>
If you want to disabled textbox from end user, I suggest you to use LABEL control instead of TEXTBOX.
Here is the jQuery function for LABEL control.
<script>
function changeTextValue(chk) {
var currentTextID = $(chk).parents('tr').find('span[id$="Label1"]');
alert(chk.checked + ": " + currentTextID.text());
if (chk.checked == true)
currentTextID.text("Present");
else
currentTextID.text("Absent");
}
</script>
Label control should be like below:
<asp:Label ID="Label1" runat="server" Text="Absent" EnableViewState="false" ClientIDMode="Static"></asp:Label>
Please let me know if you have any questions.

When i Check the Checkbox how do I change that specific row style? C# ASP.NET

So i got this gridview on ASP.NET, it is about a gmail client, i made a template field with a check box that is for select several message at the same time, but first when you select a message i want to change the row background color, I already have my CSS but i dont know how to trigger the action or how to know what row was selected in the code. Here is the template of my grid:
<asp:GridView ID="inboxGrid" runat="server"
AutoGenerateColumns="false" CssClass="inbox" ShowHeader="false" BorderStyle="None" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
<ItemStyle Width="24" CssClass="check"/>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="from" />
<ItemTemplate>
<asp:Label ID="lblFrom" runat="server"
Text = '<%# Eval("From") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="subject" />
<ItemTemplate>
<asp:Label ID="lblSubject" runat="server" Text='<%# Eval("Subject") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemStyle CssClass="date" />
<ItemTemplate>
<asp:Label ID="lblFecha" runat="server" Text='<%# Eval("Fecha") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="inbox"/>
</asp:GridView>
Here is a solution using JQuery
Provided you add a CssClass named chk to asp:checkbox
<asp:CheckBox ID="chkSelect" runat="server" CssClass="chk" />
Here is the script
$(document).ready(function () {
$(".chk :checkbox").live("click", function () {
$(this).closest("tr").css("background-color", this.checked ? "#0000FF" : "");
});
});
Try this:
<asp:CheckBox ID="chkSelect" runat="server" OnClick="changeCss(this.id);" />
You can use toggleClass('CssClassName') to toggle the class on checkbox clicks:
function changeCss(ctrlId){
$(ctrlId).parent().parent().toggleClass('check');
}
The hierarchy is tr > td > checkbox, we go reverse to change the css of tr/row.

Get gridview values from code behind

I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.

How to assign asp:RequiredFieldValidator to asp:TextBox with jquery/javascript

Just wondering, will it be possible to assign the request validation to asp:TextBox with jquery/javascript?
I have the following code will create a checkbox with a textbox next to it:
<tr>
<th class="graytext r">Add Reps to Team:</th>
<td>
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
DataSourceID="dsEmployees" EnableViewState="false"
GridLines="None" CssClass="clGridDirectory">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox runat="server" ID="employee_name" CssClass="employee_name" Text='<%# Eval("fullname") %>'/>
<asp:HiddenField runat="server" ID="employeeidToRep" Value='<%# Eval("employeeid") %>'/>
<asp:TextBox runat="server" ID="repID" Text='<%# Eval("rep_id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
SelectCommand="app_staff_without_team_select" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
</td>
</tr>
Just wondering, will it be possible for me to assign the asp:RequiredFieldValidator to the textbox when checkbox is checked or removed the asp:RequiredFieldValidator when checkbox is unchecked?
ASP.NET renders a client-side function named ValidatorEnable that you can use to enable/disable a validator on the fly. Just called it from the CheckBox's click event:
$(function() {
$('#checkBoxID').click(function() {
var validator = document.getElementById('validatorId');
ValidatorEnable(validator, $(this).prop('checked'));
});
});
Set the CssClass attribute on the TextBox, such as CssClass="RepClass" and try to disable the required field validator in the 'click' event:
so...something like this...
$(".RepClass").click(function()
{
$(".RepClass").attr("disabled","disabled");
});
not 100% this will work, but worth a try!

Categories