How to assign asp:RequiredFieldValidator to asp:TextBox with jquery/javascript - c#

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!

Related

C# WebForm, how to access HyperLink Text value inside DATALIST?

Is it possible to access the Text of ProductName inside this Hyperlink (DATALIST) ?
Thank you!!
<asp:DataList ID="DataList1" runat="server" CellPadding="0" RepeatColumns="4" RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False" OnSelectedIndexChanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# Eval("productName") %>' runat="server" Font-Size="Large"></asp:HyperLink>
</ItemTemplate>
</asp:DataList>

change textbox value in asp.net

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

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.

asp.net DataList inside Datalist problems with C# interpretation

I need to use DataList inside another DataList. It works fine for me, but when I'm trying to do something in code behind with this inside one it just doesn't exist for C#. Here is code:
...
<asp:DataList ID="DataListDziennik" runat="server"
DataSourceID="SqlDataSourcePrzedmioty">
<ItemTemplate>
<asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />
...
<asp:DataList ID="DataListOceny" runat="server"
DataSourceID="SqlDataSourceOceny"
RepeatDirection="Horizontal"
OnItemCommand="DataListOceny_ItemCommandOceny"
OnEditCommand="DataListOceny_EditCommandOceny">
<EditItemTemplate>
<asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
<td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
<td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>
</ItemTemplate>
</asp:DataList>
</td>
</ItemTemplate>
</asp:DataList>
When I write this in code behind:
protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
{
DataListOceny.EditItemIndex = e.Item.ItemIndex;
DataListOceny.DataBind();
}
...Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, it can be placed anywhere on website. Do you know any solution for this problem?
Because DataListOceny is a control inside of another control, you have to make a reference to it by doing something like:
DataList DataListOceny = (DataList)e.Item.FindControl("DataListOceny");
Once you do that, you can use the DataListOceny variable. Hope this helps.

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.

Categories