I am new to C# and I am having a hard time solving an error. When I log in, I get the error:
Object reference not set to an instance of an object.".
I know this means the table does not exist, but how do I correct it? My Database contains 4 tables and the table I am referring to is the third table in the database(I don't know if this will help). Please tell me if you need more information. I think the problem is with the loop somewhere, because I get no syntax errors.
My login code in my web user control(just for background information)
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<% if (Session["Patient"] == "Logged In")
{ %>
<asp:Button ID="Logout" runat="server" Text="Logout" onclick="Logout_Click" /> <br /><br />
<a>Continue to:</a> <asp:LinkButton ID="homepage" runat="server" PostBackUrl="~/home.aspx">Home page</asp:LinkButton>|
<asp:LinkButton ID="book_appointment" runat="server" PostBackUrl="~/appointments.aspx">Book an Appointment</asp:LinkButton>
<asp:HyperLink ID="HyperLink1" runat="server" Visible="false">HyperLink</asp:HyperLink>
<% }
else
{ %>
<br />
<a>Username:
<asp:TextBox ID="username" runat="server"></asp:TextBox> <br /><br />
Password:
<asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox> </a><br /><br />
<asp:Button ID="Login" runat="server" Text="Login" onclick="Login_Click" />
<asp:LinkButton ID="linkRegister" runat="server" PostBackUrl="~/register.aspx">Register</asp:LinkButton><br /><br />
<a><asp:Literal ID="literal1" runat="server"></asp:Literal></a><br /><br />
<% } %>
This is the code behind on my login control:
protected void Login_Click(object sender, EventArgs e)
{
cQuery record = new cQuery(); //declare instance
record.Sqlstring = "SELECT username, password FROM dbo.patient_details"; //what to get from database -- sql query
DataSet data = record.SelectStatement(); //put info into dataset
DataTable table = data.Tables[0];
foreach (DataRow row in table.Rows)
{
if (row[0].ToString() == username.Text)
{
if (row[1].ToString() == password.Text)
{
Session["Patient"] = row[0];
Response.Redirect("myaccount.aspx");
return;
}
}
} }
I know this means the table does not exist
No, it doesn't. if the table did not exist yo would get a SQL error.
What it means (assuming it's occurring in the method you posted, which you haven't confirmed) is that one of the variables below is null:
row[0]
row[1]
username
password
Which I doubt unless you have a record with a null password, or the username and password controls are created dynamically. Look at the stack trace of the exception and you should be able to pinpoint where the error occurs.
Related
I'm having trouble with my ASP.NET Webform. All my pages are working perfectly with the master page, but for some reason my ProductItem page isn't accepting the master page.
Heres my code for ProductItem:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ProductItem.aspx.cs" Inherits="mumsBoutique.ProductItem" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<link href="Content/websiteStyle.css" rel="stylesheet" type="text/css" />
<h1><center>Product Details</center></h1>
<div class="row">
<asp:Label ID="productNameLabel" runat="server" Text="Product"></asp:Label>
<br />
<asp:Label ID="productItemNumber" runat="server" Text="ProductCode" CssClass="ProductItem_productNumber"></asp:Label>
</h3>
<h3>
<asp:Label ID="productCost" runat="server" Text="£0.00"></asp:Label>
</h3>
<p>
<asp:Image ID="productImage" runat="server" />
</p>
<h4>Product summary</h4>
<p>
<asp:Label ID="productDescription" runat="server" Text=""></asp:Label>
</p>
<!-- The right colum displays a text box for user to add items to the basket -->
<h3>Buy Item</h3>
<p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="text-danger" />
<p>
Quantity: <asp:TextBox ID="itemQuantity" runat="server" Text="1"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvItemQuantity" runat="server" ErrorMessage="Quantity is required" Text="Please specify the quantity required" ControlToValidate="itemQuantity" CssClass="text-danger" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="cvItemQuantity" runat="server" ErrorMessage="Invalid quantity value" Text="Please specify a valid numeric quantity value" ControlToValidate="itemQuantity" Type="Integer" Display="Dynamic" CssClass="text-danger" Operator="DataTypeCheck"></asp:CompareValidator>
<asp:RangeValidator ID="rvItemQuantity" runat="server" ErrorMessage="Invalid quantity value" Text="Please specify a quantity value between 1 and 10" ControlToValidate="itemQuantity" Type="Integer" MinimumValue="1" MaximumValue="10" Display="Dynamic" CssClass="text-danger"></asp:RangeValidator>
</p>
<p>
<asp:Button ID="btnAddToBasket" runat="server" Text="Add to basket" CssClass="btn btn-primary btn-lg" OnClick="btnAddToBasket_Click" />
</p>
</div>
</asp:Content>
But I don't think the problem is on this particular page. It seems the problem occurs when I run the page from the hyperlink on the Bags.aspx page
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Bags.aspx.cs" Inherits="mumsBoutique.Bags" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<link href="Content/mumsBoutique.css" rel="stylesheet" type="text/css" />
<h1><center>Bags</center></h1>
<div class="row">
<asp:DataList ID="DataList1" runat="server" CssClass="table ProductsDataList" DataKeyField="productID" DataSourceID="SqlDataSource1" RepeatColumns="4" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="200px" Width="180px" ImageUrl='<%# Eval("Picture", "~/{0}") %>' />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("productID", "ProductItem.aspx/{0}") %>' Text='<%# Eval ("productName") %>'></asp:HyperLink>
<br />
Item Number:
<asp:Label ID="ItemNumberLabel" runat="server" Text='<%# Eval("productNumber") %>' />
<br />
<asp:Label ID="CostLabel" runat="server" Text='<%# Eval("cost", "{0:C}") %>' />
<br />
<br />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT Items.productID, Items.productName, Items.productNumber, Items.picture, Items.cost, Items.categoryID, ItemCategories.Name AS Expr1 FROM ItemCategories INNER JOIN Items ON ItemCategories.categoryID = Items.categoryID WHERE (Items.categoryID = 2)"></asp:SqlDataSource>
</div>
</asp:Content>
Would really appreciate any help, been stuck for hours!
Update: Here are some screen shots of the problem I mean.
This is the Bags.aspx Page When you click on the hyperlink it takes you to the following
ProductItem.aspxAs can be seen the master page isn't being used for some reason. Yet in all other pages it works perfectly fine.
Here is the ProductItem.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.FriendlyUrls;
using mumsBoutique.Models;
namespace mumsBoutique
{
public partial class ProductItem : System.Web.UI.Page
{
int _productID = 0;
protected void Page_Load(object sender, EventArgs e)
{
IList<string> segments = Request.GetFriendlyUrlSegments();
if (segments != null && segments.Count > 0)
{
// Convert the item ID to an integer and store it in the field _itemID
int.TryParse(segments[0], out _productID);
}
// If no item ID is specified, go back to the products page
if (_productID == 0)
{
Response.Redirect("Products");
}
// If _itemID is a valid value, search the database for the matching item
if (!IsPostBack && _productID > 0)
{
// open the connection to the database
using (ApplicationDbContext context = new ApplicationDbContext())
{
string sql = "SELECT * FROM Items WHERE productId = #productId";
System.Data.SqlClient.SqlParameter parameter = new System.Data.SqlClient.SqlParameter("#productId", _productID);
Item foundItem = context.Database.SqlQuery<Item>(sql, parameter).FirstOrDefault();
productNameLabel.Text = foundItem.productName;
productItemNumber.Text = foundItem.productNumber;
productCost.Text = foundItem.cost.ToString("c");
productImage.ImageUrl = "~/" + foundItem.picture;
productDescription.Text = foundItem.productDecription;
}
}
}
protected void btnAddToBasket_Click(object sender, EventArgs e)
{
// Check if page is valid
if (Page.IsValid)
{
int qty = 0;
// Get the quantity from the input field
qty = int.Parse(itemQuantity.Text);
// Check if item already exist in basket
OrderItem basketItem = ShopApp.Instance.GetBasketItem(_productID);
if (basketItem == null)
{
// item does not currently exist in basket, add new one
using (ApplicationDbContext context = new ApplicationDbContext())
{
// find the product item (pItem) with ItemId matching what is stored in the variable field (_itemId)
Item productItem = context.Items.FirstOrDefault(i => i.productID == _productID);
basketItem = new OrderItem()
{
productId = _productID,
Quantity = qty,
Price = double.Parse(productCost.Text, System.Globalization.NumberStyles.Currency),
Item = productItem
};
ShopApp.Instance.BasketItems.Add(basketItem);
}
}
else
{
// item already exists in basket, increase the quantity
basketItem.Quantity += qty;
}
((SiteMaster)Master).UpdateBasket();
}
}
}
}
It looks like you are trying to pass the productID as a parameter, so try changing NavigateUrl='<%# Eval("productID", "ProductItem.aspx/{0}") %>'
to
NavigateUrl='<%# Eval("productID", "ProductItem.aspx?productID={0}") %>'
Also, you need to move your CSS links to the appropriate place in your master page - you should not be linking to CSS style sheets from within a page when using a master page, and certainly not from within a ContentPlaceHolder section as you are not overriding any styles. This is more than likely the cause of your problem.
UPDATE
I've taken a look at your project and found the following problems, just by eyeballing it (can't run it due to diff DB version, etc):
The markup in your master page is badly formed. You are basically doing everything in the head section because you have no body section.
The link to your style sheet in the master page is outside of the
HTML document - it should be within the head section. Here is how structure of your master page should look:
<%# Master Language="C#" AutoEventWireup="true"
CodeBehind="Site.master.cs" Inherits="mumsBoutique.SiteMaster" %>
<!DOCTYPE html>
<html lang="en">
<head runat="server">
<meta charset="utf-8" />
<link href="Content/websiteStyle.css" rel="stylesheet"
type="text/css" />
</head>
<body>
<form id="form1" runat="server">
...
</form>
</body>
Remove the superfluous CSS links from your Bags and ProductItem
pages - when you have fixed the CSS link in the master page, that is
all you should need; you're obviously not overriding anything as you are specifying the same style sheet throughout.
I have seen many resources on SO that say that I can use following syntax to pass value to CommandArguement of `LinkButton'
<%forearch(var comment in Comments){%>
<asp:LinkButton ID="del" CommandArguement='<%= comment.CommentId%>' onCommand="delete_click" Text="Delete"/>
<%}%>
But when I write this in my ascx file and click on the link the value passed to command argument is "<%=comment.CommentId%>" instead of commentId itself. Please guide what am I doing wrong?
Edit 1
based on answers and comments, I have moved to use repeater instead of foreach and plain code. Here is the code I have come up with
<asp:Repeater ID="commRepeater" SelectMethod="GetPageComments" runat="server">
<ItemTemplate>
<p>
<%#Eval("Comment") %>
<%if(Page.User.Identity.IsAuthenticated && Page.User.Identity.GetUserId() == Eval("UserId")){ %>
<span>
<asp:LinkButton Text="Edit" runat="server" ID="EditLink" CommandArgument='<%#Eval("CommentId")%>' OnClick="Update_Comment" />
<asp:LinkButton Text="Delete" runat="server" ID="DeleteLink" CommandArgument='<%#Eval("CommentId")%>' OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" OnCommand="Delete_Comment" />
</span>
<%} %>
</p>
</ItemTemplate> </asp:Repeater>
you can see that I am trying to show the edit and delete links if user is logged in and his Id matches with user who commented but it tells me that I can on use Eval in databound controls. how would I hide/show edit/delete links conditionally within repeater
You could simply use codebehind, for example in Page_Load:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
del.CommandArgument = comment.CommentId;
}
}
Maybe a better approach would be to use the Comments-collection(which seems to be a list or array of a custom class) as DataSource of a Repeater(or other web-databound control). Then you can add the LinkButtons to the Itemtemplate.
You can then either use ItemCreated or ItemDataBound events of the repeater in codebehind or inline ASP.NET tags to bind the CommandArgument.
For example:
CommandArguement='<%# DataBinder.Eval( Container.DataItem, "CommentId" ) %>'
What you are doing currently is not recommended and is highly error prone. You can easily achieve this with ASP.NET Repeater control like this:-
<asp:Repeater ID="MyRepeater" runat="server">
<ItemTemplate>
<asp:LinkButton ID="del" CommandArguement='<%# Eval("CommentId") %>'
OnCommand="del_Command" Text="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
In Page_Load simply bind it:-
if (!Page.IsPostBack)
{
MyRepeater.DataSource = CommentsRepository();
MyRepeater.DataBind();
}
Or Else if you are have ASP.NET 4.5 then use strongly type Data Bound controls like this:-
<asp:Repeater ID="MyRepeater" runat="server" ItemType="MyNamespace.Comment"
SelectMethod="MyRepeater_GetData">
<ItemTemplate>
<asp:LinkButton ID="del" CommandArguement='<%# Item.CommentId %>'
OnCommand="del_Command" Text="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
And you method in code behind should be something like this(just for Demo):-
public IEnumerable<MyNamespace.Comment> MyRepeater_GetData()
{
return new List<Comment>
{
new Comment { CommentId =1, Name= "foo"},
new Comment { CommentId =2, Name= "bar"},
};
}
I have 3 types of validators:
It is part of "VG1" validation group
It is part of "VG2" validation group
It is not part of any validation groups
I have two buttons, B1 and B2.
I would like to validate B1.Click if and only if all validators of the first and third type successfully validated the controls associated to them.
I would like to validate B2.Click if and only if all validators of the second and third type successfully validated the controls associated to them.
Is this possible in ASP.NET? If so, can you tell me how can I do this or where could I read something which would enlighten me in this question?
EDIT:
function isValidButton1()
{
var VG1 = Page_ClientValidate("VG1");
var empty = Page_ClientValidate("");
return VG1 && empty;
}
This works well, however, if VG1 is invalid, then the messages will disappear, because of the validation of the empty group. Is there a solution to show all validation error messages? Thank you.
EDIT2:
function isValidSaveAsClosed()
{
Page_ClientValidate("");
Page_ClientValidate("VG1");
var groups = [];
groups[0] = undefined;
groups[1] = "VG1";
var valid = true;
for (var f in Page_Validators)
{
if (jQuery.inArray(Page_Validators[f].validationGroup, groups) >= 0)
{
ValidatorValidate(Page_Validators[f]);
valid = valid && Page_Validators[f].isvalid;
}
}
return valid;
}
The function above solves my problem.
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />
<script type="text/javascript">
function Validate()
{
var isValid = false;
isValid = Page_ClientValidate('VG1');
if (isValid)
{
isValid = Page_ClientValidate('VG2');
}
return isValid;
}
</script>
try this....hope it will help
Yes a button can validate more then one validation groups.
Inside the button_click you can validate the groups as
Page.Validate("VG1");
Page.Validate("VG2");
if (Page.IsValid)
{
// Page is valid so proceed..!!
}
I am adding another answer since adding to my last existing answer would make the answer too big for anyone to read.
In this answer, I have expanded on my last answer so multiple validation groups are automatically hooked up both on client-side as well as server-side. This means you do not need to call Page_ClientValidate("group1,group2") in JavaScript onclick event of button, since it will occur automatically. Also, the server-side validation for multiple groups will happen automatically.
The markup and code-behind for this is given below. You can try the aspx code that I have provided and test it in a website project. To test if automatic server-side validation occurs, you must set EnableClientScript="false" for each of the three validators.
Explanation of approach for automatic validation of multiple groups
If you want to implement multiple validation groups, the following steps need to done in your aspx page. Make sure that in your markup you mention a comma-delimited list of validation groups for ValidationGroup property of button control if you need to validate multiple groups at a time.
you need to override the JavaScriptmethod IsValidationGroupMatch by adding JavaScript to end of your aspx page (The code for this override is given at end of markup code below and you can copy/paste it into your aspx page); this is a standard method provided by ASP.Net validation framework.
you need to hookup the button with multiple validation groups for client-side validation since this is NOT done automatically by ASP.Net; for this, you have to call the method HookupValidationForMultipleValidationGroups in code-behind in Page_Load event for each button that has multiple validation groups.(you can copy/paste this method given in second-code snippet into the code-behind of your aspx page)
you need to override the server-side method Validate to add functionality for multiple validation groups since this is missing in ASP.Net.(you can copy/paste this method given in second-code snippet into the code-behind of your aspx page)
Markup of aspx with multiple validation groups for a button
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MultipleValidationGroupsByOneButton.aspx.cs" Inherits="MultipleValidationGroupsByOneButton" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
TextBox1 :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="TextBox1 needs input" ControlToValidate="TextBox1" ForeColor="Red" ValidationGroup="group1"></asp:RequiredFieldValidator>
<br />
<br />
TextBox2 :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="TextBox2 needs input" ControlToValidate="TextBox2" ForeColor="Red" ValidationGroup="group2"></asp:RequiredFieldValidator>
<br />
<br />
TextBox3 :
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="TextBox3 needs input" ControlToValidate="TextBox3" ForeColor="Red" ValidationGroup="group3"></asp:RequiredFieldValidator>
<br />
<br />
</div>
<asp:Button ID="btnMultipleValidationGroups" runat="server" Text="Validate group1 and group2" ValidationGroup="group1,group2" OnClick="btnMultipleValidationGroups_Click" />
<asp:Button ID="btnGroup1" runat="server" Text="Validate only group1" ValidationGroup="group1" OnClick="btnGroup1_Click" />
<asp:Button ID="btnGroup2" runat="server" Text="Validate only group2" ValidationGroup="group2" OnClick="btnGroup2_Click" />
<asp:Label ID="lblMessage" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
<script type="text/javascript">
window["IsValidationGroupMatch"] = function (control, validationGroup) {
if ((typeof (validationGroup) == "undefined") || (validationGroup == null)) {
return true;
}
var controlGroup = "";
var isGroupContained = false;
if (typeof (control.validationGroup) == "string") {
controlGroup = control.validationGroup;
var controlGroupArray = [];
if (validationGroup.indexOf(",") > -1) {
controlGroupArray = validationGroup.split(",");// validationGroup.split(",");
}
for (var i = 0; i < controlGroupArray.length; i++) {
if (controlGroupArray[i].trim() == controlGroup.trim()) {
isGroupContained = true;
}
}
}
return (controlGroup == validationGroup || isGroupContained);
}
</script>
</form>
</body>
</html>
Code-behind of above aspx page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MultipleValidationGroupsByOneButton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//always call this method in Page Load event for each button with multiple validation groups
HookupValidationForMultipleValidationGroups(btnMultipleValidationGroups);
}
//the method below will automatically hook up a button with multiple validation groups for client-side validation
private void HookupValidationForMultipleValidationGroups(IButtonControl button)
{
if (!Page.IsPostBack && button.ValidationGroup.Contains(","))
{
//hook up validation on client-side by emitting the appropriate javascript for onclick event of the button with multiple validation groups
PostBackOptions myPostBackOptions = new PostBackOptions((WebControl)button);
myPostBackOptions.ActionUrl = string.Empty;
myPostBackOptions.AutoPostBack = false;
myPostBackOptions.RequiresJavaScriptProtocol = true;
myPostBackOptions.PerformValidation = true;//THIS true value hooks up the client-side validation
myPostBackOptions.ClientSubmit = true;
myPostBackOptions.ValidationGroup = button.ValidationGroup;
// Add postback script so cleint-side validation is automatically hooked up for control with multiple validation groups
((WebControl)button).Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(myPostBackOptions));
}
}
//Override default Validate method so server-side validation of buttons with multiple validation groups occurs automatically
public override void Validate(string validationGroup)
{
if (validationGroup.Contains(","))
{
string[] validationGroups = validationGroup.Split(",".ToCharArray());
foreach (string group in validationGroups)
{
Page.Validate(group);
}
}
base.Validate(validationGroup);
}
protected void btnMultipleValidationGroups_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMessage.Text = "Button with multiple validation groups was clicked";
}
}
protected void btnGroup1_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMessage.Text = "Button with Group1 validation group was clicked";
}
}
protected void btnGroup2_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
lblMessage.Text = "Button with Group2 validation group was clicked";
}
}
}
A simpler approach that does not involve writting any aditional code is to simply replicate the validators wherever they are needed, as shown in the sample below:
<div>
<asp:TextBox ID="txt1" runat="server" placeholder="field1" />
<asp:RequiredFieldValidator runat="server" ID="req1a" ControlToValidate="txt1" ValidationGroup="group1" Display="Dynamic" />
<asp:RequiredFieldValidator runat="server" ID="req1b" ControlToValidate="txt1" Display="Dynamic" />
</div>
<div>
<asp:TextBox ID="txt2" runat="server" placeholder="field2" />
<asp:RequiredFieldValidator runat="server" ID="req2" ControlToValidate="txt2" />
</div>
<!-- Validate validators with empty ValidationGroup -->
<asp:Button runat="server" ID="btn1" Text="Validate All" />
<!-- Validate group1 validators -->
<asp:Button runat="server" ID="btb2" Text="Validate Group 1" ValidationGroup="group1"/>
First button will validate both textboxes, even though they are in diferent groups, while the second one will only validate the first textbox.
You can use Page_ClientValidate(validationgroup) function to validate a validation grouop.
like
function Validate(vgroup) {
return Page_ClientValidate(vgroup);
}
You can try
<asp:Button ID="B1" runat="server" Text="Button"
OnClientClick="return Validate('VG1') && Validate() " />
<asp:Button ID="B2" runat="server" Text="Button"
OnClientClick="return Validate('VG2') && Validate() " />
This is an old post but I had the same issue recently and the way I solved it is as explained below.
You can copy the code given in this post into your ASP.Net website project and test it for yourself. The first button i.e. the one on left is validating three validation groups at same time unlike the other two buttons.
Just include the JavaScript given at end of aspx page in code below, in which I have overridden the validation function IsValidationGroupMatch so multiple groups can be validated. Then, if you want to validate multiple groups on client-side, you must call the function Page_ClientValidate("group1,group2,group6,group5") to which you simply pass a comma-delimited list of validation groups.
(NOTE: But remember that by using this approach you can validate multiple validation groups on client-side only. This does NOT automatically also validate multiple groups on server-side. You must call the API function of Page_ClientValidate on client-side since multiple groups validation will not get automatically hooked up by ASP.Net framework.)
Sample aspx page code that allows validating multiple groups at same time on client-side
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MultipleValidationGroupsByOneButton.aspx.cs" Inherits="MultipleValidationGroupsByOneButton" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
TextBox1 :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="TextBox1 needs input" ControlToValidate="TextBox1" ForeColor="Red" ValidationGroup="group1"></asp:RequiredFieldValidator>
<br />
<br />
TextBox2 :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="TextBox2 needs input" ControlToValidate="TextBox2" ForeColor="Red" ValidationGroup="group2"></asp:RequiredFieldValidator>
<br />
<br />
TextBox3 :
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="TextBox3 needs input" ControlToValidate="TextBox3" ForeColor="Red" ValidationGroup="group3"></asp:RequiredFieldValidator>
<br />
<br />
</div>
<asp:Button ID="btnMultipleValidationGroups" runat="server" Text="Validate group1 and group2" onclientclick="if(!Page_ClientValidate('group1,group2,group3')) { return false;}" />
<asp:Button ID="btnGroup1" runat="server" Text="Validate only group1" ValidationGroup="group1" />
<asp:Button ID="btnGroup2" runat="server" Text="Validate only group2" ValidationGroup="group2" />
<script type="text/javascript">
window["IsValidationGroupMatch"] = function (control, validationGroup) {
if ((typeof (validationGroup) == "undefined") || (validationGroup == null)) {
return true;
}
var controlGroup = "";
var isGroupContained = false;
if (typeof (control.validationGroup) == "string") {
controlGroup = control.validationGroup;
var controlGroupArray = [];
if (validationGroup.indexOf(",") > -1) {
controlGroupArray = validationGroup.split(",");
}
for (var i = 0; i < controlGroupArray.length; i++) {
if (controlGroupArray[i].trim() == controlGroup.trim()) {
isGroupContained = true;
}
}
}
return (controlGroup == validationGroup || isGroupContained);
}
</script>
</form>
</body>
</html>
In my master page, on the menu there is an icon which uses Jquery to slide, showing 2 textboxes (username, password) for users to enter & 1 submit button. My idea is that after submitting, I get values of these 2 fields to assigned it to an invisible Login Control in my MasterPage, then validate automatically.
I could get values and assign but problem is I don't know how to trigger the Login button in Login Control (how to force it to process information)? the DataBind() func doesn't work
Master.master
<div id="login">
<p>
<asp:Login ID="Login2" runat="server" DestinationPageUrl="~/Index.aspx" LoginButtonStyle-CssClass="button_login"
TextBoxStyle-CssClass="input-name" Visible="false">
</asp:Login>
<asp:TextBox ID="inputUser" CssClass="input-name" Text="Username" runat="server"></asp:TextBox>
<asp:TextBox ID="inputPassword" CssClass="input-pass" Text="Password" runat="server"
TextMode="Password"></asp:TextBox>
<asp:Button ID="btn_login" CssClass="button_login" runat="server" OnClick="triggerLogin" />
</p>
</div>
Main.master.cs:
protected void triggerLogin(object sender, EventArgs e)
{
Login2.UserNameLabelText = inputUser.Text;
Login2.PasswordLabelText = inputPassword.Text;
Login2.DataBind();
}
Actually I already have a login page which processes individually, is it possible to pass information to that page to process?
You can add your username and password int o a session.
Something like:
Session["user"] = inputUser.Text;
Session["pass"] = inputPassword.Text;
By using that you can access the username,password in your pages.
var myusername = Session["user"].ToString();
var mypassword= Session["pass"].ToString();
Check this on MSDN:
ASP.NET State Management Overview
How to: Pass Values Between ASP.NET Web Pages
Regards
Yeah, I've found out the solution, without having to use Login Control. I can do it manually.
<asp:TextBox ID="inputUser" CssClass="input-name" Text="Username" runat="server"></asp:TextBox>
<asp:TextBox ID="inputPassword" CssClass="input-pass" Text="Password" runat="server"
TextMode="Password"></asp:TextBox>
<asp:Button ID="btn_login" CssClass="button_login" runat="server" OnClick="triggerLogin" />
Codebehind:
protected void triggerLogin(object sender, EventArgs e)
{
TextBox txtbxInputUser = (TextBox)Page.Master.FindControl("inputUser");
TextBox txtbxInputPass = (TextBox)Page.Master.FindControl("inputPassword");
Label samplelabel1 = (Label)Page.Master.FindControl("sampleLabel1");
if (System.Web.Security.Membership.ValidateUser(txtbxInputUser.Text, txtbxInputPass.Text))
{
System.Web.Security.FormsAuthentication.SetAuthCookie(txtbxInputUser.Text, false);
}
else
{
Response.Redirect("Login.aspx?Attempt=wrong");
}
}
I'm trying to create a draggable list to change the order some pictures are displayed in a product page. I wan't to be able to do this in the product edit page (same place where I add the pics and their description). So, when creating I have nothing inserted on the database and since the AJAX toolkit reorderlist only works with a datasource I was specifying the list as the source of the reorderlist and calling the databind method. In the item template of the reorder list I have a textbox to edit the pic description and a img that displays the photo. I can drag the items and the list gets reordered, however when I click save I can't get the updated text on the textbox and the order property on the picture doesn't get updated. I've tried manually getting the items in the reorderlist but they're always null even though the list shows 20 items the DataItem is empty. I've enabled viewstate and didn't help either.
Here's my code:
<ajaxToolkit:ReorderList ID="rdlPhotos" runat="server" SortOrderField="PhotoOrder" AllowReorder="true" PostBackOnReorder="true" ClientIDMode="AutoID" EnableViewState="true" ViewStateMode="Enabled">
<ItemTemplate>
<div>
<%--<eva:PhotoView ID="iPV" runat="server" Photo='<%# Container.DataItem %>' />--%>
<asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# string.Format("http://eva-atelier.net/sparkle{0}", Eval("Path").ToString().Substring(1)) %>' Width="150" Height="150" />
<div class="formGrid">
<label class="formLabel">Title</label>
<asp:TextBox ID="txtTitle" runat="server" Text='<%#Bind("FileTitle") %>' />
<br />
<label class="formLabel">Description</label>
<asp:TextBox ID="txtDescription" runat="server" Text='<%#Bind("FileDescription") %>' />
<br />
</div>
<p>
<asp:Button ID="btnRemove" runat="server" Text="Remover" />
</p>
</div>
</ItemTemplate>
<ReorderTemplate>
<asp:Panel ID="pnlReorder" runat="server" />
</ReorderTemplate>
<DragHandleTemplate>
<div style="width:20px;height:20px;background-color:Red"></div>
</DragHandleTemplate>
</ajaxToolkit:ReorderList>
And below the C# code:
private void AddPhotosView()
{
if (_currentProduct.Photos != null && _currentProduct.Photos.Count > 0)
{
rdlPhotos.DataSource = _currentProduct.Photos;
rdlPhotos.DataBind();
}
}
I'm new to Asp.net I come from a large WPF background, sorry if I'm making basic question :)
Thanks
For updating order of ReorderList items add your handler for it's OnItemReorder event. In this case your handler may looks like this:
protected void ReorderHandler(object sender, ReorderListItemReorderEventArgs e)
{
var movedPhoto = _currentProduct.Photos[e.OldIndex];
_currentProduct.Photos.RemoveAt(e.OldIndex);
_currentProduct.Photos.Insert(e.NewIndex, movedPhoto);
_currentProduct.Photos.Save();
}
For updating FileTitle and FileDescription of single Photo it is easy to use OnUpdateCommand event of ReorderList and a button with attribute CommandName="Update" for each Photo.
And for updating all Photos at once just iterate through rdlPhotos.Items in next manner:
protected void SaveAllHandler(object sender, EventArgs e)
{
foreach (var riItem in rdlPhotos.Items)
{
var id = ((HiddenField)riItem.FindControl("itemID")).Value;
var title = ((TextBox)riItem.FindControl("txtTitle")).Text;
var description = ((TextBox)riItem.FindControl("txtDescription")).Text;
UpdatePhoto(id, title, description);
}
}
Remember that rdlPhotos.Items here are in initial order. And for identifying which Photo should be updated add hidden field with Photo.ID-value to ReorderList's ItemTemplate like this:
<asp:HiddenField runat="server" ID="itemID" Value='<%# Eval("ID") %>' />