ASP.net, Ajax, JavaScript help - asp.net

I need help in how to set webform control proprieties in asp.net and ajax control using javascript.
I have asp page that has checkBox, TextBox, MaskedEditExtender, and RegularExpressionValidator.
I set the mask for MaskedEditExtender as Mask="(999)999-9999" and I set the ValidationExpression for RegularExpressionValidator as ValidationExpression="\d{10}".
I want to change these two properties when user checked the international checkbox to: Mask="999999999999" and as ValidationExpression="\d{12}"
Using JavaScript without interrupting with server side and when the user unchecked they get previous value so the interaction should be only in the client side.
Please help me with this and here is my code:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function pageLoad() {
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:CheckBox ID="chkIntphoneHome" runat="server" Text="Internation Code"
AutoPostBack="false"/>
<asp:TextBox ID="txtHomePhone" runat="server"></asp:TextBox>
<cc1:MaskedEditExtender ID="txtHomePhone_MaskedEditExtender" runat="server"
AutoComplete="False" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat=""
CultureDatePlaceholder="" CultureDecimalPlaceholder=""
CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True"
Mask="(999)999-9999" TargetControlID="txtHomePhone">
</cc1:MaskedEditExtender>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="RegularExpressionValidator" ControlToValidate="txtHomePhone"
ValidationExpression="\d{10}"></asp:RegularExpressionValidator>
</div>
</form>
</body>
</html>

Instead of meddling with the maskEditor/validator controls' client-side behaviour, are you OK with having 2 sets of textbox+maskExtender+validator?
Set 1 = texbox_HomePhone + maskEditExtender_HomePhone + regexValidator_HomePhone
Set 2 = texbox_IntHomePhone + maskEditExtender_IntHomePhone + regexValidator_IntHomePhone
Using the checkbox to toggle (via javascript) the display/hidding of either set.

Related

Submit button in ASP.net

I am new to .NET and I am trying out basic functionalities. I got an error when I placed submit button. Please go through the code and let me know if the Submit button syntax I used is wrong.
Code :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>My First web page</title>
</head>
<body>
<form id="form1" runat="server" >
<div style="position:absolute">
First Name :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br/>
Last Name :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button OnClick="submit" Text="submit" runat="server" />
</div>
</form>
</body>
</html>
Error is :
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.webform1_aspx' does not contain a definition for 'submit' and no extension method 'submit' accepting a first argument of type 'ASP.webform1_aspx' could be found (are you missing a using directive or an assembly reference?)
Thank You.
You have to provide the server side OnClick handler to OnClick and probably submit is not defined as handler. This MSDN Button.OnClick documentation tell how OnClick event handler is attached with button. You also need to provide the ID to your button.
Remove the OnClick attribute from button. Open the form in VS designer Double click the button in VS designer it will generate handler for you. You can find mode in How to: Create Event Handlers in ASP.NET Web Forms Pages
After generating event handler you would have something like.
Code behind
void yourButtonId_Click(Object sender, EventArgs e)
{
}
HTML (aspx)
<asp:Button ID="yourButtonId" OnClick="yourButtonId_Click" Text="submit" runat="server" />
<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Your name is " & txt1.Text
End Sub
</script>
<!DOCTYPE html>
<html>
<body>
<form runat="server">
Enter your name:
<asp:TextBox id="txt1" runat="server" />
<asp:Button OnClick="submit" Text="Submit" runat="server" />
<p><asp:Label id="lbl1" runat="server" /></p>
</form>
</body>
</html>

Dynamically Loaded Existing Panel cause issue of PostBack

At first See the Code below.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="test.aspx.vb" Inherits="BLL.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlCustom" runat="server"></asp:Panel>
<asp:Panel ID="pnlDiv1" runat="server">
<asp:DropDownList ID="ddl_Status" runat="server"></asp:DropDownList>
</asp:Panel>
<asp:Panel ID="pnlDiv2" runat="server">
<asp:DropDownList ID="ddlImg1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlImg1_SelectedIndexChanged"></asp:DropDownList>
</asp:Panel>
<asp:Panel ID="pnlDiv3" runat="server">
<asp:DropDownList ID="ddlImg2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlImg2_SelectedIndexChanged"></asp:DropDownList>
</asp:Panel>
</div>
</form>
</body>
</html>
I want to hide and show the panel in different order as per the requirement. i don't want to change the content of the panel. pnlDiv1,pnlDiv2,pnlDiv3 will bind in pnlCustom.
Now, In Page_Load Event i am setting all panel style to "display:none" except "pnlCustom" and dynamically adding pnlDiv1/pnlDiv2/pnlDiv3 in "pnlCustom" and set its style to "display:inline".
In my application problem is If i change the value of "ddlImg1" the page gets postback and all the value reset. this is same for the "ddlImg2".
Note:Values are bind in dropdown in Page_Load if its not postback.. so, anyone can explain what's an issue?
This could be a possible issue with the ViewState.
The following link might be helpful to you:
ASP.NET DropDownList not retaining selected item on postback

asp:Label with nested control disappears on setting CssClass

Is it improper to nest another control within asp:Label?
When I have another control (e.g., a button) inside my asp:Label, and I try to change CssClass on the label during an event handler, the entire label seems to disappear. I'll be grateful if someone can help me find what I might be doing wrong.
Bad1.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Bad1.aspx.cs" Inherits="Bad1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Badness?</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" Text="check and uncheck me" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />
<br />
<br />
<asp:Label ID="Label1" runat="server" > See me now? <asp:Button runat="server" /> </asp:Label>
</form>
</body>
</html>
Bad1.aspx.cs:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Bad1 : System.Web.UI.Page
{
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
var lab = Page.FindControl("Label1") as Label;
lab.CssClass = "foo";
}
}
Symptom: Page displays correctly at first, and after first click on "check and uncheck me". But when I click on the checkbox a second time, the label ("See me now") disappears, never to come back. If I remove the button from within the label, there's no problem. So, is the nesting what's incorrect?
I would look into using a Panel instead of a Label.
<asp:Panel ID="Panel1" runat="server" CssClass="someStyle">
<asp:Button ID="Button1" runat="server" Text="Hi"></asp:Button>
</asp:Panel>
If you're looking to style the button, then you should apply your CssClass to the button directly. Use a Panel if you're trying to apply a style to that region/block.

Timer issue on Asp.Net

I have encountered a serious issue in asp.net which is stopping me going further.
The issue is due to server side timer control. The following is the scenario,
When main page loads , timer will start automatically and it should keep run throughout the application. My page contains Infragistics controls.
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" onload="UpdatePanel1_Load">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Enabled="true" Interval="10000" />
</ContentTemplate>
</asp:UpdatePanel>
<ig:WebDataMenu ID="WebDataMenu1" runat="server" OnItemClick="wdmenu_Context_ItemClick">
<Items>
<ig:DataMenuItem Key="0" Text="Add" >
</ig:DataMenuItem>
<ig:DataMenuItem Key="1" Text="Edit">
</ig:DataMenuItem>
</Items>
<ClientEvents ItemClick="wdmenu_Context_ItemClick" />
</ig:WebDataMenu>
</div>
</form>
</body>
If i open model dialog and after 2 to 3 sec , i am closing the model dialog... the wdmenu_Context_ItemClick server side event (which is for opening model dialog ) is firing two times...
Also on click of that button , i am opening model dialog using java script which is shown below
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function wdmenu_Context_ItemClick(sender, eventArgs)
{
var win = window.showModalDialog("Default2.aspx", "Dialog", 'center:yes;resizable:no;dialogHeight:500px;dialogwidth:660px');
}
</script>
</head>
Please help me regarding this....

Control scope within Repeater, with and without UpdatePanel

Why does the following give me a compilation error for line B (Label2, outside UpdatePanel) but not for line A (Label1, inside UpdatePanel)? I would have expected both lines to give an error since both controls are within the same Repeater and should therefore not be directly accessible outside the repeater, since there is no one unique instance.
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Label1.ClientID; // Line A - compiles fine
Label2.Text = Label2.ClientID; // Line B - "The name 'Label2' does not exist in the current context"
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label2" runat="server" Text="Label2" />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
I'm betting if you comment out line B you'll get a run-time error on execution. Label1 is going to be a null reference.
When you create controls in the ASPX page Visual Studio tries to help you out by adding the controls to the code behind in the designer file which extends the class for the page. In this case it's adding one when it shouldn't be.
Short answer is it's a bug. You should submit it but it shouldn't be a blocking issue.
Real question is why are you creating multiple update panels in a repeater ? Put one outside of the repeater and call it good. Or if you just want to refresh some text dont use an update panel, use a call back with some client side script to set the dom element. Check out this http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/
Neither of those is proper anyway. You shouldn't be trying to directly reference a control that's contained within the ItemTemplate.
If you want to modify those Labels at runtime, you should be using OnItemDataBound and FindControl. To "find" the Label in the UpdatePanel, you'll need to use UpdatePanel.ContentTemplateContainer.FindControl().

Resources