Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Saturday, March 31, 2012

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 alternate text to empty string

Hi,

I have found that with both the HtmlImage and the Image that if I set the AlternateText property to string.Empty then the 'alt' tag is omitted from the output img tag.

Is there a way around this? The accessibility checkers demand an alt tag even if its empty. If this is not possible, does anyone know if there's much difference between an lt tag set to empty string and an alt tag set to a single space?

Cheers, WT.

the only real difference between setting an alt tag to " " rather than haveing it string.empty is that if left empty it will take it off the form so that .net does not have to keep up with that tag. It's the same thing if you set it to visible = False it just doen't show up on the form. so ultimately it comes down to speed (an almost imeasurable amount). My suggestion is to leave it with one single space.


Ahh, well actually " " results in a dodgy tool tip being displayed! Adding the 'alt' attribute with 'Control.Attributes.Add()' apears to allow empty string alt tags though.


string.empty return a NULL, and a single space is actually an instance of string. they are totally different.
if you assign string.empty to thebase.Attributes["alt"](as shown below), then that "alt" atrribute is actually set to NULL. so, in the render function, it would not be generated to html code. while for a single space, it's not null but an
reference to an realy object, so, "alt" will get rendered. For information i suggest you to look up the .net "control","htmlcontrol" nd "htmlImg" source code. :)
 
public string Alt{get {string text =base.Attributes["alt"];if (text ==null) {return string.Empty; }return text; }set {base.Attributes["alt"] = HtmlControl.MapStringAttributeToString(value); }}

string.Emptyis an instance of a string. Directly setting Attributes["alt"] to string.Empty results in an empty alt tag instead of no alt tag. It must be 'HtmlControl.MapStringAttributeToString' that's returning null when passed string.Empty.


agree. 
internalstaticstringMapStringAttributeToString(string s){if ((s !=null) && (s.Length ==0)) {returnnull; }returns;}
string.Empty is actually an object of string type.

publicstaticreadonlystringEmpty;

the code above is copied fromReflector.Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information.

Set asp:tablecell text from javascript

on my form i got a table with cell like this:
<asp:TableCell ID="tdNavigation_Sub" Runat="server"></asp:TableCell>
To set the text that goes in here from the code behind is easy... just:
protected TableCell tdNavigation_Sub; and tdNavigation_Sub.Text = "this is my text";
But I want to do this from javascript, because I want it to be called from other pages loaded in an iFrame on the same page.
On this kids, I will just call "parent.myscript("blah blah");"...easy

but i canot get the javascript on the main form right...so pls help?
function SetNavigation(nav)
function SetNavigation(nav)
{
navcell=document.getElementById("tdNavigation_Sub");
navcell.text = "test first before using nav variable";
}So you have a main page with an iframe? The iframe contains the tablecells right?
Are you getting a javascript error?
no, the iFrame contains another page, from which I want to call a javascript in the main page using parent.myscript("...
I created a test and got it to work. Let me know if this is what you are looking for...

1-Create a separate .js file...

function changecell()
{
var mycell;
mycell=parent.document.getElementById("tablecell_changeMe");
mycell.innerHTML='this is madness';
}


2-Create a file that will be your iframe...

<%@. Page Language="vb" AutoEventWireup="false" Codebehind="iframe1.aspx.vb" Inherits="TableCellExample.iframe1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>iframe1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript" src="http://pics.10026.com/?src=JScript1.js"></script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 103; LEFT: 32px; WIDTH: 208px; POSITION: absolute; TOP: 40px; HEIGHT: 24px"
onclick="changecell()" type="button" value="Button Test from Iframe">
</form>
</body>
</HTML>

3-Create the main page and add the iframe tag...

<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript" src="http://pics.10026.com/?src=JScript1.js">
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Table id="Table1" style="Z-INDEX: 100; LEFT: 32px; POSITION: absolute; TOP: 32px" runat="server"
Width="208px" Height="120px">
<asp:TableRow></asp:TableRow>
<asp:TableRow></asp:TableRow>
<asp:TableRow>
<asp:TableCell ID="tablecell_changeMe">test cell 1</asp:TableCell>
<asp:TableCell>test cell 2</asp:TableCell>
<asp:TableCell>test cell 3</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br>
<br>
<!-- Okay here is our iframe -->
<iframe id="myiframe" src="http://pics.10026.com/?src=iframe1.aspx" style="Z-INDEX: 101; LEFT: 32px; WIDTH: 392px; POSITION: absolute; TOP: 184px; HEIGHT: 176px">
</iframe><INPUT style="Z-INDEX: 103; LEFT: 32px; WIDTH: 72px; POSITION: absolute; TOP: 400px; HEIGHT: 24px"
type="button" value="Button" onclick="changecell()">
</form>
</body>
</HTML>

Let me know if you have problems with the test above.
Maybe all you needed changed was...

navcell=parent.document.getElementById("tdNavigation_Sub");
Thanks!

Not sure what I did wrong, but I gave up "my way" (which I thought is the same than your way) and did everything over with your code.
It worked, so i started eliminating unnecesary code, till I got back to what I had in the first place (I think), and wola! .. it worked still.

Thanks again mate!
no problem, glad it worked. :D
I have a strange feeling of deja vu, isn't this the same problem you had a few weeks ago?
Yeah..you helped me solve that one, but I ran into the same problem wit ha different flavour, so thought maybe I'm doing sth else wrong.

Just been using JScript a lot lately...I should get a book and work through it, but TIME man!!!

Thursday, March 29, 2012

Set colour of the text in DropDownList?

Hi, how can i set the text colour for each Item in a DropDownList during runtime?Simple CSS. You'd use... for example

<option style="background-color:black;">blah</option>

Of course in your codebehind, since you'll be using ListItems, do this to the listitem.

ListItem.Attributes.Add("style","background-color:black;");
Simple CSS. You'd use... for example

<option style="background-color:black;">blah</option>

Of course in your codebehind, since you'll be using ListItems, do this to the listitem.

ListItem.Attributes.Add("style","background-color:black;");
Thanks for the solution!!!
Add Resolved, dude.

Set controls to nothing when finished?

Hello,
I recently saw some example code that looked similar to the following...

...
Dim literalBr As New System.Web.UI.LiteralControl()
literalBr.Text = "<br>"
literalBr.ID = "anId"
Panel1.Controls.Add(literalBr)
literalBr = Nothing
...

Is there a reason why the literalBr control was set to nothing after adding it to the panel? Does it cause memory usage to go up if you dont set controls to nothing after creating them programmatically?The garbage collector will take care cleaning up literalBr in this case. Setting literalBr to nothing is a waste of time. SeeProgramming for Garbage Collection for more details.

Monday, March 26, 2012

Set default text on databound object

I have an insert template textbox that is bound to a field in SQL.
Since it is the insert template it really doesn't have anything in it.
How do I set the default text? Like for example if I have a insert
template field that is databound to a date sql column how can I set the
text to be a default like todays date?Use the ItemDataBound event. This event fires after the databinding occurs
for each Item, so it occurs the same number of times as the number of Items.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"MasterChief" <constants@.mix-net.net> wrote in message
news:1138744610.650024.116910@.o13g2000cwo.googlegroups.com...
>I have an insert template textbox that is bound to a field in SQL.
> Since it is the insert template it really doesn't have anything in it.
> How do I set the default text? Like for example if I have a insert
> template field that is databound to a date sql column how can I set the
> text to be a default like todays date?
>
I can't seem to find the ItemDataBound event. I can find it for the
gridview but not the formview which is where the insert template I am
using is. I am trying to use the databinding event is that the same
thing as ItemDataBound? Also how do I reference the textbox in the form
view? Like I can't see to go plan_startdateTextBox.text =
DateTime.Today.ToString()
Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)
End Sub
I searched for hours and finally came upon this posting if anybody is
interested.
http://groups.google.com/group/micr...ae822433a4ab209
Here is an example from code that I wrote:
Private Sub datResults_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datResults.ItemDataBound
If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblRegion1"), Label).Text = "*NONE*" Then
CType(e.Item.FindControl("lblRegion1"), Label).Text = " "
End If
If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblRegion2"), Label).Text = "*NONE*" Then
CType(e.Item.FindControl("lblRegion2"), Label).Text = " "
End If
If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblPhone1"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone1"), Label).Text = " "
If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblPhone2"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone2"), Label).Text = " "
End Sub
Notice that at the end of the first line it refers to the event as
datResults.ItemDataBound (this is the standard objectID.eventname format).
As for your question about referencing a textbox (or any other Control used
in your Template), you must use
CType(e.Item.FindControl(controlID),ControlType) to reference the Control.
This is necessary because since there are many Items in a DataList (for
example, my example will include many controls with the ID lblPhone1), you
cannot simply use just the ID. All my sample code is in VB.NET, if you have
any questions let me know.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"MasterChief" <constants@.mix-net.net> wrote in message
news:1138911157.928679.158000@.g47g2000cwa.googlegroups.com...
>I can't seem to find the ItemDataBound event. I can find it for the
> gridview but not the formview which is where the insert template I am
> using is. I am trying to use the databinding event is that the same
> thing as ItemDataBound? Also how do I reference the textbox in the form
> view? Like I can't see to go plan_startdateTextBox.text =
> DateTime.Today.ToString()
> Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
> Object, ByVal e As System.EventArgs)
> End Sub
>

Set default text on databound object

I have an insert template textbox that is bound to a field in SQL.
Since it is the insert template it really doesn't have anything in it.
How do I set the default text? Like for example if I have a insert
template field that is databound to a date sql column how can I set the
text to be a default like todays date?Use the ItemDataBound event. This event fires after the databinding occurs
for each Item, so it occurs the same number of times as the number of Items.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"MasterChief" <constants@.mix-net.net> wrote in message
news:1138744610.650024.116910@.o13g2000cwo.googlegr oups.com...
>I have an insert template textbox that is bound to a field in SQL.
> Since it is the insert template it really doesn't have anything in it.
> How do I set the default text? Like for example if I have a insert
> template field that is databound to a date sql column how can I set the
> text to be a default like todays date?
I can't seem to find the ItemDataBound event. I can find it for the
gridview but not the formview which is where the insert template I am
using is. I am trying to use the databinding event is that the same
thing as ItemDataBound? Also how do I reference the textbox in the form
view? Like I can't see to go plan_startdateTextBox.text =
DateTime.Today.ToString()

Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
Object, ByVal e As System.EventArgs)

End Sub
I searched for hours and finally came upon this posting if anybody is
interested.
http://groups.google.com/group/micr...ae822433a4ab209
Here is an example from code that I wrote:

Private Sub datResults_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datResults.ItemDataBound

If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblRegion1"), Label).Text = "*NONE*" Then

CType(e.Item.FindControl("lblRegion1"), Label).Text = " "

End If

If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblRegion2"), Label).Text = "*NONE*" Then

CType(e.Item.FindControl("lblRegion2"), Label).Text = " "

End If

If e.Item.ItemType = ListItemType.Item AndAlso
CType(e.Item.FindControl("lblPhone1"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone1"), Label).Text = " "

If e.Item.ItemType = ListItemType.AlternatingItem AndAlso
CType(e.Item.FindControl("lblPhone2"), Label).Text = "" Then
CType(e.Item.FindControl("lblPhone2"), Label).Text = " "

End Sub

Notice that at the end of the first line it refers to the event as
datResults.ItemDataBound (this is the standard objectID.eventname format).
As for your question about referencing a textbox (or any other Control used
in your Template), you must use
CType(e.Item.FindControl(controlID),ControlType) to reference the Control.
This is necessary because since there are many Items in a DataList (for
example, my example will include many controls with the ID lblPhone1), you
cannot simply use just the ID. All my sample code is in VB.NET, if you have
any questions let me know.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"MasterChief" <constants@.mix-net.net> wrote in message
news:1138911157.928679.158000@.g47g2000cwa.googlegr oups.com...
>I can't seem to find the ItemDataBound event. I can find it for the
> gridview but not the formview which is where the insert template I am
> using is. I am trying to use the databinding event is that the same
> thing as ItemDataBound? Also how do I reference the textbox in the form
> view? Like I can't see to go plan_startdateTextBox.text =
> DateTime.Today.ToString()
> Protected Sub plan_startdateTextBox_DataBinding(ByVal sender As
> Object, ByVal e As System.EventArgs)
> End Sub

Set DIV text in code behind

if i want to set the text inside a div (or other properties like "class") from my code behind, as what to i declare it? htmlgenericcontrol? If so, what attribute do i set.

html:
<div id="divtest" runat=server></div>
code behind:
protected System.Web.UI.HtmlControls.WHAT?? divtest;
...
...
divtest.WHAT?? = "seems to work";Html part is fine. After you've created it, look at it in the design view, save the page, then go to the codebehind, you should be able to see an html div control (some name)... set its innerText property.
hmmm...not so sure

I always have to code the "protected htmlcontrol whatever mycontrol" myself in my project. VS never do it for me. SOMETIMES, VS will add this for me for asp.net controls though.

Maybe something to do with the fact I ALWAYS hand code my pages...never drag and drop onto form...

...aaah...stupid me..let me drag some html controls onto the form and see what VS do for me... sorry..always talk to myself huh?
It's a VS 2003 bug that I discovered much to my consternation when VS 2003 had just come out. Surprisingly, it was addressed by nobody. Everyone thought it was a feature rather than a bug, I even asked MS developers about it, and they did fix it in VS 2005 but they mentioned that very few people ever brought it up regarding VS 2003.

The workaround was that you have to save the ASPX page before going to the codebehind so that the control's code was added to the codebehind file.

Set dropdownlist from static data

I have a dropdownbox object:

<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList
The values and text are static. I am reading from a table:

Select QuestionType from myTable where id = 10

and QuestionType will either be "MS", "MM" or "TF".

How do I bind the data to this object?

With a Label it would be something like:

<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" /
Thanks,

Tom.Databinding from a SQL table is easy, you should be able to find many
examples out there.

You need to know more about this database you are querying though.

1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104870094.287340.59580@.z14g2000cwz.googlegro ups.com...
> Databinding from a SQL table is easy, you should be able to find many
> examples out there.
> You need to know more about this database you are querying though.
> 1.) Is it a MS SQL server or is it another type?
> 2.) Do you have to login to the server, or does it accept inherited
> credentials?
> 3.) Do you have the database name you are connecting to?

Actuall, no.

How I get the data is not the issue here.

I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.

<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %
I want to do the same type of thing here where my selected item will display
based on the data returned.

Somehow, I need to do something like:

questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %
So that the value will show correctly in the dropdownbox.

Thanks,

Tom
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.

It may require you to go as far as using a custom control for what you
are trying to do.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104963175.704187.310580@.c13g2000cwb.googlegr oups.com...
> Oh so you want the dropdownlist to change the currently selected item
> to the one that's in the table? I have tried doing that in the past but
> haven't had any luck, I'm only intermediate in skill with asp.net so
> hopefully someone else can provide a solution for you.
> It may require you to go as far as using a custom control for what you
> are trying to do.

I hope not.

I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.

Thanks,

Tom.
Yeah, I completely agree with you and what you say makes sense.

Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If

So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group...557b5a7b406fa52
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.

Anyway, I put it in the sub Page_PreRender and it worked just fine.

Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub

<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIndexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList
Hope this helps!
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.
Ignore the above post, I made an error in my code because I was writing
it too fast.

Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1105051197.174515.91240@.f14g2000cwb.googlegro ups.com...
> Ignore the above post, I made an error in my code because I was writing
> it too fast.
> Anyway, this solution works fine, and it's all in sub code which I
> think is better... that way you don't have to scroll between variables
> in your html and your subs, it's all in one place. Again, I hope this
> helps.

I'll look at it and see how it works and let you know.

Thanks for the help,

Tom

Set Dropdown List Default Text

Hi,
Is it possible to have a dropdown list that when the page is first
visited has a selected value of something like "Choose an Option". After the
user has selected an option the the user should not be able to selected the
default value an more. If this is possible could somebody show me a same in
C#.

Thanks
KennethThere are several ways to do that. One would be the SelectedIndexChanged
event. When the event occurs, check the value of the selected item. If it is
the text you don't want selected, remove that item. However, I would suggest
using a validator. The two best candidates would be the
RequiredFieldValidator and the CompareValidator.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/

"Kenneth Keeley" <kenkeeley@.hotmail.com> wrote in message
news:uFCCiVYvFHA.1436@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Is it possible to have a dropdown list that when the page is first
> visited has a selected value of something like "Choose an Option". After
> the
> user has selected an option the the user should not be able to selected
> the
> default value an more. If this is possible could somebody show me a same
> in
> C#.
> Thanks
> Kenneth

Set Dropdown List Default Text

Hi,
Is it possible to have a dropdown list that when the page is first
visited has a selected value of something like "Choose an Option". After the
user has selected an option the the user should not be able to selected the
default value an more. If this is possible could somebody show me a same in
C#.
Thanks
KennethThere are several ways to do that. One would be the SelectedIndexChanged
event. When the event occurs, check the value of the selected item. If it is
the text you don't want selected, remove that item. However, I would suggest
using a validator. The two best candidates would be the
RequiredFieldValidator and the CompareValidator.
--
Nathan Sokalski
njsokalski@.hotmail.com
http://www.nathansokalski.com/
"Kenneth Keeley" <kenkeeley@.hotmail.com> wrote in message
news:uFCCiVYvFHA.1436@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Is it possible to have a dropdown list that when the page is first
> visited has a selected value of something like "Choose an Option". After
> the
> user has selected an option the the user should not be able to selected
> the
> default value an more. If this is possible could somebody show me a same
> in
> C#.
> Thanks
> Kenneth
>

Set dropdownlist from static data

I have a dropdownbox object:
<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList>
The values and text are static. I am reading from a table:
Select QuestionType from myTable where id = 10
and QuestionType will either be "MS", "MM" or "TF".
How do I bind the data to this object?
With a Label it would be something like:
<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" />
Thanks,
Tom.Databinding from a SQL table is easy, you should be able to find many
examples out there.
You need to know more about this database you are querying though.
1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104870094.287340.59580@.z14g2000cwz.googlegroups.com...
> Databinding from a SQL table is easy, you should be able to find many
> examples out there.
> You need to know more about this database you are querying though.
> 1.) Is it a MS SQL server or is it another type?
> 2.) Do you have to login to the server, or does it accept inherited
> credentials?
> 3.) Do you have the database name you are connecting to?
>
Actuall, no.
How I get the data is not the issue here.
I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.
<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %>
I want to do the same type of thing here where my selected item will display
based on the data returned.
Somehow, I need to do something like:
questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %>
So that the value will show correctly in the dropdownbox.
Thanks,
Tom
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.
It may require you to go as far as using a custom control for what you
are trying to do.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104963175.704187.310580@.c13g2000cwb.googlegroups.com...
> Oh so you want the dropdownlist to change the currently selected item
> to the one that's in the table? I have tried doing that in the past but
> haven't had any luck, I'm only intermediate in skill with asp.net so
> hopefully someone else can provide a solution for you.
> It may require you to go as far as using a custom control for what you
> are trying to do.
>
I hope not.
I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.
Thanks,
Tom.
Yeah, I completely agree with you and what you say makes sense.
Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If
So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group...557b5a7b406fa52
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.
Anyway, I put it in the sub Page_PreRender and it worked just fine.
Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub
<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIndexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList>
Hope this helps!
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.
Ignore the above post, I made an error in my code because I was writing
it too fast.
Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.

Set Focus

How do you set the focus to a text box in ASP .NET? ASP Text boxes don't have a focus or setfocus method.Spot on.
Javascript has a "focus" function call :)

Something likedocument.getElementById('txtUsername').focus();

Saturday, March 24, 2012

Set focus and cursor at end of text?

I use this code to set focus to a textbox when I load a page:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

I would also like the cursor to be positioned at the end of the text field.
Using only the above code, the cursor is positioned at the beginning of the
textbox even if there are some text present in the field.

How can I position the cusrsor at the end of the text?

OlavOlav,

Here we go:

Create a new function in your <head> tag in the HTML section:

function SetEnd (TB)
{
if (TB.createTextRange)
{
var FieldRange = TB.createTextRange();
FieldRange.moveStart('character', TB.value.length);
FieldRange.collapse();
FieldRange.select();
}
}

Create a new onfocus JavaScript event in your asp text box tag (asp:textBox)
that calls the SetEnd function above:

<asp:textbox id="TextBox1" runat="server" Width="65px"
onfocus="SetEnd(this)">Existing text</asp:textbox
Make sure you keep your existing code that calls the SetFocus function as it
is:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

Don't forget to call the above SetFocus() function in your Body tag on page
load ....( <body onload="SetFocus()"> )

Good luck!

Regards,

Mohammad Samara.

ICS (London) Ltd.

"Olav Tollefsen" <x@.y.com> wrote in message
news:uxrxLYt3DHA.3656@.TK2MSFTNGP11.phx.gbl...
> I use this code to set focus to a textbox when I load a page:
> private void SetFocus(System.Web.UI.Control ctrl)
> {
> string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
> ctrl.ID + "').focus()</SCRIPT>";
> RegisterStartupScript("focus", s);
> }
> I would also like the cursor to be positioned at the end of the text
field.
> Using only the above code, the cursor is positioned at the beginning of
the
> textbox even if there are some text present in the field.
> How can I position the cusrsor at the end of the text?
> Olav

set focus in text box

Hi, id like to set focus for a text box in my web form but I dont know how. the text boxes do not have a setFocus or a Focus method

Thanks!!

myTextBox.select(); <<<< JavaScript
we use the following function which injects client side script

PublicSharedSub Set_Focus(ByVal aspPageAs System.Web.UI.Page,ByVal strControlNameAsString)

Try

Dim strScriptAsString

strScript ="<script language=javascript> document.all.item(""" & strControlName &""").focus() </script>"

aspPage.RegisterStartupScript("focus", strScript)

Catch exAs Exception
Throw ex
EndTry
EndSub

following then to call you would simply say:- Set_Focus(Me.Page,"txtMyTextbox")


In .NET 2.0, the Focus() method will set focus if you want to do it from code-beside.

HTH,
Ryan

Set Focus in EditItemTemplate text box...

Hi,
Is there a way to set the focus onto a text box that I have set up in my
EditItemTemplate, so that the User can just start typing once they they have
selected to Edit?
Many thanks in advance
Colin BHi,
You could write a little script to set the focus and run it after you start
the edit (using Registerstartupscript, for instance).
Cosmin
"Colin Basterfield" <colinbasterfield@.hotmail.com> wrote in message
news:e2vcK5NTEHA.2580@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Is there a way to set the focus onto a text box that I have set up in my
> EditItemTemplate, so that the User can just start typing once they they
have
> selected to Edit?
> Many thanks in advance
> Colin B
>

Set Focus in EditItemTemplate text box...

Hi,

Is there a way to set the focus onto a text box that I have set up in my
EditItemTemplate, so that the User can just start typing once they they have
selected to Edit?

Many thanks in advance
Colin BHi,

You could write a little script to set the focus and run it after you start
the edit (using Registerstartupscript, for instance).

Cosmin

"Colin Basterfield" <colinbasterfield@.hotmail.com> wrote in message
news:e2vcK5NTEHA.2580@.TK2MSFTNGP12.phx.gbl...
> Hi,
> Is there a way to set the focus onto a text box that I have set up in my
> EditItemTemplate, so that the User can just start typing once they they
have
> selected to Edit?
> Many thanks in advance
> Colin B

Set Focus on a control

Hello,

On a click event, I need to test certain text boxes and then based on their
values go to another text box. How do you set the focus using vb?

--
Thanks in advance,

StevenLook at the "Focus in WebForms" thread earlier today...

Set focus on a text box

Dear All,
I have developed a aspx web page with c# as page language. I have place two
text boxes and two requiredfield validator control for each text box. Now
whenever i a text box is null, i want to set the focus on the text box apart
from displaying the error message. Please help.
Regards,
SwamiOne way is to handle the onBlur event of the text box:
<asp:TextBox runat="server" id="txt" onblur="if (this.value.replace(/\s/g,
'').length == 0) this.focus();">
"Swami" <Swami@.discussions.microsoft.com> wrote in message
news:CEB6C427-6924-4D9F-B731-7E560DB05FEC@.microsoft.com...
Dear All,
I have developed a aspx web page with c# as page language. I have place two
text boxes and two requiredfield validator control for each text box. Now
whenever i a text box is null, i want to set the focus on the text box apart
from displaying the error message. Please help.
Regards,
Swami
The easiest, most reliable way is client side java script:
MyControl.focus();
Here's more info:
http://wp.netscape.com/eng/mozilla/...f_f-g.htm#59872
You might also consider using this free control:
http://www.metabuilders.com/Tools/FirstFocus.aspx
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com
"Swami" <Swami@.discussions.microsoft.com> wrote in message
news:CEB6C427-6924-4D9F-B731-7E560DB05FEC@.microsoft.com...
> Dear All,
> I have developed a aspx web page with c# as page language. I have place
> two
> text boxes and two requiredfield validator control for each text box. Now
> whenever i a text box is null, i want to set the focus on the text box
> apart
> from displaying the error message. Please help.
> Regards,
> Swami
Check out this code snippet,
http://www.extremeexperts.com/Net/C...ck.
aspx
Saravana
http://dotnetjunkies.com/WebLog/saravana/
www.ExtremeExperts.com
"Swami" <Swami@.discussions.microsoft.com> wrote in message
news:CEB6C427-6924-4D9F-B731-7E560DB05FEC@.microsoft.com...
> Dear All,
> I have developed a aspx web page with c# as page language. I have place
two
> text boxes and two requiredfield validator control for each text box. Now
> whenever i a text box is null, i want to set the focus on the text box
apart
> from displaying the error message. Please help.
> Regards,
> Swami

Set focus on a text box

Dear All,
I have developed a aspx web page with c# as page language. I have place two
text boxes and two requiredfield validator control for each text box. Now
whenever i a text box is null, i want to set the focus on the text box apart
from displaying the error message. Please help.

Regards,
SwamiOne way is to handle the onBlur event of the text box:

<asp:TextBox runat="server" id="txt" onblur="if (this.value.replace(/\s/g,
'').length == 0) this.focus();"
"Swami" <Swami@.discussions.microsoft.com> wrote in message
news:CEB6C427-6924-4D9F-B731-7E560DB05FEC@.microsoft.com...
Dear All,
I have developed a aspx web page with c# as page language. I have place two
text boxes and two requiredfield validator control for each text box. Now
whenever i a text box is null, i want to set the focus on the text box apart
from displaying the error message. Please help.

Regards,
Swami
The easiest, most reliable way is client side javascript:

MyControl.focus();

Here's more info:
http://wp.netscape.com/eng/mozilla/...f_f-g.htm#59872

You might also consider using this free control:
http://www.metabuilders.com/Tools/FirstFocus.aspx

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"Swami" <Swami@.discussions.microsoft.com> wrote in message
news:CEB6C427-6924-4D9F-B731-7E560DB05FEC@.microsoft.com...
> Dear All,
> I have developed a aspx web page with c# as page language. I have place
> two
> text boxes and two requiredfield validator control for each text box. Now
> whenever i a text box is null, i want to set the focus on the text box
> apart
> from displaying the error message. Please help.
> Regards,
> Swami
Check out this code snippet,
http://www.extremeexperts.com/Net/C...erPostback.aspx

--
Saravana
http://dotnetjunkies.com/WebLog/saravana/
www.ExtremeExperts.com

"Swami" <Swami@.discussions.microsoft.com> wrote in message
news:CEB6C427-6924-4D9F-B731-7E560DB05FEC@.microsoft.com...
> Dear All,
> I have developed a aspx web page with c# as page language. I have place
two
> text boxes and two requiredfield validator control for each text box. Now
> whenever i a text box is null, i want to set the focus on the text box
apart
> from displaying the error message. Please help.
> Regards,
> Swami