Textboxes and Dropdown appearing when visibility set to false - asp.net

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;
}

Related

asp.net - Listbox - InsertItemTemplate RequiredFieldValidator

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

ASP.net Listview dont show current/new value inserted

I have a problem in getting new entered record in the ListView.
In my list in ItemTemplate I replaced labels with TextBox. I have also created a button which loops over all the rows in ListView and shows the AcName field in alert message. If I update it (change the text of the AcName) to something else it still shows the record which is already present before, not the new text/value I entered. Below is the code which is working correctly for me but not generating the outcome I want, which is the new value.
HTML Markup
<asp:ListView ID="ListView1" InsertItemPosition="LastItem" runat="server" >
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID") %>'></asp:Label>
</td>
<td>
<%--<asp:Label runat="server" ID="lblAcount" Text='<%#Eval("AcName") %>'></asp:Label>--%>
<asp:TextBox ID="lblAcount" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblNaration" Text='<%#Eval("Naration") %>'></asp:Label>--%>
<asp:TextBox ID="lblNaration" runat="server" CssClass="form-control" Text='<%#Bind("Naration") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblPaidAmount" Text='<%#Eval("PaidAmount") %>'></asp:Label>--%>
<asp:TextBox ID="lblPaidAmount" runat="server" CssClass="form-control" Text='<%#Bind("PaidAmount") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblDeductAmount" Text='<%#Eval("DeductionAmount") %>'></asp:Label>--%>
<asp:TextBox ID="lblDeductAmount" runat="server" CssClass="form-control" Text='<%#Bind("DeductionAmount") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="DeleteButton" cssClass="btn btn-info fa fa-trash-o" runat="server" CommandName="DeleteIt"></asp:LinkButton>
<td>
</td>
</tr>
<asp:Panel ID="Panel2" runat="server" Visible="False">
<tr>
<td>
<asp:Label ID="Label12" runat="server" Text="Credit Acount"></asp:Label>
<asp:TextBox ID="txtAcName" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
</td>
</tr>
</asp:Panel>
</ItemTemplate>
</asp:ListView>
Button handler
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For i As Integer = 0 To ListView1.Items.Count() - 1
Dim txtAcNames As TextBox = TryCast(ListView1.Items(i).FindControl("txtAcName"), TextBox)
Dim msg As String
msg = "<script language="'javascript'">"
msg += "alert('" & txtAcNames.Text & "');"
msg += "</script>"
Response.Write(msg)
Next
'txtTotal1.Text = Paid
End Sub
The above code works for me, the problem is when I enter data in the TextBox of the ListView it shows me the old values which is bound to the listview, not the new value that I enter.
what I want is it should show me the new value which I enter at runtime in the ListView TextBox.
You are alerting the txtAcName.Text at Button1_Click, but you are changing the value of lblAcount.Text at runtime. So, the old value you see is in fact from the other textbox.
Note that you have both textboxes binded to the same property <%# Bind("AcName") %>

Unable to get TextBox text in DataList

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>

Required Field Validation not working in asp.net

I have required field validation control for a radiobutton list. So if no values are selected then it gives me a error which is fine. But when i redo select something and click the button then it does't not fires the server event of the button. Once i have the validation erro then whatever i do it disable the server side event.
any ideas why is it happening my code
<div id="studysub_popul" runat="server" visible="false">
<asp:Label ID="lbl_rdb_study_popul" runat="server"
CssClass="questions"
Text="2.Select your study subjects">
</asp:Label>
<asp:RadioButtonList ID="rdb_study_popul" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged">
<asp:ListItem>Individuals</asp:ListItem>
<asp:ListItem>Population</asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="rdb_study_popul"
Display="Dynamic"
ErrorMessage="Study Subject is required"
ValidationGroup="StudySubject">
</asp:RequiredFieldValidator>
</div>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btn_s_section" runat="server"
OnClick="btn_studysubject_section_Click"
Text="Next" ValidationGroup="StudySubject"
Visible="false" />
</td>
You should add a validating group to the RadioButtonList definition too.
<asp:RadioButtonList ID="rdb_study_popul" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="rdb_study_popul_SelectedIndexChanged"
ValidationGroup="StudySubject">

ASP.NET Trying to add css class to tr in codebehind

So i have the following method
protected void isDirector_CheckedChanged(object sender, EventArgs e)
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("today");
But getting error
CS0117: 'System.EventArgs' does not contain a definition for 'Item'
EDIT :
<asp:UpdatePanel ID="UpdatePanel2"
runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:RadioButtonList ID="isDirector" RepeatDirection="Horizontal" runat="server" AutoPostBack="True" OnSelectedIndexChanged="isDirector_CheckedChanged">
<asp:ListItem Text="Yes" Value="True" selected></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:RadioButtonList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel
<ContentTemplate>
<tr runat="server" id="test">
<td>Director First Name:</td>
<td><asp:TextBox ID="DirectorfirstNametxt" runat="server" MaxLength="100" CssClass="input"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" Display="None" runat="server"
ErrorMessage="Director First Name is required." ControlToValidate="DirectorfirstNametxt"></asp:RequiredFieldValidator>
</td>
</tr>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="isDirector" EventName="SelectedIndexChanged" />
</Triggers>
I am trying to change the CSS CLASS of TR ID = "test"
Assuming you have your Checkboxes inside of a HTML-TableRow and you want to set the CSS-Class of the TR in the CheckedChanged-Event:
This is an example(note that the TR's have a runat="server"-tag):
<table>
<tr ID="TR1" runat="server">
<td>
<asp:CheckBox ID="CheckBox1" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
<tr ID="TR2" runat="server">
<td>
<asp:CheckBox ID="CheckBox2" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
<tr ID="test" runat="server">
<td>
<asp:CheckBox ID="CheckBox3" OnCheckedChanged="isDirector_CheckedChanged" AutoPostBack="true" runat="server" />
</td>
</tr>
</table>
and this is the codebehind:
protected void isDirector_CheckedChanged(object sender, EventArgs e)
{
//var row = (HtmlTableRow)((CheckBox)sender).Parent.Parent;
test.Attributes("class") = "CssClass";
}
Edit: if your tr's are runat="server" and they have unique ID's, you can access them directly
What kind of control is isDirector_CheckChanged tied to - a checkbox?
As the error says, EventArgs is the type of event you're expecting and it doesn't have an 'Item' property. Maybe you're thinking of GridView, Repeater, or other 'item-like' controls?
I'm guessing you're trying to handle the changed event of a checkbox you've put in a repeating/table control. If so, you'll need to handle a Selected or similar event for the repeater that DOES use an EventArgs-derived type with an Item property.

Resources