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.
Related
I am getting the following error.
HttpStatusCode:500
Name:Sys.WebForms.PageRequestManagerServerErrorException
Message: Sys.WebForms.Page.RequestManagerServerErrorException
Failed to Load viewstate. The control tree into which viewstate is being
loaded must match the control tree that was used to save viewstate during
the previous request.
The scenario is as follows I have a Gridview that is bound after a DropDownList is selected. Then you can click a row on the Gridview and a ModalPopupExtender pops up. There is another GridView in the popup. If I edit that Gridview and then exit the ModalPopupExtender, and then select a different value on the DropDownList which generates the original Gridview I get the error.
DropDownList
<asp:DropDownList ID="SearchCategoryDD" runat="server" DataSourceID="InductionCategoriesDS" DataTextField="CompentencyCategory" DataValueField="CompentencyCategoryID" AutoPostBack="True" OnDataBound="SearchCategoryDD_DataBound" OnSelectedIndexChanged="SearchCategoryDD_SelectedIndexChanged"></asp:DropDownList>
Gridview when Databound with new data causes error
<asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
<asp:GridView runat="server" ID="SkillsXXX" OnRowDataBound="SkillsXXX_OnRowDataBound" >
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="SelectLBXX" Visible="False" OnClick="SelectLBXX_OnClick"></asp:LinkButton>
<asp:Label runat="server" ID="EmployeeXXXXD" Text='<%# Bind("EmployeeID") %>' Visible="False"></asp:Label>
</EditItemTemplate>
......
...
ModalPopupExtender with Gridview
<asp:Button ID="EditSupplierContactPopupBTN" runat="server" Text="" Style="visibility: hidden;" />
<ajaxToolkit:ModalPopupExtender ID="EditSupplierContactMPE" runat="server" CancelControlID="EditSupplierContactCancelBTN"
TargetControlID="EditSupplierContactPopupBTN" PopupControlID="EditSupplierContactPanel" PopupDragHandleControlID="EditSupplierContactHeader"
Drag="true" BackgroundCssClass="ModalPopupBG">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="EditSupplierContactPanel" runat="server" CssClass="PopupPNL" Style="display: none"> <!-- Style="display: none" -->
<div class="PopupHeader" id="EditSupplierContactHeader">
<div class="PopupControls">
<asp:Button ID="EditSupplierContactCancelBTN" runat="server" Text="Close" />
</div>
Edit Truck Driver
</div>
<div class="PopupBody">
<asp:UpdatePanel ID="UpdatePanel3" runat="server"><ContentTemplate>
<asp:GridView ID="Competancies2GV" runat="server" AutoGenerateColumns="False" DataKeyNames="SkillsMatrixID" Width="100%">
<Columns>
<asp:TemplateField InsertVisible="False" SortExpression="SkillsMatrixID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("SkillsMatrixID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="EditSkillsMatrixLB" runat="server" CommandArgument='<%# Eval("SkillsMatrixID") %>' OnCommand="EditSkillsMatrixLB2_Command">Edit</asp:LinkButton>
<asp:HiddenField runat="server" ID="SkillsMatrixIDHF" Value='<%# Bind("SkillsMatrixID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompentencyCategory" HeaderText="Category" SortExpression="CompentencyCategory">
</asp:BoundField>
<asp:BoundField DataField="CompentencyName" HeaderText="Compentency" SortExpression="CompentencyName">
</asp:BoundField>
...........
........
.....
I have tried the loading the Datatable from ViewState in the LoadViewState method. I still get the error.
LoadViewState
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
dataTable = (DataTable)ViewState["dataTable2"];
SkillsMatrixGV.DataSource = dataTable;
compentencyDataTable = (DataTable) ViewState["CompentencyDataTable2"];
Competancies2GV.DataSource = compentencyDataTable;
Competancies2GV.DataBind();
......
...
if I put EnableViewState="false" into the page or the GridView the error goes away but my GridView in the ModalPopupExtender does not update.
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
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
<asp:datalist ID="Datalist1" runat="server"
Width="500px" >
<ItemTemplate>
<asp:Button ID="btnviewfullprofile" runat="server" Text="View Full Profile" ToolTip="Click for Full Profile of User" CommandArgument='<%#Eval("Uid")%>' CommandName="fullprofile" />
<asp:Button ID="sendinterest" runat="server" Text="Send Interest" CommandArgument='<%#Eval("Uid")%>' CommandName="sendinterest" />
<asp:Label ID="lblstatus" runat="server" Visible="False" ></asp:Label>
</ItemTemplate>
</asp:datalist>
text of label will change according to the value of status stored in database.
code for button
if (e.CommandName == "fullprofile")
{
int Id = int.Parse(e.CommandArgument.ToString());
Response.Redirect("~/FullProfile.aspx?Id=" + Id);
`enter code here` }
but what should i write for label so that text of label should change itself based on value of status stored in database
If I understand you correctly, you need to change the HTML to something like:
<asp:Label ID="lblstatus" runat="server" Visible="False"
Text='<%# Eval("DatabaseField") %>' />
You are already using this for the CommandArgument of the button. Obviously you need to replace 'DatabaseField' with the name of the field that you want to show as text. ASP.net will fill the Text attribute with the correct value from your datasource.
as you are binding commandargument of the button from database, same way you can bind the text property of the label
<asp:Label ID="lblstatus" runat="server" Visible="False" Text= '<%#Eval("textfield")%>' ></asp:Label>
If you need more advance control then you need to set them when items are getting bound see here fore details http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.itemdatabound.aspx
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!