Showing posts with label object. Show all posts
Showing posts with label object. 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.


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

Thursday, March 22, 2012

Set Focus using: Clientscript.RegisterStartupscript

Hello,

I am trying to set the focus on an object using the following: However, I
am getting the following error:

The name Clientscript does not exist in the current context.

protected void SetFocusControl(Control FocusControl)
{
StringBuilder script = new StringBuilder();
string ClientID = FocusControl.ClientID;
script.Append("<script language='javascript'>");
script.Append("document.getElementById('");
script.Append(ClientID);
script.Append("').focus();");
script.Append("</script>");
Clientscript.RegisterStartupscript("SetFocusControl", script);
}try it with the correct case

ClientScript.RegisterStartupscript("SetFocusControl", script);

--
Regards

John Timney (MVP)

"sck10" <sck10@.online.nospamwrote in message
news:O23DxRPsGHA.4380@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

Hello,
>
I am trying to set the focus on an object using the following: However, I
am getting the following error:
>
The name Clientscript does not exist in the current context.
>
protected void SetFocusControl(Control FocusControl)
{
StringBuilder script = new StringBuilder();
string ClientID = FocusControl.ClientID;
script.Append("<script language='javascript'>");
script.Append("document.getElementById('");
script.Append(ClientID);
script.Append("').focus();");
script.Append("</script>");
Clientscript.RegisterStartupscript("SetFocusControl", script);
}
>


Thanks John...

"John Timney (MVP)" <x_john@.timney.eclipse.co.ukwrote in message
news:5KednZH2LoThRFrZRVnyiA@.eclipse.net.uk...

Quote:

Originally Posted by

try it with the correct case
>
ClientScript.RegisterStartupscript("SetFocusControl", script);
>
--
Regards
>
John Timney (MVP)
>
>
"sck10" <sck10@.online.nospamwrote in message
news:O23DxRPsGHA.4380@.TK2MSFTNGP05.phx.gbl...

Quote:

Originally Posted by

>Hello,
>>
>I am trying to set the focus on an object using the following: However,
>I am getting the following error:
>>
>The name Clientscript does not exist in the current context.
>>
>protected void SetFocusControl(Control FocusControl)
>{
> StringBuilder script = new StringBuilder();
> string ClientID = FocusControl.ClientID;
> script.Append("<script language='javascript'>");
> script.Append("document.getElementById('");
> script.Append(ClientID);
> script.Append("').focus();");
> script.Append("</script>");
> Clientscript.RegisterStartupscript("SetFocusControl", script);
>}
>>


>
>


On Wed, 26 Jul 2006 16:39:00 -0500, "sck10" <sck10@.online.nospam>
wrote:

Quote:

Originally Posted by

>Thanks John...
>
>


With 2.0, controls have a Focus method that seems to do the same
thing. Actually a bit better because it takes care of ClientId if
using a master page.

Set Header Title from within a normal object

Is it possible to set the title for a page within a normal object; e.g.

1using System;
2using System.Web;
3using System.Web.UI;
4using System.Collections;
5using System.Web.UI.WebControls;
6using System.Web.UI.HtmlControls;
78namespace Utils
9{
10// Program start class11public class Misc
12 {
13public static void setSubject(Page page, String subject) {
14 page.Header.Title=subject;
15 }
16}

Compiling it gives me following error:

Utils.cs(16,4): error CS0117: `System.Web.UI.Page' does not contain a definition for `Header' Compilation failed: 1 error(s), 0 warnings

Thanks in advance,

jerous.

isn't it just Page.Title?

You'll need to ensure that the calling page has the runat="server" in it's <title..> tag.


Use page.Title property. It would set your page Title.

Set HTML Attribute In Page_Load Event

I want to be able to set an attribute of the HTML object in the form's
Page_Load event. I can't see how to do this. The main problem is that I
can't see how to access the HTML object from this event. Can anyone help ?

BenBen,

Forthe element you need to specify runat=server and id=myHtml. Then VS will
define the element for you in code-behind as the page's protected member. In
any page method you can set an attribute as (c# syntax):

myHtml.Attributes["myAttribute"]="my string";

Eliyahu

"Ben Foster" <ben.foster@.nospam.com> wrote in message
news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
> I want to be able to set an attribute of the HTML object in the form's
> Page_Load event. I can't see how to do this. The main problem is that I
> can't see how to access the HTML object from this event. Can anyone help ?
> Ben
Eliyahu,

That works fine. Thanks.

Ben

"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:%23TIxNYBGFHA.3244@.TK2MSFTNGP15.phx.gbl...
> Ben,
> Forthe element you need to specify runat=server and id=myHtml. Then VS
> will
> define the element for you in code-behind as the page's protected member.
> In
> any page method you can set an attribute as (c# syntax):
> myHtml.Attributes["myAttribute"]="my string";
> Eliyahu
> "Ben Foster" <ben.foster@.nospam.com> wrote in message
> news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
>> I want to be able to set an attribute of the HTML object in the form's
>> Page_Load event. I can't see how to do this. The main problem is that I
>> can't see how to access the HTML object from this event. Can anyone help
>> ?
>>
>> Ben
>>
>>

Set HTML Attribute In Page_Load Event

I want to be able to set an attribute of the HTML object in the form's
Page_Load event. I can't see how to do this. The main problem is that I
can't see how to access the HTML object from this event. Can anyone help ?
BenBen,
Forthe element you need to specify runat=server and id=myHtml. Then VS will
define the element for you in code-behind as the page's protected member. In
any page method you can set an attribute as (c# syntax):
myHtml.Attributes["myAttribute"]="my string";
Eliyahu
"Ben Foster" <ben.foster@.nospam.com> wrote in message
news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
> I want to be able to set an attribute of the HTML object in the form's
> Page_Load event. I can't see how to do this. The main problem is that I
> can't see how to access the HTML object from this event. Can anyone help ?
> Ben
>
Eliyahu,
That works fine. Thanks.
Ben
"Eliyahu Goldin" <removemeegoldin@.monarchmed.com> wrote in message
news:%23TIxNYBGFHA.3244@.TK2MSFTNGP15.phx.gbl...
> Ben,
> Forthe element you need to specify runat=server and id=myHtml. Then VS
> will
> define the element for you in code-behind as the page's protected member.
> In
> any page method you can set an attribute as (c# syntax):
> myHtml.Attributes["myAttribute"]="my string";
> Eliyahu
> "Ben Foster" <ben.foster@.nospam.com> wrote in message
> news:OozPcRBGFHA.2180@.TK2MSFTNGP10.phx.gbl...
>

Set mode in FormView on Page_Load

I'm trying to set the FormView in insert mode but nothing happens with
this code:
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
FormView1.ChangeMode(FormViewMode.Insert)
End Sub
If I click the New button on the form everything works but not with
code.
Can someone please help me?
Regards,
Stry
Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) handles Me.Load
Try this inside your Page_Load:
If (Not IsPostBack) Then
FormView1.DefaultMode = FormViewMode.Insert
End If
Then again, I'm not sure if you're showing enough code.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."
<staeri@.gmail.com> wrote in message
news:1144144823.902803.185090@.i39g2000cwa.googlegroups.com...
> I'm trying to set the FormView in insert mode but nothing happens with
> this code:
> Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> FormView1.ChangeMode(FormViewMode.Insert)
> End Sub
> If I click the New button on the form everything works but not with
> code.
> Can someone please help me?
> Regards,
> S
>

Set mode in FormView on Page_Load

I'm trying to set the FormView in insert mode but nothing happens with
this code:

Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
FormView1.ChangeMode(FormViewMode.Insert)
End Sub

If I click the New button on the form everything works but not with
code.

Can someone please help me?

Regards,

Stry

Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) handles Me.Load
Try this inside your Page_Load:

If (Not IsPostBack) Then
FormView1.DefaultMode = FormViewMode.Insert
End If

Then again, I'm not sure if you're showing enough code.
--
Christopher A. Reed
"The oxen are slow, but the earth is patient."

<staeri@.gmail.com> wrote in message
news:1144144823.902803.185090@.i39g2000cwa.googlegr oups.com...
> I'm trying to set the FormView in insert mode but nothing happens with
> this code:
> Sub Page_Load(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> FormView1.ChangeMode(FormViewMode.Insert)
> End Sub
> If I click the New button on the form everything works but not with
> code.
> Can someone please help me?
> Regards,
> S

Tuesday, March 13, 2012

Set object properties via a loop?

I have a data container object that holds user info, with one public property mapping to each field in my database table.

When retrieving a user's info from my db I'm using a DataReader, and writing code like this:

myDBObj.name = reader["name"];
myDBObj.id = reader["id"];

Etc, etc. This works ok, but I was hoping to use the DataReader's GetName and FieldCount methods to automate setting my property values.

Something like:

for (int i = 0; i <= reader.FieldCount; i++)
{
string fieldName = reader.GetName(i);
usr.[fieldName] = (reader.GetDataTypeName(i))reader[i];
}

The part in bold is throwing me--is there a way to use a variable in determining which property I'm assigning? Is there another way to do what I'm trying?

Thanks!

Aaron

You could try reader[i].ToString()

usr.[fieldName] = reader[i].ToString();

Sample Code:

SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand commond = new SqlCommand("SELECT * FROM TEST");
DataSet fillDataSet = new DataSet();
commond.Connection = connection;
commond.CommandType = CommandType.Text;
connection.Open();
SqlDataReader dr;
dr = commond.ExecuteReader();
while(dr.Read())
{
for(int i=0; i<dr.FieldCount;i++)
{
Response.Write(dr.GetName(i) + ":"+ i.ToString() +":" + dr[i].ToString());
Response.Write("<br>");
}
}
connection.Close();


Obtaining the info via the datareader isn't the problem, the issue is that the object I'm creating (objUserDetail) has properties of varying types (strings, ints, datetimes, etc).

While I could certainly simply write out a long list of property assignments and cast the datareader's info in the correct type each time, I'd much rather figure out a way to do it via a loop.

I figured out how to use a PropertyInfo object to get the names of the properties dynamically at runtime, but now the problem is the datareader is sending all the data back as a string, and I can't find a way to convert/cast it into the format the various properties are expecting.

Normally I would do this:

objUserDetail.UserID = (int)reader["UserID"];

But I need a way to convert the reader's field value dynamically.

Anyone know how?


Can you try this..

(dr[i].GetType()) dr[i].ToString()

while(dr.Read())
{
for(int i=0; i<dr.FieldCount;i++)
{
Response.Write(dr.GetName(i) + ":"+ dr[i].GetType() +":" + dr[i].ToString());
Response.Write("<br>");
}
}

Why don't you use TypeDataSet?


like this:

Dim myDBobjAs New usrDim fieldNameAs String Dim flagsAs BindingFlags = BindingFlags.InstanceOr BindingFlags.Public Or BindingFlags.IgnoreCaseOr BindingFlags.Static Or BindingFlags.FlattenHierarchyDim pAs PropertyInfoDim readerAs System.Data.SqlClient.SqlDataReader = GetTheReader()'populate one entityFor iAs Integer = 0To (reader.FieldCount - 1) fieldname = reader.GetName(i) p =GetType(usr).GetProperty(fieldName, flags) p.SetValue(myDBobj, reader(i),Nothing)Next

Thanks for your help, everyone!

I was able to get it working, and now I'm reconsidering the whole data table class concept, since that's what DataSets are for, right?

aaron


DataSets are basically just collections of DataTables.

NC...