Asp.net Validations and controls - asp.net

how to disable the (Required attribute) in Text box in ASP.NET web forms
I want to change the web form page to another one when I click on the Login button but I cant!
please help!

For Disable Validation :
<asp:TextBox id="txt" runat="server" causesvalidation="false" /> // or reqiured=false
For Redirect :
protected void btnConfirm_Click(object sender, EventArgs e)
{
Response.Redirect("YourPage.aspx");
//or
Server.Transfer("YourPage.aspx");
}

Related

Using ASP.Net C#, DropDownList and Redirecting? Without using button event

I have a DropDownList with several options. I want the user to select an option and the page should redirect as user selected the dropdown option without using button
Set AutoPostBack="True" OnSelectedIndexChanged="YourMethodName" and in YourMethodName(){} add for Response.Redirect to certain pages dependant on the selected value.
Example:
<asp:DropDownList runat="server" AutoPostBack="True" ID="YourMethod" OnSelectedIndexChanged="YourMethod_OnSelectedIndexChanged" />
protected void YourMethod_OnSelectedIndexChanged(object sender, EventArgs e)
{
switch (YourMethod.SelectedValue)
{
case "first":
Response.Redirect("url");
break;
}
}

ASP.NET passing variables from web form to code behind

i'm developing a web application for my company, but i'm coming up against a very simple problem that i don't now how to resolve. I search a lot on internet but i can't find anything.
I need to call a code behind method from an asp.net controls passing variables.
This is the method in the code behind (file.aspx.cs):
protected void SayHello(object sender, EventArgs e, String RandomName)
{
Response.Write(PrintedString);
}
And this is a asp.net control that call the method through OnLoad event :
<asp:Label ID="Label1" runat="server" Text="Hello" OnLoad="Visibility('ciao mamma')"></asp:Label>
What's wrong with this simple thing? Where i'm wrong?
Please answer to this simple question, it's driving me crazy...Thanks.
You can pass an argument to the event handler by the following way, but please note it will work for only asp button.
<asp:Button ID="btn" runat="server" Text="Click" OnCommand="btn_Command" CommandArgument="YourArgumentValue"/>
and then in the code behind file you do like this :
protected void btn_Command(object sender, CommandEventArgs e)
{
Response.Write(e.CommandArgument);
}
It will work for only asp button because they have a onCommand attribute with them.
whenever you will click on the button this code behind file code gets executed.
Hope this helps.
You can set the value to label on Page_Load event
You can use below code.
protected void Page_Load(object sender, EventArgs e)
{
string UserName="test";
Label1.Text="Hello "+ UserName;
}
Is this what you need?
Markup:
<asp:Label ID="lbl" runat="server"><%=SayHello("foo") %></asp:Label>
Code behind:
protected string SayHello(string name)
{
return "Hello " + name;
}

How to navigate to Facebook page in asp.net

I created my own site and my own Facebook page.
I add follow me button on site.
in that user have to follow me either on facebook or on twitter.
Problem: How to navigate user to first login page and then after successfull login to my facebook page
Place image and navigate to login page on image click like following:-
<asp:Button ID="imgbtnFaceBook" CausesValidation="False" runat="server" OnClick="imgbtnFaceBook_Click"/>
protected void imgbtnFaceBook_Click(object sender, EventArgs e)
{
Response.Redirect("yourLoginPageLink");
}
On login page check credentials and redirect to facebook page like following:-
protected void btnLogin_Click(object sender, EventArgs e)
{
// check login Credentials
Response.Redirect("yourFaceBookPageLink");
}
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<asp:Button ID="imgbtnFaceBook" CausesValidation="False" runat="server" OnClick="imgbtnFaceBook_Click"/>
protected void imgbtnFaceBook_Click(object sender, EventArgs e)
{
Response.Redirect("https://www.facebook.com/yourPageLink");
}

asp.net Panel DefaultButton property not working in Android Browser?

clicking go button in android keyboard does not firing Default button. This works in iPhone
<asp:Panel runat="server" id="SubmitPanel" DefaultButton="Submit">
<asp:LinkButton ID="Submit" runat="server" OnClick="SubmitButton_Click">
Submit
</asp:LinkButton>
</asp:Panel>
Only Button and ImageButton controls are supported. The LinkButton is not supported.
Make sure that the textbox or text input object that has focus (this is required for your android keyboard to be active) is wrapped inside this Submit Panel.
Try this
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
btnSubmit_Click(sender, e);
}
}

Page Validation, Submit Button is not part of the usercontrol

I have some fields to be validated on the server (CustomValidator) which is in a separate user control and my Submit button is in the page not in the user control.
What I have done is, I placed all my Validation Methods in the code behind of my usercontrol (ascx.cs) and the page validation is in the code behind of the page (aspx.cs). I put them in the same ValidationGroupName (fields and Submit button). usercontrol (ascx) is a child of my page (aspx). I have already placed CauseValidation="true" to my Button and UseSubmitBehavior="true". The problem is it does not validate. What could be the problem in this?
Note: I cannot put the the Button to be part of the usercontrol.
Edit:
in my aspx page, click event of the button has this.Page.Validate(ValidationGroupName) and all of my validators are on the fields on a separate control (ascx) which is a child of the aspx page.
protected void Button1_Command(object sender, CommandEventArgs e)
{
if(e.CommandName.Equals("Validate", StringComparison.Ordinal))
{
this.Page.Validate("MyValidationGroup");
if(this.Page.IsValid)
{
// I'll change my View here.
}
}
}
My Button looks like
<asp:Button ID="Button1" runat="server" UseSubmitBehavior="true" CommandName="Validate"
Text="Submit" OnCommand="Button1_Command" CausesValidation="true" ValidationGroup="MyValidationGroup" />
The above snippet is located in my aspx page.
I've tried to put the same button on the ascx page, it works fine. My thought is since the ascx page is under the aspx page. When the button event on aspx page is fired (Button1), the rest of the event in ascx page is not fired. I've tried to put breakpoints on the button event and validator events, if the button of the page (aspx) is the one I click, it will not stop on my validator events, if the button on the control (ascx) I click it will stop to the validator events. What could be the remedy for this?
Have you tried calling ucName.Page.Validate(); in the button click event?
(where ucName is whatever you called the user control in the markup)
Edit: OK this simple code below works fine for me, the only way I broke the validation was setting my user control to visible = false; Can you post more of your code?
Parent page markup:
<uc:ucTest id="ucName" runat="server">
<asp:Button ID="btnTest" runat="server" Text="Test validation" onclick="btnTest_Click" />
code behind:
protected void btnTest_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
Response.Write("Hi!");
}
}
user control markup:
<asp:TextBox ID="txtDummy" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="*"
ControlToValidate="txtDummy"
onservervalidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>
code behind:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
if (txtDummy.Text.Length > 0)
{
args.IsValid = false;
}
}

Resources