Repeater unable to show textbox - c#

I have a repeater trying to get multiple data to display.
which includes a few textboxes which will show the current setting.
take note that this acts like a 'edit info' page for multiple images at once.
i also have problem displaying the images from the database.
To make it simple :
my .cs code:
DataTable ChildImageDT = myImagesBAL.GetChildImageDT(userID, childID, display);
var userList = new List<Images>();
foreach (DataRow row in ChildImageDT.Rows)
{
var child = new Images()
{
DateTaken = DateTime.Parse(row["image_taken_dt"].ToString()),
PlaceTaken = row["image_taken_loc"].ToString(),
DetailedInfo = row["image_info"].ToString()
};
userList.Add(child);
}
Repeater1.DataSource = userList;
Repeater1.DataBind();
my .aspx code
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table class="content_background">
<tr>
<td width= "10%">Date Taken:</td>
<td><asp:TextBox ID="txtName" Text="<%#Eval("DateTaken")%>" Visible="true" runat="server" Height="100px" Width="100px"></asp:TextBox></td>
</tr>
<tr>
<td width= "10%" bgcolor=aqua>Place Taken:</td>
<td bgcolor=blue ><asp:TextBox ID="txtPassword" Text="<%#Eval("PlaceTaken")%>" Visible=true runat="server" BackColor="White" Font-Size="Large" ForeColor="Fuchsia" Height=50px ></asp:TextBox></td>
</tr>
<tr>
<td width= "10%">Detailed Info:</td>
<td><asp:TextBox ID="TextBox1" Text="<%#Eval("DetailedInfo")%>" Visible=true runat="server" ></asp:TextBox></td>
</tr>
</table>
</ItemTemplate>
my output as shown:
note: that the output is in the "text: " but the whole text box doesnt appear.

You might be getting a "The server tag is not well formed." error.
Just change your Eval code to single quotes instead of a double quote e.g.
Text="<%# Eval("DateTaken") %>" // It's understood as string text
to
Text='<%# Eval("DateTaken") %>' // now understood as server side code.

Related

How to hide table rows in a DataList if column data returns null from SQL Server

I need to hide table rows in a DataList if column data returns null from SQL Server (for each individual column). I have it working successfully but this method will be very tedious as I have about 100 rows in my table. Surely there is a simpler way.
Here is my C# code:
protected void DataList1_ItemDataBound1(object sender, DataListItemEventArgs e)
{
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountStatus")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountStatus");
row.Visible = false;
}
if ((String.IsNullOrEmpty(((Label)e.Item.FindControl("lblAccountName")).Text)))
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("rowAccountName");
row.Visible = false;
}
}
Here is my webform markup:
<asp:DataList ID="DataListAccount" runat="server" OnItemDataBound="DataList1_ItemDataBound1">
<ItemTemplate>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
<tr id="rowAccountName">
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountName" runat="server" Text='<%# Eval("ACCOUNT_NAME") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:DataList>
You can wrap the ItemTemplate contents with a PlaceHolder and use a Ternary Operator to set the visibility.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# !string.IsNullOrEmpty(Eval("ACCOUNT_STATUS").ToString()) ? true : false %>'>
<tr>
<td style="width: 171px">Account Status:</td>
<td style="width: 220px">
<asp:Label ID="lblAccountStatus" runat="server" Text='<%# Eval("ACCOUNT_STATUS") %>'></asp:Label>
</td>
</tr>
</asp:PlaceHolder>
But I would recommend you make sure you filter the source data of empty items. Something like
SELECT * FROM accounts WHERE account_status IS NOT NULL
just modify this code by adding multiple conditions
string value = Convert.ToString( row["MyColumn"]);
if (string.IsNullOrEmpty(value))

how to show an image in image control of which image path is stored in database when user selects a value in dropdownlist in asp.net?

I have a database which is shown below in the image :
Image of database
Here is the image of the form :
Image of the Form
In this form i want to show up the image in image control of which image path is stored up in database and according to subcategory value from the drop down list when user selects it how can i do it please help ... Thank You
Guys here is the source code of my form i have added sqldatasource and bind the dropdown list with sqldatasource .........
<form method="post" enctype="multipart/form-data">
<table border="0" width="450px" height="500px" align="center" class="tableshadow">
<tr>
<td colspan="2" class="toptd" style="color: white; font-size: 24px; text-align: center;
height: 40px; background-color: #60b2e7">
Update Subcategory
</td>
</tr>
<tr>
<td class="lefttxt">
<asp:Label ID="Label1" runat="server" Text="Select Subcategory:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Height="35px" Width="134px" DataSourceID="SqlDataSource1"
DataTextField="subcatname" DataValueField="subcatid">
<asp:ListItem Text="Select category" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnshow" runat="server" OnClick="btnshow_Click" Text="Show" Width="94px" />
</td>
</tr>
<tr>
<td class="lefttxt">
<asp:Label ID="Label2" runat="server" Text="Subcategory Name:"></asp:Label>
<br />
</td>
<td>
<asp:TextBox ID="txtsubcategoryname" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtsubcategoryname"
ErrorMessage="*Please Enter the Subcategory Name" Font-Bold="True" Font-Italic="True"
Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="lefttxt">
<asp:Label ID="Label3" runat="server" Text="Select Category:"></asp:Label>
</td>
<td>
<asp:DropDownList ID="DLCategory" runat="server" Height="35px" Width="146px" DataSourceID="SqlDataSource1"
DataTextField="categoryname" DataValueField="categoryname">
<asp:ListItem Text="Select category" Value="0"></asp:ListItem>
</asp:DropDownList>
<tr>
<td class="lefttxt">
<asp:Label ID="Label6" runat="server" Text="Category Name:"></asp:Label>
<br />
</td>
<td>
<asp:TextBox ID="txtcategoryname" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcategoryname"
ErrorMessage="*Please Enter the Category Name" Font-Bold="True" Font-Italic="True"
Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="lefttxt">
<asp:Label ID="Label4" runat="server" Text="Old Pic:"></asp:Label>
</td>
<td>
<input type="hidden" name="h1" value="" /><asp:Image ID="Image1" runat="server" />
</td>
</tr>
<tr>
<td class="lefttxt">
<asp:Label ID="Label5" runat="server" Text="Upload New Pic:"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" Width="225px" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" />
</td>
</tr>
</table>
</form>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ToursandTravelsConnectionString %>"
SelectCommand="SELECT [subcatid], [subcatname], [categoryname], [pic] FROM [subcategory]">
</asp:SqlDataSource>
Providing the 'pic' column in database holds links to the images are absolute, it also depends on how you have set the drop down list up.
You will need an OnSelectedIndexChanged for the drop down which does the following:
Selects the pic url from the database where subcategory = dropdown value or text. (this depends on how you set the drop-down list up).
With that obtained value set the pic to the one you have obtained.
You need the following, providing code would make it easier.
In you ASP file add the following to the dropdown:
OnSelectedIndexChanged="drp_SelectedIndexChanged" AutoPostBack="True"
Within code behind you need:
protected void drp_SelectedIndexChanged(object sender, EventArgs e) {
//SQL To obtain url for image based on drop down selection
//providing code makes this easier
//with return url:
Image1.ImageUrl = //returned url
}
Are you having issue in showing image in image control ? Can you right click on that container and check path in properties ?
You can use AutoPostBack as True and "OnSelectedIndexChanged" to call server function and update image control based on result.
Or you can use JQuery to show images in Div tag.
jQuery(document).ready(function(){
$("#dropdownID").change(function() {
$.ajax({
url: "SericeFunction name",
success: function(msg){
.....<bind img tag >
}
});
});
});
Thank You All of you guys especially #John for your syntax you provided thanks alot.. Below is the code :
protected void btnshow_Click(object sender, EventArgs e)
{
using (var cn = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True"))
using (var cmd = cn.CreateCommand())
{
cn.Open();
cmd.CommandText = "select pic from subcategory where subcatid = '"+DropDownList1.Text+"'";
cmd.Parameters.AddWithValue("#subcatid",DropDownList1.Text);
using (var reader = cmd.ExecuteReader())
{
if (reader.Read())
{
var filePath = reader.GetString(0);
// For this to work images must be stored inside the web application.
// filePath must be a relative location inside the virtual directory
// hosting the application. Depending on your environment some
// transformations might be necessary on filePath before assigning it
// to the image url.
Image1.ImageUrl =("~/"+filePath);
}
}
}

C# Repeater displaying single listing 12 times

I have a repeater I'm using to display a sidebar nav type system on my page. It loads some data from a db for the display of the links. It should be showing a single listing in this case, however it shows the same listing a total of 13 times.
My code:
private void Load_Locations() //Loads a list of locations.
{
var query = from q in CurrentContext.LOC
join w in CurrentContext.int on q.key equals w.L_key
select new
{
Location = q.Location
};
rpt_locations.DataSource = query;
rpt_locations.DataBind();
}
^to pull the data from the database, works perfectly as I use the same snippet for other things.
<div class="Side_Menu" style="margin-left: auto; margin-right: auto; text-align: center">
<asp:Repeater ID="rpt_locations" runat="server" OnItemCommand="rpt_locations_ItemCommand">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:LinkButton ID="btn_location" runat="server" Text='<%#Eval("Location") %>' Font-Underline="False" OnClick="btn_location_Click"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td>
<asp:LinkButton ID="btn_location" runat="server" Text='<%#Eval("Location") %>' Font-Underline="False" OnClick="btn_location_Click"></asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
^Asp coding for the repeater.
Havn't expericned this issue before, pretty lost on how it's happening.

How can I display a value in a repeater without changing the data

Title says it all really. I have a asp.net repeater, I want a way that I can display a string.empty instead of whatever data is actually there, but I want that data to remain unchanged. Any ideas?
C#:
foreach (var item in theContainer.Coverages.Values)
{
if (!item.IsCancelled)
{
if (Enumerations.Coverage.MainCoverages.Contains(item.Coverage))
{
if (item.isScheduled == true)
//TODO code that displays a string.empty for limit
}
rpMainCoverages.DataSource = item.Coverage;
}
}
};
And html:
<asp:Repeater ID="rpMainCoverages" runat="server" OnItemDataBound="BindItem">
<ItemTemplate>
<tr>
<td><asp:Label ID="lblCovName" runat="server" /></td>
<td style="text-align: right;"><asp:Label ID="lblDisplayLimit" runat="server" /></td>
<td style="text-align: right;"><asp:Label ID="lblDisplayPremium" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>

Get Repeater Label in the current "row" being accessed

I have the following Repeater:
<asp:Repeater ID="RptLeaveRequests" runat="server"
onitemdatabound="RptLeaveRequests_ItemDataBound"> <ItemTemplate>
<table id="tableItem" runat="server">
<tr>
<td style="width: 100px;">
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:dd/M/yyyy}") %>'></asp:Label>
</td>
<td style="width: 100px;">
<asp:Label ID="lblHours" runat="server" Text='<%#Eval("Hours") %>'></asp:Label>
</td>
<td style="width: 200px;">
<asp:Label ID="lblPeriod" runat="server" Text='<%#Eval("AMorPM") %>'></asp:Label>
</td>
<td style="width: 200px; font-size:10px;">
<asp:Label ID="lblNote" runat="server" Text='<%#Eval("Note") %>'></asp:Label>
</td>
<td style="50px">
<asp:RadioButtonList ID="rbtVerified" runat="server" >
<asp:ListItem Value="1">Accept</asp:ListItem>
<asp:ListItem Value="2">Reject</asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<asp:TextBox ID="txtNotes" runat="server" ></asp:TextBox>
</td>
</tr>
</table>
I am trying to get the data in each Label (ex: Convert.ToString((Label)item.FindControl("Date")) ) but it is returning an empty string, what am I doing wrong:
foreach (RepeaterItem item in RptLeaveRequests.Items)
{
var rdbList = item.FindControl("rbtVerified") as RadioButtonList;
switch (rdbList.SelectedValue)
{
case "1":
if (new LeaveLogic().AddLeaveEmployee(Convert.ToString((Label)item.FindControl("Date")), Convert.ToDouble((Label)item.FindControl("Hours")), Convert.ToString((Label)item.FindControl("AMorPM")), "Vacational Leave", Convert.ToInt32(Context.User.Identity.Name), Convert.ToString((Label)item.FindControl("Note")))
{
Response.Redirect(Request.RawUrl);
}
break;
I believe it's not working because you aren't finding the controls. FindControl will return null if it can't find the control, and Convert.ToString will return an empty string if the object value is null.
From what I can see, you are searching for the wrong string ID. So instead of Date, it should be lblDate.
If you are in debug build mode, keep in mind that ASP.NET loves to change your control names at runtime, so "lblDate" control might not actually be "lblDate". So you can try debugging on the browser and inspect your elements' IDs for their actual IDs.
Also, you probably want to do this instead if you want the label's actual data (notice the .Text):
((Label)item.FindControl("lblDate")).Text

Categories

Resources