I have a textbox in a datalist I'm trying to access in edit mode:
<asp:DataList ID="dl1" OnEditCommand="dl1_EditCommand"
OnCancelCommand="dl1_CancelCommand" OnUpdateCommand="dl1_UpdateCommand"
runat="server">
...
<asp:TextBox ID="tbType" Width="600" runat="server" Text='<%# Eval("Type") %>' />
CodeBehind:
protected void dl1_UpdateCommand(object sender, DataListCommandEventArgs e)
{
TextBox tb = (TextBox)e.Item.FindControl("tbType");
}
My code executes, but the value of the textbox is always empty, even though I have a value in it! I don't get either my updated text or the default text - I get a null. It finds the textbox, I've even opened the inspector to view its text...
This hasn't happened before and I'm not sure what I'm doing wrong. I've never had a problem like this before...
Full disclosure - this is a datalist inside a usercontrol.
UPDATE
Showing EditItemTemplate tags as requested.
The value is "" - I get a reference to the textbox, but no value.
<EditItemTemplate>
<td><asp:LinkButton ID="lbEdit" runat="server" Text="Update" CommandName="update" CausesValidation="false" />
<br /><asp:LinkButton ID="lbCancel" runat="server" Text="Cancel" CommandName="cancel" CausesValidation="false" />
</td>
<td>
<asp:HiddenField ID="hfID" runat="server" Value='<%# Eval("Id") %>' />
<asp:TextBox ID="tbType" Width="600" runat="server" Text='<%# Eval("Type") %>' />
</td>
<td></td>
</EditItemTemplate>
Related
We have a page that allows users to update data using about a dozen textboxes and several dropdowns. Half of all the fields are permanently visible and the rest are hidden. If a checkbox is checked, the page reveals the rest of the fields.
When first arriving on the page, the two textboxes and dropdown in question are not visible, along with the rest of the fields not visible, unless the checkbox is checked.
However, with the update functionality (a button click) the two textboxes and dropdown appear with the other permanently visible fields. Oddly enough, the labels corresponding to the fields that should be hidden do not appear. Another point is that, if the checkbox is checked and then unchecked, the fields in question are not visible.
So it appears that the issue is with the update functionality but we haven't been able to pinpoint what the issue is. We have tried setting the visibility to false in the .ascx.cs file but that has had zero effect along with other things we've tried.
Is there anything obvious we might be missing from our code below?
ascx:
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsQWER" DataKeyNames="Oid"
OnItemInserting="FormView1_ItemInserting" EnableModelValidation="true"
OnItemUpdating="FormView1_ItemUpdating">
...
<tr>
<td style="width: 200px">
<asp:Label ID="ContactFNameLabel" runat="server" Text="Vendor Contact (First Name):"
Visible="false"></asp:Label>
</td>
<td>
<asp:TextBox Width="400px" ID="ContactFNameTextBox" runat="server" Text='<%# Bind("FirstName") %>'
Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactFNameValidator" runat="server" ControlToValidate="ContactFNameTextBox"
Display="Dynamic" ErrorMessage="Contact Name cannot be Blank" Visible="false"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 200px">
<asp:Label ID="ContactLNameLabel" runat="server" Text="Vendor Contact (Last Name):"
Visible="false"></asp:Label>
</td>
<td>
<asp:TextBox Width="400px" ID="ContactLNameTextBox" runat="server" Text='<%# Bind("LastName") %>'
Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactLNameValidator" runat="server" ControlToValidate="ContactLNameTextBox"
Display="Dynamic" ErrorMessage="Contact Last Name cannot be Blank" Visible="false"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 200px">
<asp:Label ID="CountryLabel" runat="server" Text="Country:" Visible="false"></asp:Label>
</td>
<td>
<asp:DropDownList Width="405px" ID="CountryDropDownList" runat="server" AppendDataBoundItems="true"
DataSourceID="odsCountry" DataTextField="Name" DataValueField="Oid" SelectedValue='<%# Bind("CountryId") %>'
Visible="false">
<asp:ListItem Selected="true" Text="<--Select Here-->" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="CountryValidator" runat="server" ControlToValidate="CountryDropDownList"
Display="Dynamic" ErrorMessage="Country Must be Selected" InitialValue="0"></asp:RequiredFieldValidator>
</td>
</tr>
...
<asp:Button ID="UpdateButton" runat="server" CausesValidation="true" CommandName="Update" OnClick="btnUpdateClick"
CssClass="button" Text="Update" Visible='<%# true %>' />
...
<asp:ObjectDataSource ID="odsQWER" runat="server" InsertMethod="AddRequest"
TypeName="BLL.TESTRequest" SelectMethod="GetRequest" OnInserting="odsQWER_Inserting"
UpdateMethod="UpdateRequest" OnUpdating="odsQWER_Updating" OnInserted="odsQWER_Inserted"
OnUpdated="odsQWER_Updated" OldValuesParameterFormatString="{0}">
...
aspx:
protected void btnUpdateClick(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)FormView1.FindControl("IsNewCheckBox");
FormView1.FindControl("ContactFNameTextBox").Visible = chk.Checked;
FormView1.FindControl("ContactFNameLabel").Visible = chk.Checked;
FormView1.FindControl("ContactFNameValidator").Visible = chk.Checked;
FormView1.FindControl("ContactLNameTextBox").Visible = chk.Checked;
FormView1.FindControl("ContactLNameLabel").Visible = chk.Checked;
FormView1.FindControl("ContactLNameValidator").Visible = chk.Checked;
FormView1.FindControl("StateDropDownList").Visible = chk.Checked;
FormView1.FindControl("StateLabel").Visible = chk.Checked;
}
I have reapeater in my code and trying to validate the textbox using asp.net required field validator. But validation messsage
not displaying, i opened the developer tools and found that style="visibility:hidden" added into the required field validator.
Below is my code
<asp:Repeater ID="RepeaterCategory" runat="server" DataSource='<%# this.Categories.Count==0 ? null : this.Categories %>'>
<ItemTemplate>
<div>
<asp:Label runat="server" Visible="true" Text="Category" />
<asp:PlaceHolder runat="server" Visible="true">
<asp:TextBox ID="txtCategoryID" runat="server" value="1" />
<asp:TextBox ID="txtCategoryName" runat="server" value="<%# (Container.DataItem as Category).Name %>" />
<asp:RequiredFieldValidator runat="server" ErrorMessage="<br/>This is a required field" ControlToValidate="txtCategoryName" ValidationGroup="NewCategoryGroup"></asp:RequiredFieldValidator>
</asp:PlaceHolder>
<asp:LinkButton runat="server" ToolTip="Save" ValidationGroup="NewCategoryGroup" OnClick="SaveCategory_Click"><img src='<%# some path%>/images/save.gif' /></asp:LinkButton>
<asp:LinkButton runat="server" ToolTip="Close" OnClick="CloseCategory_Click"></asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
Code behind file
protected void SaveCategory_Click(object o, EventArgs e)
{
Page.Validate("NewCategoryGroup");
if (!Page.IsValid)
return;
//logic
}
Can anyone suggest how to enable it?
The style="visibility:hidden" is default behavior. It changes to style="visibility: visible;" when the error message needs displaying. Therefore you probably don't have an error.
The validator is a 'RequiredFieldValidator', and since the textbox that is being validated is already filled with the value "TestCategory" there are no errors. If you just add text to the Save button (which has no ID tag) so that it becomes visible, remove the value from the txtCategoryName textbox and click the save button you will see the error message.
This works:
<asp:TextBox ID="txtCategoryName" runat="server" value="" />
<asp:RequiredFieldValidator runat="server" ErrorMessage="<br/>This is a required field" ControlToValidate="txtCategoryName" ValidationGroup="NewCategoryGroup"></asp:RequiredFieldValidator>
<asp:LinkButton runat="server" ToolTip="Save" ValidationGroup="NewCategoryGroup" OnClick="SaveCategory_Click" ID="LinkButton1">Save Me!</asp:LinkButton>
You don't need this code
Page.Validate("NewCategoryGroup");
if (!Page.IsValid)
Another advantage is that the validators work now without a postback, this saves a roundtrip to the server.
And ALWAYS do server side validation also, but try to do the first validation without postback.
UPDATE
What you want is probably validation per item. And since your validationgroup is always the same it will fire for all textboxes. Try this:
<asp:Repeater ID="RepeaterCategory" runat="server">
<ItemTemplate>
<div>
<asp:TextBox ID="txtCategoryName" runat="server" ValidationGroup='<%# "myVal_" + Container.ItemIndex %>' Text='<%# Eval("Category") %>' />
<br />
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ErrorMessage="This is a required field<br />" ControlToValidate="txtCategoryName" ValidationGroup='<%# "myVal_" + Container.ItemIndex %>'></asp:RequiredFieldValidator>
<asp:LinkButton runat="server" ValidationGroup='<%# "myVal_" + Container.ItemIndex %>' OnClick="Button1_Click" ID="LinkButton1">Save Me!</asp:LinkButton>
</div>
</ItemTemplate>
</asp:Repeater>
I have a asp.net Listview and I have add a RequiredFieldValidator for edit operations in EditItemTemplate.
When I click "Edit" button and try to save the data with an empty CustomerNameTextBox then I get error "Please enter your name!"
This is OK
<%# Page Language="C#" UnobtrusiveValidationMode="None" AutoEventWireup="true" CodeBehind="frmMain.aspx.cs" Inherits="DB_mit_GridView.frmMain" %>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="CustomerIDLabel1" runat="server" Text='<%# Eval("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
<%-- CustomerNameTextBox must not be empty when editing an existing record
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
</td>
But I want to check for empty CustomerNameTextBox not only when editing an existing record but also when I insert a new record.
So I add RequiredFieldValidator for Insert operations in InsertItemTemplate :
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="CustomerIDTextBox" runat="server" Text='<%# Bind("CustomerID") %>' />
</td>
<td>
<asp:TextBox ID="CustomerNameTextBox" runat="server" Text='<%# Bind("CustomerName") %>' />
<%-- CustomerNameTextBox must not be empty when insertig a new record
<asp:RequiredFieldValidator runat="server" id="reqName2" controltovalidate="CustomerNameTextBox" errormessage="Please enter your name!" />
</td
But as soon as I add RequiredFieldValidator to InsertItemTemplate I get the message "Please enter your name!" immediately after start of then page.
I have not clicked on the "Insert" button - the message just appears without any click.
So how can I make sure that certain textboxes are not empty when editing or insertig ? (I have no code behind)
You can add validation group for all textboxes and for Insert button
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup(v=vs.110).aspx
I want to show some data in tool tip on mouse hover. I am using datalist in asp.net. How can I show dynamic data in tool tip.The data will be based on database tables. This is the code for my datalist and I want tool tip to be shown on item template. The tooltip data will contain 3 items viz available quantity,on hand quantity and on order quantity.
EDIT: I tried ways provided in answer of this question but they are not working.
Is there any way that I can do the same using JQuery and web service? If yes then please guide me the proper way...
<asp:DataList ID="dlvProductSpecification" runat="server" RepeatColumns="10" HeaderStyle-Font-Size="16px"
Font-Size="Smaller" OnItemCommand="dlvProductSpecification_ItemCommand" OnItemCreated="dlvProductSpecification_ItemCreated"
OnItemDataBound="dlvProductSpecification_ItemDataBound">
<ItemStyle Font-Size="Smaller" />
<HeaderStyle HorizontalAlign="Center"> </HeaderStyle>
<HeaderTemplate>
<table class="header_dl">
<tr>
<td>
SPECIFICATION HEADER
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<%--<table style="text-align: center;">--%>
<table class="style_dl" onmouseover="showtooltip()" onmouseout="hidetooltip()">
<tr>
<td class="style_dl_td">
<asp:Label ID="lblReferenceNo" CssClass="labeltitle_dl" runat="server"><%# Eval("ReferenceNo")%></asp:Label>
<br />
<asp:Label ID="Label1" runat="server"><%# Eval("Specification")%></asp:Label>
<br />
<br />
<asp:HiddenField ID="hdnSpecificationID" runat="server" Value='<%# Eval("ProductSpecificationID")%>' />
<asp:HiddenField ID="hdnSpecification" runat="server" Value='<%# Eval("Specification")%>' />
<asp:HiddenField ID="hdnReferenceNo" runat="server" Value='<%# Eval("ReferenceNo")%>' />
<asp:HiddenField ID="hdnGTIN" runat="server" Value='<%# Eval("GTIN")%>' />
<asp:HiddenField ID="hdnAvailableQty" runat="server" Value='<%# Eval("AvailableQty")%>' />
<asp:Button ID="btnAddQty" runat="server" CommandName="GetData" Text="Add" /><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
You can do it on ItemDataBound event. Try this:
protected void dlvProductSpecification_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType== ListItemType.AlternatingItem)
{
e.Item.ToolTip = "Tool Tip";
}
}
Inside your Item templates' Label put a toolTip property and bind it like this:
<asp:Label ID="Label1" runat="server" ToolTip='<%# Eval("Specification") %>'><%# Eval("Specification")%></asp:Label>
hope it works.
I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.