ListView - Show LayoutTemplate on empty data source - c#

For a shopping cart page, the list of items is displayed in a html table.
I use a ListView for that and it works great.
When the cart is empty, the text 'This cart is empty' appears. But it only renders the code in the EmptyDataTemplate. My goal is to display the table headers ('delete', 'product', 'quantity', etc.) without repeating that html code in the EmptyDataTemplate.
Trying being clever I changed my EmptyDataTemplate into an EditItemTemplate and used the bit of code displayed below.
Can anyone think of a more more elegant solution for this problem??
[C# code]
lvShoppingCart.DataSource = _cart.Items;
lvShoppingCart.DataBind();
if (_cart.ProductCount == 0)
{
lvShoppingCart.DataSource = new List<string>() { "dummy cart item" };
lvShoppingCart.EditIndex = 0;
lvShoppingCart.DataBind();
}
[ASPX code]
<asp:ListView ID="lvShoppingCart" runat="server">
<LayoutTemplate>
<table style="width: 600px;" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
<strong>Delete</strong>
</td>
<td width="400">
<strong>Product</strong>
</td>
<td width="100">
<strong>Quantity</strong>
</td>
<td width="100">
<strong>Price</strong>
</td>
<td width="100">
<strong>Total</strong>
</td>
</tr>
</table>
<hr />
</td>
</tr>
<tr id="itemPlaceHolder" runat="server">
</tr>
<tr id="trShoppingCartUpdateBtn" runat="server">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
</td>
<td width="400">
</td>
<td colspan="3" width="300">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<asp:ImageButton ID="btnImgUpdateQuantities" ImageUrl="../img/refresh.gif" AlternateText="update shopping cart"
OnClick="btnUpdateQuantities_Click" runat="server" />
</td>
<td>
<asp:LinkButton ID="btnUpdateQuantities" Text="update cart" OnClick="btnUpdateQuantities_Click"
runat="server" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr id="trShoppingCartTotals" runat="server">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="4">
<div align="right">
<strong>Totals: </strong>
</div>
</td>
<td width="100">
<asp:Label ID="lblCartTotal" runat="server" Text="0" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<EditItemTemplate>
<tr>
<td colspan="5" align="center">
<p>
<em>This cart is empty.</em>
</p>
</td>
</tr>
</EditItemTemplate>
<ItemTemplate>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
<a href='<%# ShoppingCartUrl %>?action=remove&id=<%# Eval("Product.Id") %>'>X</a>
</td>
<td width="400">
<%# Eval("Product.DisplayName") %>
</td>
<td width="100">
<label>
<asp:TextBox ID="txtQuantity" Text='<%# Eval("Quantity") %>' runat="server" size="3" />
</label>
</td>
<td width="100">
<%# Eval("Price", "{0:C}") %>
</td>
<td width="100">
<%# Eval("TotalPrice", "{0:C}") %>
</td>
</tr>
</table>
<hr />
</td>
</tr>
</ItemTemplate>
</asp:ListView>

You can add an empty InsertItemTemplate and set InsertItemPosition="LastItem"

Below you can find a simplified example of the shopping cart code.
It is using the 'InsertItemTemplate' solution provided in the answer of user757933.
I find this a more elegant solution than using the 'EditItemTemplate' which requires a 'dummy' data source.
Usage:
By default you should see an empty cart. When you uncomment the lines for 'bread', 'apples' and 'eggs' the message 'This cart is empty' should be hidden, instead you will see the three items appear in the cart.
[ASPX code]
<asp:ListView ID="lvShoppingCart" runat="server">
<LayoutTemplate>
<pre>
---------------------------------------------------------------------------
| Product | Quantity | Price | Total |
---------------------------------------------------------------------------
<div id="itemPlaceHolder" runat="server">
</div>
---------------------------------------------------------------------------
| | <asp:Label ID="lblCartTotal" runat="server" Text="0" /> |
---------------------------------------------------------------------------
</pre>
</LayoutTemplate>
<InsertItemTemplate>
| This cart is empty |
</InsertItemTemplate>
<ItemTemplate>
| <%# Container.DataItem.ToString().PadRight(17) %> | | | |
</ItemTemplate>
</asp:ListView>
[C# code]
internal class Cart : IEnumerable<string>
{
public List<string> Items { get; set; }
public Cart()
{
Items = new List<string>();
}
public IEnumerator<string> GetEnumerator()
{
return Items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
protected void Page_Load(object sender, EventArgs e)
{
Cart _cart = new Cart();
//_cart.Items.Add("bread");
//_cart.Items.Add("apples");
//_cart.Items.Add("eggs");
lvShoppingCart.DataSource = _cart;
// Make sure the 'InsertItemTemplate' is hidden from view when items are added to the cart.
lvShoppingCart.InsertItemPosition = _cart.Items.Count == 0 ? InsertItemPosition.LastItem : InsertItemPosition.None;
lvShoppingCart.DataBind();
Label _lblCartTotal = lvShoppingCart.FindControl("lblCartTotal") as Label;
if (_lblCartTotal != null)
{
_lblCartTotal.Text = string.Format("<strong>Total: </strong> {0}", _cart.Items.Count);
}
}

Related

C# = The ListView raised event which wasn't handled.

There is a ListView which shows the data that were retrieved from the database.
I want to edit a single record on a ListView by clicking the edit button beside it but if I press the edit button, I get the error:
The ListView 'lvItemView' raised event ItemEditing which wasn't handled.
Here is the ListView:
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder">
<LayoutTemplate>
<table style="color: Black;" width="100%" border="0" cellpadding="5">
<tr>
<th style="text-align: center;">Customer ID</th>
<th style="text-align: center;">Contact Name</th>
<th style="text-align: center;">Company</th>
<th style="text-align: center;">Created By</th>
<th style="text-align: center;">Created Date</th>
<th style="text-align: center;">Modified By</th>
<th style="text-align: center;">Modified Date</th>
</tr>
<asp:PlaceHolder ID="itemHolder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td align="left">
<asp:Label ID="lblCustomerID" runat="Server" Text='<%#Eval("_CustomerID") %>' />
</td>
<td align="left">
<asp:Label ID="lblContactName" runat="Server" Text='<%#Eval("_ContactName") %>' />
</td>
<td align="left">
<asp:Label ID="lblCompany" runat="Server" Text='<%#Eval("_CompanyID") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedBy" runat="Server" Text='<%#Eval("_CreatedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblCreatedDate" runat="Server" Text='<%#Eval("_CreatedDate") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedBy" runat="Server" Text='<%#Eval("_ModifiedBy") %>' />
</td>
<td align="left">
<asp:Label ID="lblModifiedDate" runat="Server" Text='<%#Eval("_ModifiedDate") %>' />
</td>
<td align="left">
<asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
<td align="left">
<asp:LinkButton ID="Delete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
And the code behind
protected void lvItemView_ItemEditing(object sender, ListViewEditEventArgs e)
{
System.Web.UI.WebControls.Label index_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCustomerID");
int customerID = int.Parse(index_id.Text);
Item item = new Item();
item._CustomerID = customerID;
System.Web.UI.WebControls.Label cmp_id = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCompany");
int companyID = int.Parse(cmp_id.Text);
item._CompanyID = companyID;
System.Web.UI.WebControls.Label samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblContactName");
item._ContactName = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedBy");
item._CreatedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblCreatedDate");
item._CreatedDate = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedBy");
item._ModifiedBy = samp.Text;
samp = (System.Web.UI.WebControls.Label)lvItemView.Items[e.NewEditIndex].FindControl("lblModifiedDate");
item._ModifiedDate = samp.Text;
CustomerID = item._CustomerID.ToString();
ContactName = item._ContactName.ToString();
CompanyID = item._CompanyID.ToString();
CreatedBy = item._CreatedBy.ToString();
CreatedDate = item._CreatedDate.ToString();
ModifiedBy = item._ModifiedBy.ToString();
ModifiedDate = item._ModifiedDate.ToString();
modAdd.Show();
}
I am new to asp.net and c# so I have no idea what to do with this kind of error.
set the event lvItemView_ItemEditing in OnItemEditing which will be triggered when click on edit button
<asp:ListView ID="lvItemView" runat="server" ItemPlaceholderID="itemHolder" OnItemEditing="lvItemView_ItemEditing">
and also set data source if you are binding on pageload
lvItemView.DataSource = SomeData;
lvItemView.DataBind();

TextBox validation is empty or 11 digits

I have a textbox in my form which must be validated only if is empty or contains 11 digits.
I tried every solution found on internet but nothing happens.
<script type="text/javascript">
$(document).ready(function() {
$('#ctl00_CPH1_tax_id').attr("placeholder", "1234567890");
$('#ctl00_CPH1_citizen_id').attr("placeholder", "12345678901");
$('#ctl00_CPH1_e_posta').attr("placeholder", "ahmet.mehmet#sirket.com.tr");
//var isbp = $('#ctl00_CPH1_IsBP');
var name = $('#ctl00_CPH1_name').val();
var code = $('#ctl00_CPH1_code').val();
var dropdownlist2 = $('select:first');
var tax_id = $('#ctl00_CPH1_tax_id').val();
var citizen_id = $('#ctl00_CPH1_citizen_id').val();
var email1 = $('#ctl00_CPH1_e_posta').val();
var isbp = $('input[type=checkbox]:first');
var dropdownlist1 = $('select:last');
var isactive = $('input[type=checkbox]:last');
//var dropdownlist1 = $('#ct100_CPH1_DropDownList1');
var tax_id_check = /^\d{10}$/.test(tax_id);
var citizen_id_check = /^$|^\d{11}$/.test(citizen_id);
var email1_check = /^[a-zA-Z0-9._]+#([a-zA-Z0-9.]|[a-zA-Z0-9]+\-[a-zA-Z0-9.])+\.[a-zA-Z]{2,6}$/.test(email1);
dropdownlist1.attr("disabled", "disabled");
isactive.attr("disabled", "disabled");
$(isbp).click(function(e) {
if (isbp.is(':checked')) {
dropdownlist1.removeAttr("disabled");
isactive.removeAttr("disabled");
}
else {
isactive.attr("disabled", "disabled");
dropdownlist1.attr("disabled", "disabled");
}
});
$("#ctl00_CPH1_Button1").click(function(e) {
//console.log(drop2);
//var name_status = /^[a-zA-Z][a-zA-Z]*$/.test(name);
//var code_status = /^[a-zA-Z][a-zA-Z]*$/.test(code);
//var IsBP = $('#ctl00_CPH1_IsBP');
//if (name.length < 3 || name_status == false) {
// e.preventDefault();
// alert('İsim sadece karakterlerden oluşmalı ve en az iki karakter olmalı!');
//}
//if (code.length < 3 || code_status == false) {
// e.preventDefault();
// alert('Soyisim sadece karakterlerden oluşmalı ve en az iki karakter olmalı!');
//}
if (name.length == 0) {
e.preventDefault();
alert("İsim girilmedi!");
}
if (code.length == 0) {
e.preventDefault();
alert("Şirket Kodu girilmedi!");
}
if (dropdownlist2.val() == 0) {
e.preventDefault();
alert("Ülke seçilmedi!");
}
if (citizen_id.length != 0) {
e.preventDefault();
alert("Vatandaşlık numarası onbir haneli olmalı!");
}
if (tax_id.length == 0) {
e.preventDefault();
alert("Vergi numarası girilmedi!");
}
else if (!tax_id_check) {
e.preventDefault();
alert("Vergi numarası on haneli olmalı!");
}
if (email1 != "" && !email1_check) {
e.preventDefault();
alert("E-Posta geçerli değil!");
}
if (isbp.is(':checked') && dropdownlist1.val() == 0) {
e.preventDefault();
alert("İş ortağı tipi seçilmedi!");
}
});
});
This is my script and aspx follows:
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td colspan="3" align="center">
Şirket Giriş Ekranı</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label1" runat="server" Text="İsim"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="name" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label2" runat="server" Text="Şirket Kodu"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="code" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label3" runat="server" Text="Ülke"></asp:Label>
</td>
<td class="style4" colspan="2">
<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label4" runat="server" Text="Vergi No"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="tax_id" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label5" runat="server" Text="Vergi Dairesi"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="tax_office" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label6" runat="server" Text="Vatandaşlık No"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="citizen_id" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label7" runat="server" Text="Adres"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="address" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label14" runat="server" Text="E-posta"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="e_posta" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label15" runat="server" Text="Telefon"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="telefon" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label16" runat="server" Text="Fax"></asp:Label>
</td>
<td colspan="2">
<asp:TextBox ID="faks" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label10" runat="server" Text="İş Ortağım"></asp:Label>
</td>
<td colspan="2">
<asp:CheckBox ID="IsBP" runat="server" oncheckedchanged="IsBP_CheckedChanged"/> <!--AutoPostBack="true"-->
<asp:Label ID="Label11" runat="server" Text="Evet"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:Label ID="Label12" runat="server" Text="İş Ortağı Tipi"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</td>
<td>
<asp:CheckBox ID="IsActive" runat="server"
oncheckedchanged="IsActive_CheckedChanged" />
<asp:Label ID="Label13" runat="server" Text="Aktif"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Geri" />
</td>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Kaydet" />
</td>
</tr>
</table>
<br />
</center>
</div>
</form>
I tried jquery older versions like 1.8.3 which is stable instead of 1.9.1
Also jquery.validate.
When I run the code on chrome and inspect element on console everything runs properly.
But my code does not show any alerts.
"==" works but "!=" or ">" not.
Thanks in advance!
not working textboxes are e_mail and citizen_id. other textboxes work properly.
Use the ASP.NET validator with a regular expression akin to
"(\d{11})?"
<asp:RegularExpressionValidator ControlToValidate="citizen_id"
ValidationExpression="(\d{11})?" runat="server"
Text="Must be 11 digits or empty" />
This will prevent a postback if the textbox "citizen_id" is anything but "" or an 11 digit number.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx

Sorting in Repeater

Here is my code
<asp:Repeater ID="rpRatesheetDetails" runat="server"
onitemcommand="rpRatesheetDetails_ItemCommand"
onitemdatabound="rpRatesheetDetails_ItemDataBound">
<HeaderTemplate>
<tr>
<td width="110" height="25px" class="content">
<table cellpadding="0" cellspacing="1">
<tr>
<td style="cursor:pointer;" runat="server" onclick="toggle();">
Carrier
</td>
<td align="right">
<asp:ImageButton ID="btnCarrierSortAsc" runat="server" ImageUrl="~/Images/Arrow_T.png" ToolTip="Sort Ascending Order" style="display:none;"
CommandName="SortCarrierASC" />
</td>
<td align="right">
<asp:ImageButton ID="btnCarrierSortDsc" runat="server" ImageUrl="~/Images/Arrow_B.png" ToolTip="Sort Descending Order" style="display:none;"
CommandName="SortCarrierDESC" />
</td>
</tr>
</table>
</td>
<td width="110" class="content">
<table cellpadding="0" cellspacing="1">
<tr>
<td>
Date
</td>
<td align="right">
<asp:ImageButton ID="btnDateSortAsc" runat="server" ImageUrl="~/Images/Arrow_T.png" ToolTip="Sort Ascending Order" style="display:none;"
CommandName="SortDateASC" />
</td>
<td align="right">
<asp:ImageButton ID="btnDateSortDesc" runat="server" ImageUrl="~/Images/Arrow_B.png" ToolTip="Sort Descending Order" style="display:none;"
CommandName="SortDateDESC" />
</td>
</tr>
</table>
</td>
<td width="82" class="content">
Call Type
</td>
<td width="110" class="content">
Format
</td>
<td width="100" class="content">
<table cellpadding="0" cellspacing="1">
<tr>
<td>
Status
</td>
<td align="right">
<asp:ImageButton ID="btnStatusAsc" runat="server" ImageUrl="~/Images/Arrow_T.png" ToolTip="Sort Ascending Order" style="display:none;"
CommandName="SortStatusASC" />
</td>
<td align="right">
<asp:ImageButton ID="btnStatusDsc" runat="server" ImageUrl="~/Images/Arrow_B.png" ToolTip="Sort Descending Order" style="display:none;"
CommandName="SortStatusDESC" />
</td>
</tr>
</table>
</td>
<td width="70" class="content">
User
</td>
<td width="82" class="content">
File Name
</td>
<td width="110" class="content">
System
</td>
<td width="110" class="content">
<table cellpadding="0" cellspacing="1">
<tr>
<td>
No. Of Days
</td>
<td align="right">
<asp:ImageButton ID="btnDaysAsc" runat="server" ImageUrl="~/Images/Arrow_T.png" ToolTip="Sort Ascending Order"
CommandName="SortNoOfDaysASC" />
<asp:ImageButton ID="btnDaysDsc" runat="server" ImageUrl="~/Images/Arrow_B.png" ToolTip="Sort Descending Order"
CommandName="SortNoOfDaysDESC" />
</td>
</tr>
</table>
</td>
</tr>
</HeaderTemplate>
Above is the header template of my repeater.By default when my repeater loads it is sorted by the last column.Now i want dat if the user want to sort by any other column he should click on dat column.Only den the sort icon will be shown and the records will be sorted.I am showing this icon on the ItemDataBound event but how to fire an ItemDataBound event while clicking on header??
The order of the items in the repeater depends on the order in the bound collection (the DataSource).
Sort the items in the DataSource before binding in order to get a sorted list in your repeater.
Do this in your rpRatesheetDetails_ItemCommand - check the command name, sort the collection accordingly, then bind.

How do I keep the existing filename in the database when updating a page?

I have an page called Update.aspx, and in it I am updating all values instead of filename. When I am going to update info, all values are fetched from the database to corresponding textboxes. But if I have a filename such as abc.jpg in the database and I go on the update page without browsing the file and update, it's inserted as null.
How can I keep the same file (i.e. abc.jpg) in database even though it's not updated by user?
This is my code:
string onlyname = string.Empty;
string filename = "";
string path = "";
if (FileUp.HasFile)
{
filename = FileUp.PostedFile.FileName;
path = Server.MapPath("/clients_images/") + FileUp.FileName;
onlyname = path.Substring(path.LastIndexOf("\\") + 1);
FileUp.SaveAs(path);
}
else
{
onlyname = dr["Client_Logo"].ToString().Trim();
}
param[4] = new SqlParameter("#Client_Logo", SqlDbType.VarChar, 200);
param[4].Value = onlyname.ToString().Trim();
cmd.Parameters.Add(param[4]);
ASPX code:
<table align="center" cellpadding="3" cellspacing="0" border="0" width="638" class="tableborder">
<tr>
<td colspan="2" class="mainbg" align="center" height="13">
<font class="general">Update Client Detail</font></td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Client Name :</b></td>
<td align="left">
<asp:TextBox ID="txtUpdateclientname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Company Name :</b></td>
<td align="left">
<asp:TextBox ID="updatecompanyname" Text="" Columns = "30" CssClass="checkbox02" runat="server" onchange="Javascript: return initialCap(this);"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<b>Email :</b></td>
<td align="left">
<asp:TextBox ID="updateemail" Text="" Columns = "30" CssClass="checkbox02" runat="server"></asp:TextBox>
<font class="mandatory">*</font>
</td>
</tr>
<tr>
<td class="general" width="50%" valign="top" align="right">
<b>Logo :</b></td>
<td class="general" width="50%" align="left">
<asp:FileUpload ID="FileUp" runat="server" CssClass="checkbox02" />
<br /> (Height of image should not be more than 67 And Width of image should not be more than 380)
</td>
</tr>
<tr>
<td class="general" width="50%" align="right">
<%--<b><font class="bluetext"><strong> Upload : </strong>--%></td>
<td align="left">
<asp:LinkButton ID="viewImage" Text="View Existing" runat="server" cssClass="general-white" class="link" OnCommand="LinkButton_Command" CommandName="downloadfile" ToolTip="Click to View Existing" Font-Bold="true"></asp:LinkButton>
</td>
</tr>
<tr>
<td class="general" width="50%" align="right" valign="top">
<b>Project Type :</b></td>
<td width="100%" align="left" valign="top" colspan="2">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="right" valign="top" colspan="3">
<asp:ListBox ID="LstUserLeft" Width="100%" Autoscroll="false" SelectionMode="Multiple" Height="150" CssClass="checkbox02"
runat="server">
</asp:ListBox>
</td>
</tr>
</table>
</td>
</tr>
<tr>
</tr>
</table>
You can use HiddenField or ViewState to store the filename from the Client_logo field (from the database).
For instance.
Assign the value to the HiddenField1 while retrieving result,
HiddenField1.Value=dr["Client_Logo"].ToString().Trim();
and while updating,
if (FileUp.HasFile)
{
...
}
else
{
onlyname = HiddenField1.Value;
}

Validation not working for ImageButton click

I have an image button for login in my system. so as the most common thing i m having two text boxes txt_username and txt_password.
i have put required field validator for both the text boxes. and set the validation group "a" and respective control to validate for both of them. the source code for my ImageButton is as follows:-
<td align="right" class="simple_text"> </td>
<td align="left">
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="true"
ImageUrl="~/images/signin_button.gif" onclick="ImageButton1_Click"
ValidationGroup="a" />
</td>
now I am fed up as when I run my application and click the ImageButton it doesn't respond to the validation at all and redirects to the next page. Can anyone say why is it so? this is the first time wherein the validators are not working.
this is source code:-
<td align="left"><table width="480" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="178" align="right" class="simple_text">User Name : </td>
<td width="302" align="left">
<asp:TextBox ID="txt_username" runat="server"
CssClass="text_box_username" Width="180px" AutoPostBack="True"
ontextchanged="txt_username_TextChanged" ValidationGroup="a"
CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_username" ErrorMessage="RequiredFieldValidator"
ValidationGroup="a">**</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" class="simple_text"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="right" class="simple_text">Password : </td>
<td align="left">
<asp:TextBox ID="txt_password" runat="server" CssClass="text_box_password"
TextMode="Password" Width="180px" ValidationGroup="a"
CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txt_password" ErrorMessage="RequiredFieldValidator"
ValidationGroup="a">**</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" class="simple_text"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="right" class="simple_text"> </td>
<td align="left">
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="true"
ImageUrl="~/images/signin_button.gif" onclick="ImageButton1_Click"
ValidationGroup="a" />
</td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"> </td>
</tr>
<tr>
hi just paste this code will work surely for you
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<td align="left"><table width="480" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="178" align="right" class="simple_text">User Name : </td>
<td width="302" align="left">
<asp:TextBox ID="txt_username" runat="server"
Width="180px" AutoPostBack="True"
ValidationGroup="a"
CausesValidation="True" ontextchanged="txt_username_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_username" ErrorMessage="RequiredFieldValidator"
ValidationGroup="a">**</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" class="simple_text"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="right" class="simple_text">Password : </td>
<td align="left">
<asp:TextBox ID="txt_password" runat="server"
TextMode="Password" Width="180px" ValidationGroup="a"
CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txt_password" ErrorMessage="RequiredFieldValidator"
ValidationGroup="a">**</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" class="simple_text"> </td>
<td align="left"> </td>
</tr>
<tr>
<td align="right" class="simple_text"> </td>
<td align="left">
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="true"
ImageUrl="~/images/signin_button.gif" onclick="ImageButton1_Click"
ValidationGroup="a" />
</td>
</tr>
</table></td>
</tr>
<tr>
<td align="left"> </td>
</tr>
<tr>
</div>
</form>
</body>
</html>
Hope you provided the validationGroup property properly.
Specifying Validation Groups
How about this:
<asp:ImageButton runat="server" CausesValidation="true" ImageUrl="..." />
CausesValidation="true" might force the validation to run.

Categories