The following code works except the dropdown list is not returned to its original value when the validation fails any ideas?
<tr><td valign="top" style="padding-right: 10px;">
<b>Select Account:</b>
<asp:DropDownList ID="m_lstAccounts" runat="server" CausesValidation="true" ValidationGroup="Group1"
CssClass="dropdownList" OnSelectedIndexChanged="OnAccountChange" AutoPostBack="True"></asp:DropDownList>
<br />
<script type="text/javascript">
function ConfirmDropDownValueChange(source, arguments) {
if (document.all("AccountProfileDirty").value == "1") {
arguments.IsValid = confirm("Are you sure you want to continue with out saving?");
source.
}
else {
arguments.IsValid = true;
}
}
</script>
<asp:CustomValidator ID="ConfirmDropDownValidator" runat="server"
ClientValidationFunction="ConfirmDropDownValueChange" Display="Dynamic" ValidationGroup="Group1" /> </td> </tr>
What is the random source. in the middle of your ConfirmDropDownValueChange() function? That would certainly make JavaScript fail.
Related
I'm building an asp.net/C# application that has several true/false questions, along with some multiple choice questions, in it. I've written code for this, and for some reason the onclick() event is not triggering when a checkbox is clicked. This is confirmed working in another app ; I don't understand why it isn't working here. Please see the relevant code below. I've sat this code side-by-side with the known working code, and can't find any anomalies. Does anyone have an idea why this isn't working?
<script type="text/javascript">
function ChkQuestion1(obj) {
var chk1a = document.getElementById("<%=chkFalse1.ClientID %>");
var chk1b = document.getElementById("<%=chkTrue1.ClientID %>");
if (obj.checked == true) {
chk1a.checked = false;
chk1b.checked = false;
obj.checked = true;
}
}
function ClientValidation1(source, arguments) {
var chka = document.getElementById("<%=chkFalse1.ClientID%>");
var chkb = document.getElementById("<%=chkTrue1.ClientID%>");
if (chka.checked == false && chkb.checked == false) {
arguments.IsValid = false;
}
}
</script>
<tr>
<td valign="top">1.</td>
<td width="70px" valign="top">
<asp:CheckBox ID="chkTrue1" runat="server" Text=" " onclick="ChkQuestion1(this)" />
<asp:Label ID="lblTrue1" runat="server" Text="True"></asp:Label></td>
<td width="90px" valign="top">
<asp:CheckBox ID="chkFalse1" runat="server" Text=" " onclick="ChkQuestion1(this)" />
<asp:Label ID="lblFalse1" runat="server" Text="False"></asp:Label>
</td>
<td>
<asp:Label ID="lblP1" runat="server" Text="This is a test True/False Question."></asp:Label>
<asp:CustomValidator ID="cvTwo" runat="server" ErrorMessage="Please select correct answer true/false." ToolTip="Please select correct answer true/false." ForeColor="Red" ValidationGroup="Employee" ClientValidationFunction="ClientValidation1">*</asp:CustomValidator>
</td>
</tr>
Long story short, for an old project I used ASP.Net Dynamic Data and probably did a terrible job with it. One of the Field Templates has multiple controls in it, and now I need to get at the value of one control from the FormView's Submit event because we changed the way that value is stored.
I can find the Field Template itself using FindFieldTemplate... but I can't figure out how to get to the controls inside of the template.
How can I do that without re-engineering the whole thing to pull that one field out? It would probably be more correct to re-engineer it, but this is a quick fix for a website that's going to be scrapped in a couple months.
EDIT: Was asked to show code so here it is. The FormView is pretty standard, just uses an . The Field Template actually has it's own listview and I'm controlling it's mode in codebehind. But I need to get the value of txtTitle.
Ticket_TicketMemo.ascx:
<asp:ListView ID="lvTicketMemos" DataSourceID="ldsTicketMemo"
InsertItemPosition="FirstItem" OnLoad="lvTicketMemo_Load" runat="server">
<LayoutTemplate>
<div style="overflow:auto; height:125px; width:600px;">
<table class="ListViewTable" runat="server">
<tr id="itemPlaceHolder" runat="server" />
</table>
</div>
</LayoutTemplate>
<ItemTemplate>
<tr valign="top" class='<%# Container.DataItemIndex % 2 == 0 ? "" : "Alternate" %>'>
<td><asp:DynamicControl ID="dcType" DataField="Type" runat="server" /></td>
<td><asp:DynamicControl ID="dcMemo" DataField="Memo" runat="server" /></td>
<td><asp:DynamicControl ID="dcCreateTime" DataField="CreateTime" runat="server" /></td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr valign="top">
<td colspan="3">
<asp:TextBox ID="txtTitle" Width="99%" Visible="false" OnLoad="txtTitle_Load" runat="server" /><br /><br />
</td>
</tr>
<tr valign="top">
<td colspan="3" width="600px">
<asp:TextBox ID="txtMemo" Text='<%# Bind("Memo") %>' Width="99%" OnLoad="txtMemo_Load" TextMode="MultiLine"
Rows="5" runat="server" />
<asp:RequiredFieldValidator ID="rfvMemo" Text="Must enter notes" ControlToValidate="txtMemo" runat="server" />
</td>
</tr>
</InsertItemTemplate>
I have just simulated your problem in my Dynamic Data project. Based on my research (and search) in order to get control value (not from DynamicControl value) in Dynamic Data you should implement the following method (i am using this method in my project and i have found one on Steve blog, i don't remember full link):
/// <summary>
/// Get the control by searching recursively for it.
/// </summary>
/// <param name="Root">The control to start the search at.</param>
/// <param name="Id">The ID of the control to find</param>
/// <returns>The control the was found or NULL if not found</returns>
public static Control FindControlRecursive(this Control Root, string Id)
{
if (Root.ClientID.IndexOf(Id) > 0)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
Now, my EXAMPLE.
First, my custom Insert.aspx page with FormView and EntityDataSource:
<asp:FormView runat="server" ID="FormView1" DataSourceID="DetailsDataSource" DefaultMode="Insert"
OnItemCommand="FormView1_ItemCommand" RenderOuterTable="false">
<InsertItemTemplate>
<table>
<tr valign="top">
<td colspan="3">
<asp:TextBox ID="txtTitle" Width="99%" Visible="true" runat="server" /><br />
<br />
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>
<asp:EntityDataSource ID="DetailsDataSource" runat="server" EnableInsert="true" OnInserted="DetailsDataSource_Inserted" />
Then, Inserted event of EntityDataSource:
protected MetaTable table;
protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
{
if (e.Exception == null || e.ExceptionHandled)
{
string strTitle = String.Empty;
Control CtlTitle = FormView1.FindControlRecursive("txtTitle");
if (CtlTitle != null)
{
TextBox TextBoxTitle = (TextBox)CtlTitle;
strTitle = TextBoxTitle.Text;
}
Response.Redirect(table.ListActionPath + "?" + "Department_Id=" + strTitle);
}
}
Finally, i enter the text into txtTitle, for example, 13 and then i get
This is an ASPX/CS program with Javascript.
I have inherited this code and I am trying to complete the validation which was not completed. I have found the javascript code which validates of a radio button is clicked but the aspx code does not reference it.
Radio buttons are grouped together such that when one in the group is selected, the others are unselected. But, for starters, I cannot see how to utilize this. It must have something to do with the "GroupName" that each radio button mentions.
<ItemTemplate>
<%# FormatGroup(Eval("Group").ToString()) %>
<tr>
<td> <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'><%# Eval("Question") %></asp:Label></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion4" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion3" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion2" runat="server" GroupName='<%# Eval("Question") %>' /></td>
<td align="center" width="10%"><asp:RadioButton ID="rblEvalQuestion1" runat="server" GroupName='<%# Eval("Question") %>' /></td>
</tr>
</ItemTemplate>
So how would I make use of the ClientValidationFunction method in asp for radio button groups?
Try a custom field validator.
Add this bit of JavaScript:
<script language="javascript" type="text/javascript" >
function ClientValidate(source,args)
{
if(document.getElementById("<%= rblEvalQuestion1.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion2.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion3.ClientID %>").checked || document.getElementById("<%= rblEvalQuestion4.ClientID %>").checked)
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
</script>
Add this validator to the page:
<asp:CustomValidator id="RadioButtonValidator" runat="server" Display="Dynamic" ErrorMessage="Please select an option." ClientValidationFunction="ClientValidate" OnServerValidate="ServerValidate"></asp:CustomValidator>
And this method in the code behind:
protected void ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = rblEvalQuestion1.Checked || rblEvalQuestion2.Checked || rblEvalQuestion3.Checked || rblEvalQuestion4.Checked;
}
Finally, make sure to check if
Page.IsValid
in your button for submitting the form.
Finally, if you can re-factor this into a RadioButtonList you can simply just use a RequiredFieldValidator which will make your life a lot easier to implement!
I have a checkbox in repeater control.
on changing it's bit value, it should prompt the alert message like do u wish to delete?
and if yes is selected, the onselectedchange server event should be fired.
Right now. on clicking the checkbox i am using onclick event to fire the prompt but it is not able to fire the server event.
here is my code
<script language="javascript" type="text/javascript">
function CheckedChange() {
if (confirm('Are you sure you want to delete?'))
return true;
else
return false;
}
</script>
<asp:Repeater ID="repQuestion" runat="server" onitemcommand="repQuestion_ItemCommand" onitemdatabound="repQuestion_ItemDataBound">
<ItemTemplate>
<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td>
<asp:CheckBox onclick="return CheckedChange();" ID="delete" runat="server" Checked='<%#DataBinder.Eval(Container.DataItem, "IsDeleted")%>' OnCheckedChanged="chkdelete_Click" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Work around this snippet.
The Repeater.
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td><%# Eval("Index") %></td>
<td>
<asp:CheckBox ID="delete" runat="server"
AutoPostBack="true"
Checked='<%# Convert.ToBoolean(Eval("IsDeleted")) %>'
Enabled='<%# !Convert.ToBoolean(Eval("IsDeleted")) %>'
OnCheckedChanged="chkdelete_Click"
onclick="javascript:return CheckedChange(this)"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
JavaScript
function CheckedChange(objCheckBox) {
if (confirm('Are you sure you want to delete?')) {
__doPostBack("'" + objCheckBox.id + "'", '');
return true;
}
else {
objCheckBox.checked = false;
return false;
}
}
Page_Load Event
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
//bind gridview here
DataTable dataTable = new DataTable
{
Columns = { "Index", { "IsDeleted", typeof(bool) } }
};
bool temp = true;
for (var i = 0; i < 6; i++)
{
dataTable.Rows.Add((i + 1).ToString(), temp);
temp = !temp;
}
Repeater1.DataSource = dataTable;
Repeater1.DataBind();
}
}
catch (Exception exception)
{
//Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
}
Well, I don't personally like exposing the __doPostBack but this seems to be the only way in this case.
On an unrelated side note, if you are showing deleted items, its better to show them disabled.
Try by passing the UniqueID (that is the name of the Checkbox).
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table>
<tr>
<td><%# Eval("Index") %></td>
<td>
<asp:CheckBox ID="delete" runat="server"
AutoPostBack="true"
Checked='<%# Convert.ToBoolean(Eval("IsDeleted")) %>'
Enabled='<%# !Convert.ToBoolean(Eval("IsDeleted")) %>'
OnCheckedChanged="chkdelete_Click"
onclick="javascript:return CheckedChange(this)"/>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
JavaScript
function CheckedChange(objCheckBox) {
if (confirm('Are you sure you want to delete?')) {
__doPostBack("'" + objCheckBox.name + "'", '');
return true;
}
else {
objCheckBox.checked = false;
return false;
}
}
Try this
function confirmUser(checkBox)
{
if(checkBox.childNodes[0].checked === false){
alert('Your alert message');
}
}
<asp:Repeater ID="rptSelecteduserRoles" runat="server" OnItemDataBound="rptSelecteduserRoles_OnItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="chkBox" CssClass="fieldLabel" AutoPostBack="False" onchange="confirmUser(this)"runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:Repeater>
I am working in asp.net and have radiobutton list and I want to align their text as I require.
Here is what I have currently:
I want to make them like this:
EDIT:
Secondly, when I click Ages From radiobutton, I display a div against this like:
and when I click back to All Ages radio button, I want to hide that div. But SelectedIndexChanged doesn't work second time and onwards. It only works first time.
Code of aspx:
<table>
<tr>
<td>
<asp:RadioButtonList ID="rdoAge" runat="server" RepeatDirection="Horizontal"
onselectedindexchanged="rdoAge_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Text="All Ages" Value="All Ages" Selected="True"></asp:ListItem>
<asp:ListItem Text="Ages From" Value="Ages From"></asp:ListItem>
</asp:RadioButtonList>
</td>
<div id="divAge" runat="server" visible="false">
<td>
<asp:TextBox ID="txtAgeFrom" runat="server" CssClass="textEntry2" MaxLength="3" Width="65"></asp:TextBox>
</td>
<td>
<asp:Label ID="lblTo" runat="server" Text="To"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtAgeTo" runat="server" CssClass="textEntry2" MaxLength="3" Width="65"></asp:TextBox>
</td>
</div>
</tr>
</table>
Code of cs file:
protected void rdoAge_SelectedIndexChanged(object sender, EventArgs e)
{
switch (rdoAge.SelectedValue)
{
case "All Ages":
divAge.Visible = false;
break;
case "Ages From":
divAge.Visible = true;
break;
}
}
I'll be grateful if anyone suggests something useful for this issue.
Thanks in advance.
That was the problem of missing closing tag. I must have missed a closing tag of some control. I re-added all controls with taking care of closing tags. Now it is working fine.
Thanks all for helping.
try using css sytel
<style type="text/css">
table.radioWithProperWrap input
{
float: left;
}
table.radioWithProperWrap label
{
word-wrap: break-word;
}
</style>
<asp:RadioButtonList runat="server" CssClass="radioWithProperWrap" ....>
switch (rdoAge.SelectedItem.Text)
In the Source Code, Define the Update Panel like this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
and then in the OnSelectedIndexChanged event, add
UpdatePanel1.Update();