Saturday, March 31, 2012

Set a bgcolor in a repeater based on databse value asp newbie

Hi,

I'm just starting to write in asp and have been trying to create a repeater
to display some data in a simple form.

The basics worked ok and then I tried to set a bgcolor on a cell based on
it's value, I have read and re-read a good few things but nothing seems to
point to this scenario directly. I know the following code doesn't actually
work but this is roughly what I'm trying to do. eventually I want to do the
same thing with dates but baby steps first :)

<ItemTemplate>
<%If #(DataBinder.Eval(Container.DataItem, "CategoryName") =
"something") Then%>'
<tr>
<td bgcolor="#ff0000">
<%Else%>
<tr>
<td bgcolor="#CCFFCC">
<% End If%>
<asp:Label runat="server" ID="Labell" Text='<%#
Eval("CategoryName") %>' />
</td>
<td bgcolor="#CCFFCC">
<asp:Label runat="server" ID="Label2" Text='<%#
Eval("Description") %>' />
</td>
</tr>
</ItemTemplate
Thanks in advance for any help

Jim FlorenceInstead of attempting to change the colour of the <ItemTemplate>, add an
<AlternatingItemTemplate> section and type the colour in there. dotNet will
then take care of the rest for you.

"Jim Florence" wrote:

> Hi,
> I'm just starting to write in asp and have been trying to create a repeater
> to display some data in a simple form.
> The basics worked ok and then I tried to set a bgcolor on a cell based on
> it's value, I have read and re-read a good few things but nothing seems to
> point to this scenario directly. I know the following code doesn't actually
> work but this is roughly what I'm trying to do. eventually I want to do the
> same thing with dates but baby steps first :)
> <ItemTemplate>
> <%If #(DataBinder.Eval(Container.DataItem, "CategoryName") =
> "something") Then%>'
> <tr>
> <td bgcolor="#ff0000">
> <%Else%>
> <tr>
> <td bgcolor="#CCFFCC">
> <% End If%>
> <asp:Label runat="server" ID="Labell" Text='<%#
> Eval("CategoryName") %>' />
> </td>
> <td bgcolor="#CCFFCC">
> <asp:Label runat="server" ID="Label2" Text='<%#
> Eval("Description") %>' />
> </td>
> </tr>
> </ItemTemplate>
> Thanks in advance for any help
> Jim Florence
"Dean" <Dean@.discussions.microsoft.com> wrote in message
news:79F7A0BD-A3ED-4605-8CE4-89DACF298D04@.microsoft.com...
Dean,

Thanks for the reply, that works great for the entire row but not for
individual cells within the row based on the cell's value or name.

What I'm eventually trying to do is set an individual date cell to be either
red, amber or green depending on how many days overdue an item is

Many thanks again

Jim

>Instead of attempting to change the colour of the <ItemTemplate>, add an
> <AlternatingItemTemplate> section and type the colour in there. dotNet
> will
> then take care of the rest for you.
>
> "Jim Florence" wrote:
>>
>> Hi,
>>
>> I'm just starting to write in asp and have been trying to create a
>> repeater
>> to display some data in a simple form.
>>
>> The basics worked ok and then I tried to set a bgcolor on a cell based on
>> it's value, I have read and re-read a good few things but nothing seems
>> to
>> point to this scenario directly. I know the following code doesn't
>> actually
>> work but this is roughly what I'm trying to do. eventually I want to do
>> the
>> same thing with dates but baby steps first :)
>>
>> <ItemTemplate>
>> <%If #(DataBinder.Eval(Container.DataItem, "CategoryName") =
>> "something") Then%>'
>> <tr>
>> <td bgcolor="#ff0000">
>> <%Else%>
>> <tr>
>> <td bgcolor="#CCFFCC">
>> <% End If%>
>> <asp:Label runat="server" ID="Labell" Text='<%#
>> Eval("CategoryName") %>' />
>> </td>
>> <td bgcolor="#CCFFCC">
>> <asp:Label runat="server" ID="Label2" Text='<%#
>> Eval("Description") %>' />
>> </td>
>> </tr>
>> </ItemTemplate>
>>
>> Thanks in advance for any help
>>
>> Jim Florence
>>
Jim,

Try this instead:
<td bgcolor='<%#(DataBinder.Eval(Container.DataItem, "CategoryName") ==
"something"?"#ff0000":"#ffffff"%>'
This assumes that you are in C#, in VB i think there is a immediate if
of IIf();

HTH,
Chris
"chris" <chris@.cubed-c.com> wrote in message
news:1151142480.516385.115290@.m73g2000cwd.googlegr oups.com...
Chris your a star,

After very minor fiddling the code is

<td bgcolor='<%# IIF (DataBinder.Eval(Container.DataItem, "CategoryName") =
"Confections","#ff0000","#ccffcc")%>'
Thanks very much for your help I tearing my hair out trying to make this
work

Regards

Jim

> Jim,
> Try this instead:
> <td bgcolor='<%#(DataBinder.Eval(Container.DataItem, "CategoryName") ==
> "something"?"#ff0000":"#ffffff"%>'>
> This assumes that you are in C#, in VB i think there is a immediate if
> of IIf();
> HTH,
> Chris

set a date value

Dear sir,

We are on the week of 27 in year 2004. So this week is "2004-27", last week
is "2004-26",

My question is how can I get the first sunday of week "2004-26", how can I
change string "2004-26" into a DateTime variable with the value of first
sunday of week 2004-26?

--
Kind regards

Guoqi Zheng
guoqi AT meetholland dot com
Http://www.meetholland.comThe easiest way I could think of would be to check what the first day of the
year is (1/1/2004) through the DayOfWeek property of the DateTime class and
calculate the offset between it and the previous Sunday.

For 2004, calling DateTime.DayOfWeek for 1/1/2004 would return Thursday.
Therefore, the first week of 2004 (by week numbers) started on Sunday,
12/28/2003. To determine the Sunday starting any given week during 2004 you
could simply calculate it via:

public DateTime GetWeekStart(int WeekNumber)
{
return FirstSundayOfYear.AddDays(7*(WeekNumber-1));
}

The rationale for (WeekNumber-1) is that the FirstSundayOfYear would already
be set to 12/28/2003 so if you were looking for week 1 then you would want
to add 0 days as opposed to 7.

You indicated that you wanted to convert a string (2004-26) into a DateTime
for the first sunday of each week. What I would probably do in this
scenario since obviously no direct conversion exists would be to define a
WeekDictionary class derived from System.Collections.DictionaryBase and set
the key of the key to the desired string and the value to the appropriate
DateTime value.

Hope this helps!

"Guoqi Zheng" <no@.sorry.nl> wrote in message
news:uIdNKvDXEHA.128@.TK2MSFTNGP10.phx.gbl...
> Dear sir,
> We are on the week of 27 in year 2004. So this week is "2004-27", last
week
> is "2004-26",
> My question is how can I get the first sunday of week "2004-26", how can I
> change string "2004-26" into a DateTime variable with the value of first
> sunday of week 2004-26?
> --
> Kind regards
> Guoqi Zheng
> guoqi AT meetholland dot com
> Http://www.meetholland.com

Set a custom object property in a user control

hi, i have a user control that have a property MyProp of type MyObject.

MyObject also have a property named Prop1;

I want to set Prop1 in the aspx.

For example

<User:MyControl id="control>

<MyProp Prop1="value" />

</User:MyControl>

and

public class MyObject

{

public string Prop1 {get; set;}

}

This code works but i can't go to Design View becouse contents isn't allowed between openin and closing tag of an user control)

Somebody have a solution? ( i don't ant to change my user Control to a server control)

thanks

You cant have content between opening and closing tags.

Your code should be like as below:

<User:MyControl id="control"

Prop1="value">

</User:MyControl>

Try this and let me know if any issues


thanks for your reply but i don't need to set a property of my control, i need to set some properties of an object returned by a property of my control and

i neither want to remap all this properties to some new properties of the control becouse are too match;

a better example is (some implementation details are hidden):

class User

{

public string Name { set; }

public string Surname {set;}

}

class UserView : UserControl

{

public User TargetUser {get;}

}

and in the page

<MyControls:UserView id="userView1">

<TargetUser Name="sam" Surname="foofoo" />

</MyControls:UserView>

This code works but i can't switch to DesignView becouse Visual Studio see contents into my control tags.

I want know if there's a way to set Name and Value property of internal User object into the aspx without problems with visual studio parser.


set a cookie clientside on Textbox onChange

Hi,
I have a textbox, I need to set a cookie to the value of the textbox... when
textbox.Text changes.

How do I do this using clientSide script (javascript) only.

I do not want to post back to the server and then set the cookie using
server script.

Any help is very appretiated (I did search the internet)...
Nalakahttp://techpatterns.com/downloads/j...ipt_cookies.php
Pretty standard pattern, ASP.NET or otherwise.
:-)

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
Think Outside the Box!
*************************************************
"Nalaka" <nalaka12@.nospam.nospamwrote in message
news:%23vE8eBAwGHA.1956@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

Hi,
I have a textbox, I need to set a cookie to the value of the textbox...
when textbox.Text changes.
>
How do I do this using clientSide script (javascript) only.
>
I do not want to post back to the server and then set the cookie using
server script.
>
Any help is very appretiated (I did search the internet)...
Nalaka
>
>


Hi Nalaka,

I think the article Gregory provided is quite good and useful.

BTW, client-side inserted cookie may escape some special characters. When
reading them in server-side code, you may take care on this, you can use
the UrlDecode method to decode them if the cookie(added at client-side) is
escaped:

foreach (string key in Request.Cookies.Keys)
{
Response.Write("<br/>" + key + ": " +
Server.UrlDecode(Request.Cookies[key].Value));
}

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
Thanks all.....

"Steven Cheng[MSFT]" <stcheng@.online.microsoft.comwrote in message
news:pLngR8CwGHA.1992@.TK2MSFTNGXA01.phx.gbl...

Quote:

Originally Posted by

Hi Nalaka,
>
I think the article Gregory provided is quite good and useful.
>
BTW, client-side inserted cookie may escape some special characters. When
reading them in server-side code, you may take care on this, you can use
the UrlDecode method to decode them if the cookie(added at client-side) is
escaped:
>
foreach (string key in Request.Cookies.Keys)
{
Response.Write("<br/>" + key + ": " +
Server.UrlDecode(Request.Cookies[key].Value));
}
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
This posting is provided "AS IS" with no warranties, and confers no
rights.
>

Set a condition before following a hyperlink

In straightforward html I have used the following:-

<a onClick="return checkpass(Checkit.PassToForm.value);" href="http://links.10026.com/?link=myweb3/members.htm">Continue

where checkpass is a function that returns true or false and it has to be true before the user can access the next page.

Validation controls would not be an answer in this particular instance

I have not been able to do the same sort of thing in ASP. onclick = "return Somefunction"; href etc produces an error message "Somefunction isundefined"


In Asp, if the OnClick event is already defined for the webcontrol, then it is expecting a C# function handler. In this case, you will need to use OnClientClick.

I would suggest using a LinkButton. This will ensure that people are validated when going to the next page, cause if javascript is disabled then the link button wont redirect the user.

<asp:LinkButton ID="lb" runat="server" Text="Gohere" OnClientClick="return checkpass("myvalue")" />


You would need preventDefault.http://developer.mozilla.org/en/docs/DOM:event.preventDefault

But it is not supported in old browsers.


Hi

I think the checkpass function is some sort of a client side scripting function (probably javascript). If that is right I would suggest you to open the new page from the javascript function itself rather than using href.

Hope this helps to solve your problem

Good LuckYes

Regards

Vineed


Hi,

Mulozi:

<a onClick="return checkpass(Checkit.PassToForm.value);" href="http://links.10026.com/?link=myweb3/members.htm">Continue

You can redirect the page in the javascript function itself. So your fucntion checks the details and if correct it will transfer to another page.

use the below...

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function checkDetails()
{
if(document.getElementById("txt").value != "")
{
window.location.href="default2.aspx";
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox><br />
<a onclick="checkDetails();" href="http://links.10026.com/?link=http://forums.asp.net/AddPost.aspx?ReplyToPostID=1967328&Quote=False#"> Hi</a></div>
</form>
</body>
</html>


Thanks.

I should have mentioned that I am using VB for this application. Sorry to have put you to the trouble.

I seem to have succeeded in a previous application which used Javascript.

I have to use VB for this one and am trying to find the VB equivalent.

Thee must be an equivalent somehow using VB

I should also have mentioned that I have been using Visual Web Developer 2008 express edition


Thanks.

I haven't been able to succeed with this either

I should have mentioned that I am using VB for this application. Sorry to have put you to the trouble.

I seem to have succeeded in a previous application which used Javascript. and the previous example was Javascript .

I have to use VB for this one and am trying to find the VB equivalent.

There must be an equivalent somehow using VB

Can I not run a script from the command button which would include a conditional go to URL ... ?

I should also have mentioned that I have been using Visual Web Developer 2008 express edition

Any further thoughts gratefully received.


Thanks.

I presume preventDefault is Javascript

I should have mentioned that I am using VB for this application.

I seem to have succeeded in a previous application which used Javascript. and the previous example was Javascript .

I have to use VB for this one and am trying to find the VB equivalent.

There must be an equivalent somehow using VB

Can I not run a script from the command button which would include a conditional go to URL ... ? I have not found a way of doing it, but this must be a common enough requirement.

I should also have mentioned that I have been using Visual Web Developer 2008 express edition

Any further thoughts gratefully received.


Hi,

Are you trying to execute the function on the client-side or server side. If you can execute it on the server side, then instead of a hyperlink use a linkbutton and on the click event of it check the condition that you are looking out for and use Response.Redirect to the new page. That should possibly solve the problem.

Hope this solves your problem

Good LuckYes

Regards

Vineed

Set a default property in a user control?

I'm not even sure if what I'm doing is possible. I created a simple control
that will be reused throughout the site. It will accept a large amount of
HTML with bulleted lists, etc
I want to implement like:
<uc1:KeyFeatures id="KeyFeatures1" runat="server">
Large amount of Inner Html content goes here between tags...
</uc1:KeyFeatures>
Not like:
<uc1:KeyFeatures id="KeyFeatures1" runat="server" InnerHtml="Large amount of
Inner Html content goes here between tags..."></uc1:KeyFeatures>
Can I do it this way? If so, is this considered the 'default' property and h
ow do I declare this in my uc so it accepts the text between the tags?
The code is below..this is a stripped down version.
public class KeyFeatures : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label lblInnerHtml;
public string InnerHtml
{
set
{
lblInnerHtml.Text = value;
}
}If you find can you please tell here how you do?
Iyigun
"Dave" <anonymous@.discussions.microsoft.com> wrote in message
news:243F1108-1CB3-4F93-8386-BE39FC3601CA@.microsoft.com...
> I'm not even sure if what I'm doing is possible. I created a simple
control that will be reused throughout the site. It will accept a large
amount of HTML with bulleted lists, etc
> I want to implement like:
> <uc1:KeyFeatures id="KeyFeatures1" runat="server">
> Large amount of Inner Html content goes here between tags...
> </uc1:KeyFeatures>
> Not like:
> <uc1:KeyFeatures id="KeyFeatures1" runat="server" InnerHtml="Large amount
of Inner Html content goes here between tags..."></uc1:KeyFeatures>
> Can I do it this way? If so, is this considered the 'default' property and
how do I declare this in my uc so it accepts the text between the tags?
> The code is below..this is a stripped down version.
> public class KeyFeatures : System.Web.UI.UserControl
> {
> protected System.Web.UI.WebControls.Label lblInnerHtml;
> public string InnerHtml
> {
> set
> {
> lblInnerHtml.Text = value;
> }
> }
>
>
>
>
>

Set a default property in a user control?

I'm not even sure if what I'm doing is possible. I created a simple control that will be reused throughout the site. It will accept a large amount of HTML with bulleted lists, et

I want to implement like

<uc1:KeyFeatures id="KeyFeatures1" runat="server"
Large amount of Inner Html content goes here between tags..
</uc1:KeyFeatures

Not like

<uc1:KeyFeatures id="KeyFeatures1" runat="server" InnerHtml="Large amount of Inner Html content goes here between tags..."></uc1:KeyFeatures

Can I do it this way? If so, is this considered the 'default' property and how do I declare this in my uc so it accepts the text between the tags?

The code is below..this is a stripped down version.

public class KeyFeatures : System.Web.UI.UserContro

protected System.Web.UI.WebControls.Label lblInnerHtml

public string InnerHtml

se

lblInnerHtml.Text = value
If you find can you please tell here how you do?
Iyigun

"Dave" <anonymous@.discussions.microsoft.com> wrote in message
news:243F1108-1CB3-4F93-8386-BE39FC3601CA@.microsoft.com...
> I'm not even sure if what I'm doing is possible. I created a simple
control that will be reused throughout the site. It will accept a large
amount of HTML with bulleted lists, etc
> I want to implement like:
> <uc1:KeyFeatures id="KeyFeatures1" runat="server">
> Large amount of Inner Html content goes here between tags...
> </uc1:KeyFeatures>
> Not like:
> <uc1:KeyFeatures id="KeyFeatures1" runat="server" InnerHtml="Large amount
of Inner Html content goes here between tags..."></uc1:KeyFeatures>
> Can I do it this way? If so, is this considered the 'default' property and
how do I declare this in my uc so it accepts the text between the tags?
> The code is below..this is a stripped down version.
> public class KeyFeatures : System.Web.UI.UserControl
> {
> protected System.Web.UI.WebControls.Label lblInnerHtml;
> public string InnerHtml
> {
> set
> {
> lblInnerHtml.Text = value;
> }
> }
>
>
>