Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Saturday, March 31, 2012

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 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;
> }
> }
>
>
>

set a file upload control to a data column within a formview edit item template

I have a formview with an edit template which contains a textbox which has "ProductImageUrl" data column bound to it. I want to bind this to a fileupload control instead so that an image can be selected. There is no text field for a fileupload control.

Any ideas?

Thanks

Andrew

Hi, Andrew

Do you want to get the full path of the image? If so, you can use <input id="file1" type="file" ...> then you can get the full path using fil1.value.

Hope this helps.


Is there a way of just getting the file name and not the full path. Can you give me an example of the code behind for using the fil1.value bit.

Thanks

Andrew


Hello

If you want filename, not the full path, you can useFileUpload control and FileUpload1.FileName is that you want.

As to the code, it's very easy.

<%@. Page Language="C#" AutoEventWireup="True" %>

<script runat="server">

void Button1_Click(object Source, EventArgs e)
{

if (Text1.Value == "")
{
Span1.InnerHtml = "Error: You must enter a file name." + File1.Value;
return;
}

if (File1.PostedFile.ContentLength > 0)
{
try
{
File1.PostedFile.SaveAs("c:\\temp\\" + Text1.Value);
Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
Text1.Value + "</b> on the Web server.";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
Text1.Value + "</b><br>" + exc.ToString() + ".";


}
}

}
void Click(Object src, EventArgs s)
{
Span1.InnerHtml = File1.Value;
}
</script>

<html>
<head>
<title>HtmlInputFile Example</title>
</head>
<body>

<h3>HtmlInputFile Example</h3>

<form id="Form1" enctype="multipart/form-data"
runat="server">

Select File to Upload:
<input id="File1"
type="file" onselect="Click"
runat="server">

<p>
Save as file name (no path):
<input id="Text1"
type="text"
runat="server">

</p>
<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />

</p>
<p>
<input type=button
id="Button1"
value="Upload"
onserverclick="Button1_Click"
runat="server">

</p>

</form>

</body>
</html>

Just click "browser" and then click "upload", the "span" will show the File1.Value.


You can get the file name with the following code

<input type="File" runat="server" ID="InputFile" />

To get the file name use the below line

InputFile.PostedFile.FileName


to get only the filename in a fullpath use

System.IO.Path.GetFileName(fullpath)

HC


The difficult part is that the textbox3 and fileupload controls are within a loginview and a detailsview so doing the findcontrol bit is twisting my melon considerably at the moment.

Any help gratefully received.

Thanks

Andrew


Here is my code

<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate></LoggedInTemplate>
<AnonymousTemplate>
You need to be logged in and be an Administrator to see this page.
</AnonymousTemplate>
<RoleGroups>
<asp:RoleGroup roles="Administrator">
<ContentTemplate>
<h3>Click Edit to view and edit product details and description</h3>
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False" DataSourceID="ObjectDataSource1" Height="50px" Width="100%" DataKeyNames="ProductItemID" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" AutoGenerateDeleteButton="True" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<Fields>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="ProductDescription" HeaderText="ProductDescription" SortExpression="ProductDescription" Visible="False" />
<asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" SortExpression="ProductPrice" />
<asp:TemplateField HeaderText="ProductImageUrl" SortExpression="ProductImageUrl">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProductImageUrl") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ProductImageUrl") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductImageUrl") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductCategoryID" SortExpression="ProductCategoryID">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ProductCategoryID") %>'></asp:TextBox>
cat id<br /> </EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ProductCategoryID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductCategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<FTB:FreeTextBox id="ProductDescriptionTextBox" runat="server" Text='<%# Bind("ProductDescription") %>'>
</FTB:FreeTextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>

</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductItemID") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<img src="http://pics.10026.com/?src=images/<%# Eval("ProductImageUrl") %>" style="width:100px;" />

</ItemTemplate>
</asp:TemplateField>
</Fields>
<EmptyDataTemplate>
<strong>Product successfully deleted</strong>
</EmptyDataTemplate>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<AlternatingRowStyle BackColor="#F7F7F7" />
</asp:DetailsView>
<br />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/editproducts.aspx">Go back to edit products</asp:HyperLink><br />
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="{0}"
SelectMethod="GetDataByProductItemID" TypeName="DataSet3TableAdapters.DataTable1TableAdapter" UpdateMethod="UpdateQuery" InsertMethod="InsertQuery" DeleteMethod="DeleteQuery">
<SelectParameters>
<asp:QueryStringParameter Name="ProductItemID" QueryStringField="ProductItemID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="ProductDescription" Type="String" />
<asp:Parameter Name="ProductPrice" Type="Int32" />
<asp:Parameter Name="ProductImageUrl" Type="String" />
<asp:Parameter Name="WebsiteCategoryID" Type="Int32" />
<asp:Parameter Name="ProductCategoryID" Type="Int32" />
<asp:Parameter Name="ProductItemID" Type="Int32" />

</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductCategoryID" Type="Int32" />
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="ProductDescription" Type="String" />
<asp:Parameter Name="ProductImageUrl" Type="String" />
<asp:Parameter Name="WebsiteCategoryID" Type="Int32" />
<asp:Parameter Name="ProductPrice" Type="Int32" />
</InsertParameters>
<DeleteParameters>
<asp:Parameter Name="ProductItemID" Type="Int32" />

</DeleteParameters>
</asp:ObjectDataSource>
<br />
<br />
<br />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>


</asp:LoginView>

Set Asp.Net web control ID

Hello,
Can someone tell me where can I find a list of the characters which I
can use in a web control ID?
Can I for example do:
MyLabel.ID = "Namespace#Control_Type"
Thanks,
Miguelshould be a legal xml id. (note: leading special chars are not allowed - ms
breaks this rule)
reg expression: [A-Za-z][A-Za-z0-9:_.-]*
if the id is used with a runat server, then it needs to be a valid .net
identifier.
-- bruce (sqlwork.com)
"shapper" <mdmoura@.gmail.com> wrote in message
news:1161701364.541401.15700@.k70g2000cwa.googlegroups.com...
> Hello,
> Can someone tell me where can I find a list of the characters which I
> can use in a web control ID?
> Can I for example do:
> MyLabel.ID = "Namespace#Control_Type"
> Thanks,
> Miguel
>

Set Asp.Net web control ID

Hello,

Can someone tell me where can I find a list of the characters which I can use in a web control ID?

Can I for example do:

MyLabel.ID = "Namespace#Control_Type"

Thanks,

Miguel

If you want to be safe, stick with letters and numbers (starting with a letter). Why would you need to worry about other characters?


System.Web.UI documentation for the Control.ID property states only this:

Including spaces in this property will cause an ASP.NET page parser error.

Just for fun, I created 256 labels, each using 1 of the ASCII characters:

for (Int32 counter = 0; counter <= 255; counter++)
{
try
{
Label lbl = new Label();
lbl.ID = "test_ " + Convert.ToChar(counter) + "test";
lbl.Text = "My ID is '" + lbl.ID + "'<br>";
Controls.Add(lbl);
}
catch
{
Label lbl = new Label();
lbl.Text = "ERROR USING CHARACTER '" + Convert.ToChar(counter) + "'<br>";
Controls.Add(lbl);
}
}

It generated no errors. So, I assume you can use any characters you want.

However, you may not use spaces if you declare the label in your HTML markup. For example, this throws an error:

<asp:Label ID="test test" runat="server" />


You, sir, have too much time on your hands. I envy you your four seasons...

Actually, that's a nice little demo, but you probably should consider a few more things. First off, I'd bet that there are other characters that the compiler doesn't like in markup (though of course I'm just assuming). Second, what happens when these controls need to be picked up on postback, or -- more likely to cause a problem -- accessed by client-side code? If nothing else, using chars like underscores, dollar signs, pound signs, and dots are going to confuse the heck out of anyone in the rendered HTML trying to tell what belongs to the control and what part is from the naming container.

Cheers,

Set Asp.Net web control ID

Hello,

Can someone tell me where can I find a list of the characters which I
can use in a web control ID?

Can I for example do:

MyLabel.ID = "Namespace#Control_Type"

Thanks,

Miguelshould be a legal xml id. (note: leading special chars are not allowed - ms
breaks this rule)

reg expression: [A-Za-z][A-Za-z0-9:_.-]*

if the id is used with a runat server, then it needs to be a valid .net
identifier.

-- bruce (sqlwork.com)

"shapper" <mdmoura@.gmail.comwrote in message
news:1161701364.541401.15700@.k70g2000cwa.googlegro ups.com...

Quote:

Originally Posted by

Hello,
>
Can someone tell me where can I find a list of the characters which I
can use in a web control ID?
>
Can I for example do:
>
MyLabel.ID = "Namespace#Control_Type"
>
Thanks,
>
Miguel
>

Thursday, March 29, 2012

Set Conditional AutoPostBack In User Control

Assume that a user control has a TextBox (Page1.ascx)

<asp:TextBox ID="txtName" runat="server"/>

I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:

<%@dotnet.itags.org. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
........
........
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>

When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.

One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):

Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub

& then in the ASCX page

<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>

Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?

Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.If you want to do this as simply as possible, you will have the checkbox
auto postback to set the value. If you want this fully dynamic, you will
have to write JavaScript that creates a postback event on the textbox and
removes it via checking/unchecking the box.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/
*************************************************
Think Outside the Box!
*************************************************
<rn5a@.rediffmail.comwrote in message
news:1160321009.138108.22940@.c28g2000cwb.googlegro ups.com...

Quote:

Originally Posted by

Assume that a user control has a TextBox (Page1.ascx)
>
<asp:TextBox ID="txtName" runat="server"/>
>
I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:
>
<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
........
........
End Sub
</script>
>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
>
When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.
>
One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):
>
Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
& then in the ASCX page
>
<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>
>
Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?
>
Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.
>


It's simple (i am assuming you want to use postbacks instead of dynamic
javascript). Just create a property for user control that will expose
autopostback for the text box as shown below (i defined it in code behind of
the user control)

Public Class Page1
Inherits System.Web.UI.UserControl

Public Property TextBoxAutoPostBack() As Boolean
Get
Return txtName.AutoPostBack
End Get
Set(ByVal value As Boolean)
txtName.AutoPostBack = value
End Set
End Property

End Class

Now use it in aspx page like this

<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
mpc.TextBoxAutoPostBack = CType(obj, CheckBox).Checked
End Sub
</script>

<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>

Autopostback is serialized to viewstate by default so you page contains it
between postbacks. That's all.

Hope this helps
--

Milosz Skalecki
MCP, MCAD

"rn5a@.rediffmail.com" wrote:

Quote:

Originally Posted by

Assume that a user control has a TextBox (Page1.ascx)
>
<asp:TextBox ID="txtName" runat="server"/>
>
I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <formtags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:
>
<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
........
........
End Sub
</script>
>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
>
When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.
>
One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):
>
Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
>
& then in the ASCX page
>
<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>
>
Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?
>
Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.
>
>

Set Conditional AutoPostBack In User Control

Assume that a user control has a TextBox (Page1.ascx)
<asp:TextBox ID="txtName" runat="server"/>
I am encapsulating the above user control in a ASPX page named
Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
CheckBox in this ASPX page within the <form> tags. Note that the
CheckBox IS NOT a part of the user control. It has been explicitly
added to the Form existing in the ASPX page. The AutoPostBack property
of the CheckBox is set to True so that whenever the CheckBox is
checked/unchecked by the user, a sub named 'chk_Check' gets executed.
This is the ASPX code:
<%@dotnet.itags.org. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
.......
.......
End Sub
</script>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
When the page loads for the first time, the CheckBox is unchecked by
default. Now what I want is if the CheckBox is checked by the user,
then the AutoPostBack property of the TextBox existing in the user
control (whose ID is txtName) should be set to True. If the CheckBox is
unchecked by the user, then the AutoPostBack property of the TextBox
named txtName in the user control should be set to False.
One way of doing this is to make use of a Session variable...something
like this (in the ASPX page):
Sub Page_Load(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
Sub chk_Check(.....)
If (chk1.Checked = True) Then
Session("chk") = 1
Else
Session("chk") = 0
End If
End Sub
& then in the ASCX page
<script runat="server">
Sub Page_Load(.....)
If (Session("chk") = 1) Then
txtName.AutoPostBack = True
Else
txtName.AutoPostBack = False
End If
End Sub
</script>
Are there any other ways to accomplish this logic since, as far as
possible, I would like to avoid using Session variables?
Moreover, are there any major drawbacks of the above logic? Of course,
one of them being Session variables are memory resident & another being
users' browser should have cookies enabled although on most occasions,
cookies remain enabled.If you want to do this as simply as possible, you will have the checkbox
auto postback to set the value. If you want this fully dynamic, you will
have to write JavaScript that creates a postback event on the textbox and
removes it via checking/unchecking the box.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com/
****************************************
*********
Think Outside the Box!
****************************************
*********
<rn5a@.rediffmail.com> wrote in message
news:1160321009.138108.22940@.c28g2000cwb.googlegroups.com...
> Assume that a user control has a TextBox (Page1.ascx)
> <asp:TextBox ID="txtName" runat="server"/>
> I am encapsulating the above user control in a ASPX page named
> Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
> CheckBox in this ASPX page within the <form> tags. Note that the
> CheckBox IS NOT a part of the user control. It has been explicitly
> added to the Form existing in the ASPX page. The AutoPostBack property
> of the CheckBox is set to True so that whenever the CheckBox is
> checked/unchecked by the user, a sub named 'chk_Check' gets executed.
> This is the ASPX code:
> <%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
> <script runat="server">
> Sub chk_Check(obj As Object, ea As EventArgs)
> ........
> ........
> End Sub
> </script>
> <form runat="server">
> <MyPage:Ctrl ID="mpc" runat="server"/><br>
> <asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
> AutoPostBack="true" Text="Check Me" runat="server"/>
> </form>
> When the page loads for the first time, the CheckBox is unchecked by
> default. Now what I want is if the CheckBox is checked by the user,
> then the AutoPostBack property of the TextBox existing in the user
> control (whose ID is txtName) should be set to True. If the CheckBox is
> unchecked by the user, then the AutoPostBack property of the TextBox
> named txtName in the user control should be set to False.
> One way of doing this is to make use of a Session variable...something
> like this (in the ASPX page):
> Sub Page_Load(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> Sub chk_Check(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> & then in the ASCX page
> <script runat="server">
> Sub Page_Load(.....)
> If (Session("chk") = 1) Then
> txtName.AutoPostBack = True
> Else
> txtName.AutoPostBack = False
> End If
> End Sub
> </script>
> Are there any other ways to accomplish this logic since, as far as
> possible, I would like to avoid using Session variables?
> Moreover, are there any major drawbacks of the above logic? Of course,
> one of them being Session variables are memory resident & another being
> users' browser should have cookies enabled although on most occasions,
> cookies remain enabled.
>
It's simple (i am assuming you want to use postbacks instead of dynamic
javascript). Just create a property for user control that will expose
autopostback for the text box as shown below (i defined it in code behind of
the user control)
Public Class Page1
Inherits System.Web.UI.UserControl
Public Property TextBoxAutoPostBack() As Boolean
Get
Return txtName.AutoPostBack
End Get
Set(ByVal value As Boolean)
txtName.AutoPostBack = value
End Set
End Property
End Class
Now use it in aspx page like this
<%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
<script runat="server">
Sub chk_Check(obj As Object, ea As EventArgs)
mpc.TextBoxAutoPostBack = CType(obj, CheckBox).Checked
End Sub
</script>
<form runat="server">
<MyPage:Ctrl ID="mpc" runat="server"/><br>
<asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
AutoPostBack="true" Text="Check Me" runat="server"/>
</form>
Autopostback is serialized to viewstate by default so you page contains it
between postbacks. That's all.
Hope this helps
--
Milosz Skalecki
MCP, MCAD
"rn5a@.rediffmail.com" wrote:

> Assume that a user control has a TextBox (Page1.ascx)
> <asp:TextBox ID="txtName" runat="server"/>
> I am encapsulating the above user control in a ASPX page named
> Page1.aspx i.e. the ASPX page displays a single TextBox. There's also a
> CheckBox in this ASPX page within the <form> tags. Note that the
> CheckBox IS NOT a part of the user control. It has been explicitly
> added to the Form existing in the ASPX page. The AutoPostBack property
> of the CheckBox is set to True so that whenever the CheckBox is
> checked/unchecked by the user, a sub named 'chk_Check' gets executed.
> This is the ASPX code:
> <%@. Register TagPrefix="MyPage" TagName="Ctrl" src="http://pics.10026.com/?src=Page1.ascx" %>
> <script runat="server">
> Sub chk_Check(obj As Object, ea As EventArgs)
> ........
> ........
> End Sub
> </script>
> <form runat="server">
> <MyPage:Ctrl ID="mpc" runat="server"/><br>
> <asp:CheckBox ID="chk1" OnCheckedChanged="chk_Check"
> AutoPostBack="true" Text="Check Me" runat="server"/>
> </form>
> When the page loads for the first time, the CheckBox is unchecked by
> default. Now what I want is if the CheckBox is checked by the user,
> then the AutoPostBack property of the TextBox existing in the user
> control (whose ID is txtName) should be set to True. If the CheckBox is
> unchecked by the user, then the AutoPostBack property of the TextBox
> named txtName in the user control should be set to False.
> One way of doing this is to make use of a Session variable...something
> like this (in the ASPX page):
> Sub Page_Load(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> Sub chk_Check(.....)
> If (chk1.Checked = True) Then
> Session("chk") = 1
> Else
> Session("chk") = 0
> End If
> End Sub
> & then in the ASCX page
> <script runat="server">
> Sub Page_Load(.....)
> If (Session("chk") = 1) Then
> txtName.AutoPostBack = True
> Else
> txtName.AutoPostBack = False
> End If
> End Sub
> </script>
> Are there any other ways to accomplish this logic since, as far as
> possible, I would like to avoid using Session variables?
> Moreover, are there any major drawbacks of the above logic? Of course,
> one of them being Session variables are memory resident & another being
> users' browser should have cookies enabled although on most occasions,
> cookies remain enabled.
>

set control on masterpage

Is it possible to set a control on the masterpage from a content page.
Or can you only place stuff in the contentplace holder.
f.e i want to place a label on the masterpage.
and put info in it from a contentpage created from the masterpage
thanxI don't think so and probably this violates the benefit of master pages.
Hi,

Pirate: This can be useful if you have a label on your page and want to change it to reflect what is in your content and I don't see a problem with this myself but my ears are always open to suggestions...

spoofer: You can do it like this...
In your master page define a property public string setLabel
{
set { this.lblTitle.Text = value; }
get { return this.lblTitle.Text; }
}

Then in your content page at the top of the .aspx<%@. MasterType VirtualPath="~/base.master" %>
and in your content page codebehindthis.Master.setLabel = "This is new label";
I'm just following what's called 'best practice' from MS... :p.I've not seen anyone doing it this way.It could be right but rarely used!
Thanx Fishcake, just what i was looking for!
Example + Demo

here:
http://www.geula.biz/works/sample/asp%20net/addControlFromTheContentToTheMaster.aspx
I'm just following what's called 'best practice' from MS... :p.I've not seen anyone doing it this way.It could be right but rarely used!
Not necessarily 'best', then.

There are certain dependencies that do arise in some applications, because a Master Page cannot necessarily be so static. It's also why Master Pages are actually controls that are injected into a page that 'uses' the master page.

Set DataList SelectedIndex to data record

I want to set the SelectedIndex of a DataList control to equal a specific
record in the database. What is the easiest way to accomplish this??

Thanks,
David D. McCroryI know this is a simple request, but I could sure use some
help.....thanks...
"David D. McCrory" <dmccrory@.alltell.net> wrote in message
news:eylWxCV3DHA.1924@.TK2MSFTNGP10.phx.gbl...
> I want to set the SelectedIndex of a DataList control to equal a specific
> record in the database. What is the easiest way to accomplish this??
>
> Thanks,
> David D. McCrory
I'm not sure this is possible. What are you trying to accomplish? There
exists no dependency on a control and its durable datasource - at least not
yet. ASP 2.0 promises to change this.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"David D. McCrory" <dmccrory@.alltell.net> wrote in message
news:eylWxCV3DHA.1924@.TK2MSFTNGP10.phx.gbl...
> I want to set the SelectedIndex of a DataList control to equal a specific
> record in the database. What is the easiest way to accomplish this??
>
> Thanks,
> David D. McCrory

Monday, March 26, 2012

Set default Tag Prefix and CSS?

Hello,
I am creating a web server control derived from
datagrid for the first time. Now I would like to set the
prefix of my control by default to SK instead of CC1. Is
that possible? The other question is that my datagrid
should use a default stylesheet. I have set the cssclass
property of all items which I want to change. BUT I am not
setting the link tag in the head section to my css. I dont
want to do that. I would like to do that direclty in my
component. Is that possible?
ThanksAlternatingItemStyle
ItemStyle
HeaderStyle
footerstyle
if u set css for these four element, css will apply for u're componet. plz
see blow , I explained how we hve to use sample class...
<asp:datagrid id="dgActivity" runat="server" AllowPaging="True"
Class="DataGridStyle" Width="100%"
CellPadding="2" PageSize="30" AutoGenerateColumns="False" AllowSorting="True
">
<AlternatingItemStyle
CssClass="DataGridAlterItemStyle"></AlternatingItemStyle>
<ItemStyle CssClass="DataGridItemStyle"></ItemStyle>
<HeaderStyle CssClass="DataGridHeaderStyle"></HeaderStyle>
.DataGridStyle
{
border-color : White;
border-style : solid;
}
.DataGridStyle td
{
border-color : White;
border-style : solid;
}
.DataGridHeaderStyle
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a:link
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a:hover
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
.DataGridHeaderStyle a:visited
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
"SK" wrote:

> Hello,
> I am creating a web server control derived from
> datagrid for the first time. Now I would like to set the
> prefix of my control by default to SK instead of CC1. Is
> that possible? The other question is that my datagrid
> should use a default stylesheet. I have set the cssclass
> property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont
> want to do that. I would like to do that direclty in my
> component. Is that possible?
> Thanks
>
Hello SK,
Look at the [assembly:TagPrefix] attribute. You'll need to reference System.Web.UI,
if you're not already.
[assembly:TagPrefix("YourNamespace", "YourPrefix")]
Matt Berther
http://www.mattberther.com

> Hello,
> I am creating a web server control derived from datagrid for the
> first time. Now I would like to set the prefix of my control by
> default to SK instead of CC1. Is that possible? The other question is
> that my datagrid should use a default stylesheet. I have set the
> cssclass property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont want to do
> that. I would like to do that direclty in my component. Is that
> possible?
> Thanks
>

Set default Tag Prefix and CSS?

Hello,

I am creating a web server control derived from
datagrid for the first time. Now I would like to set the
prefix of my control by default to SK instead of CC1. Is
that possible? The other question is that my datagrid
should use a default stylesheet. I have set the cssclass
property of all items which I want to change. BUT I am not
setting the link tag in the head section to my css. I dont
want to do that. I would like to do that direclty in my
component. Is that possible?

ThanksAlternatingItemStyle
ItemStyle
HeaderStyle
footerstyle

if u set css for these four element, css will apply for u're componet. plz
see blow , I explained how we hve to use sample class...
<asp:datagrid id="dgActivity" runat="server" AllowPaging="True"
Class="DataGridStyle" Width="100%"
CellPadding="2" PageSize="30" AutoGenerateColumns="False" AllowSorting="True">
<AlternatingItemStyle
CssClass="DataGridAlterItemStyle"></AlternatingItemStyle>
<ItemStyle CssClass="DataGridItemStyle"></ItemStyle>
<HeaderStyle CssClass="DataGridHeaderStyle"></HeaderStyle
..DataGridStyle
{
border-color : White;
border-style : solid;
}
..DataGridStyle td
{
border-color : White;
border-style : solid;
}
..DataGridHeaderStyle
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";

}
..DataGridHeaderStyle a
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
..DataGridHeaderStyle a:link
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
..DataGridHeaderStyle a:hover
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}
..DataGridHeaderStyle a:visited
{
font-size : 8pt;
font-family : Verdana;
font-weight : bold;
vertical-align : middle;
text-align : center;
color:White;
text-align : left;
background-color : "#6487DC";
}

"SK" wrote:

> Hello,
> I am creating a web server control derived from
> datagrid for the first time. Now I would like to set the
> prefix of my control by default to SK instead of CC1. Is
> that possible? The other question is that my datagrid
> should use a default stylesheet. I have set the cssclass
> property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont
> want to do that. I would like to do that direclty in my
> component. Is that possible?
> Thanks
Hello SK,

Look at the [assembly:TagPrefix] attribute. You'll need to reference System.Web.UI,
if you're not already.

[assembly:TagPrefix("YourNamespace", "YourPrefix")]

--
Matt Berther
http://www.mattberther.com

> Hello,
> I am creating a web server control derived from datagrid for the
> first time. Now I would like to set the prefix of my control by
> default to SK instead of CC1. Is that possible? The other question is
> that my datagrid should use a default stylesheet. I have set the
> cssclass property of all items which I want to change. BUT I am not
> setting the link tag in the head section to my css. I dont want to do
> that. I would like to do that direclty in my component. Is that
> possible?
> Thanks

Set display order of ListBox control

Hi
I have a ListBox control in my ASP.NET page. After I binding data to this
control, I would like to be able to change this display order of items in
this control. Just like change layout function of my.yahoo.com.
I tried client side approach, yes I can change display order by JavaScript,
but after clicking one of the button the display order will reverse back to
its original status. Because after PostBack, the page will try to render
this control by viewstate again.

I tried server side approach, but looks there is no way to switch two
ListItems programmatically.

Is there anybody who has some good idea?

Thanks!

--

WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours HardyI already solved this problem.

"Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
news:upQn0K$KEHA.628@.TK2MSFTNGP11.phx.gbl...
> Hi
> I have a ListBox control in my ASP.NET page. After I binding data to this
> control, I would like to be able to change this display order of items in
> this control. Just like change layout function of my.yahoo.com.
> I tried client side approach, yes I can change display order by
JavaScript,
> but after clicking one of the button the display order will reverse back
to
> its original status. Because after PostBack, the page will try to render
> this control by viewstate again.
> I tried server side approach, but looks there is no way to switch two
> ListItems programmatically.
>
> Is there anybody who has some good idea?
> Thanks!
> --
>
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
How?
--
Joe Fallon

"Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
news:O7ZTxp$KEHA.2244@.tk2msftngp13.phx.gbl...
> I already solved this problem.
>
> "Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
> news:upQn0K$KEHA.628@.TK2MSFTNGP11.phx.gbl...
> > Hi
> > I have a ListBox control in my ASP.NET page. After I binding data to
this
> > control, I would like to be able to change this display order of items
in
> > this control. Just like change layout function of my.yahoo.com.
> > I tried client side approach, yes I can change display order by
> JavaScript,
> > but after clicking one of the button the display order will reverse back
> to
> > its original status. Because after PostBack, the page will try to render
> > this control by viewstate again.
> > I tried server side approach, but looks there is no way to switch two
> > ListItems programmatically.
> > Is there anybody who has some good idea?
> > Thanks!
> > --
> > WWW: http://hardywang.1accesshost.com
> > ICQ: 3359839
> > yours Hardy
Dump content of listbox to array, sort array in memory, then clear this
listbox and add listitem from array one by one.

--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"Joe Fallon" <jfallon1@.nospamtwcny.rr.com> wrote in message
news:%23nd3uPALEHA.3052@.TK2MSFTNGP12.phx.gbl...
> How?
> --
> Joe Fallon
>
> "Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
> news:O7ZTxp$KEHA.2244@.tk2msftngp13.phx.gbl...
> > I already solved this problem.
> > "Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
> > news:upQn0K$KEHA.628@.TK2MSFTNGP11.phx.gbl...
> > > Hi
> > > I have a ListBox control in my ASP.NET page. After I binding data to
> this
> > > control, I would like to be able to change this display order of items
> in
> > > this control. Just like change layout function of my.yahoo.com.
> > > I tried client side approach, yes I can change display order by
> > JavaScript,
> > > but after clicking one of the button the display order will reverse
back
> > to
> > > its original status. Because after PostBack, the page will try to
render
> > > this control by viewstate again.
> > > > I tried server side approach, but looks there is no way to switch two
> > > ListItems programmatically.
> > > > > Is there anybody who has some good idea?
> > > > Thanks!
> > > > --
> > > > > > WWW: http://hardywang.1accesshost.com
> > > ICQ: 3359839
> > > yours Hardy
> >
Yep.
That is what my listbox sort routine does.
Just wondering if you came up with something different.
--
Joe Fallon

"Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
news:uVlWuSFLEHA.3808@.TK2MSFTNGP12.phx.gbl...
> Dump content of listbox to array, sort array in memory, then clear this
> listbox and add listitem from array one by one.
> --
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
> "Joe Fallon" <jfallon1@.nospamtwcny.rr.com> wrote in message
> news:%23nd3uPALEHA.3052@.TK2MSFTNGP12.phx.gbl...
> > How?
> > --
> > Joe Fallon
> > "Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
> > news:O7ZTxp$KEHA.2244@.tk2msftngp13.phx.gbl...
> > > I already solved this problem.
> > > > > "Hardy Wang" <hardy_wang@.marketrend.com> wrote in message
> > > news:upQn0K$KEHA.628@.TK2MSFTNGP11.phx.gbl...
> > > > Hi
> > > > I have a ListBox control in my ASP.NET page. After I binding data to
> > this
> > > > control, I would like to be able to change this display order of
items
> > in
> > > > this control. Just like change layout function of my.yahoo.com.
> > > > I tried client side approach, yes I can change display order by
> > > JavaScript,
> > > > but after clicking one of the button the display order will reverse
> back
> > > to
> > > > its original status. Because after PostBack, the page will try to
> render
> > > > this control by viewstate again.
> > > > > > I tried server side approach, but looks there is no way to switch
two
> > > > ListItems programmatically.
> > > > > > > > Is there anybody who has some good idea?
> > > > > > Thanks!
> > > > > > --
> > > > > > > > > > WWW: http://hardywang.1accesshost.com
> > > > ICQ: 3359839
> > > > yours Hardy
> > > > > >

Set Focus

When i load a form and the user selects an item from a dropdown list i want to set focus to another control. In standard vb and standard vb.net i can do this. Anyone know how to do this asp.net?... any help would be greatly appreciated

Thank youYou need to add some clientside JavaScript code in the form of an onchange event handler.
OTTOMH, something like:

DDL.attributes.add("onchange","document.getElementById('controlId').focus()")

Set Focus

Does anyone have any idea how to set focus on any control inside of a user
control?
Any help appreciated.
LeonLeon wrote:
> Does anyone have any idea how to set focus on any control inside of a user
> control?
Leon, you can use Andy Smith's FirstFocus control
[http://metabuilders.com/Tools/FirstFocus.aspx], or read up on how to do
it yourself, through server-side code, in my article:
Working with Client-Side Script
http://tinyurl.com/3w474
Essentially you need to get the ID from the control you want to set
focus to using the control's ClientID property, and then a small bit of
client-side JavaScript is needed to put focus on that control.
Happy Programming!
Scott Mitchell
mitchell@.4guysfromrolla.com
http://www.4GuysFromRolla.com
* When you think ASP.NET, think 4GuysFromRolla.com!

Set Focus

Does anyone have any idea how to set focus on any control inside of a user
control?

Any help appreciated.

LeonLeon wrote:
> Does anyone have any idea how to set focus on any control inside of a user
> control?

Leon, you can use Andy Smith's FirstFocus control
[http://metabuilders.com/Tools/FirstFocus.aspx], or read up on how to do
it yourself, through server-side code, in my article:

Working with Client-Side Script
http://tinyurl.com/3w474

Essentially you need to get the ID from the control you want to set
focus to using the control's ClientID property, and then a small bit of
client-side JavaScript is needed to put focus on that control.

Happy Programming!

--

Scott Mitchell
mitchell@.4guysfromrolla.com
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!

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 after postback

How can I set the focus to a control after postback?I have some javascript function to set the focus in onload event,but it looks like the onload event is not trigger after postback.ThanksThis will do what you want:
-- focus and select in a textbox
Dim sb As New System.Text.StringBuilder

sb.Append("<script language=javascript>")
sb.Append("document.forms.myForm.txtMyBox.focus();")
sb.Append("document.forms.myForm.txtMyBox.select();")
sb.Append("</script>")

If Not IsStartupScriptRegistered("MyBoxSelect") Then
RegisterStartupScript("MyBoxSelect", sb.ToString())
End If


Also, you might have a look @.view post 821121

regards

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...