Showing posts with label load. Show all posts
Showing posts with label load. Show all posts

Wednesday, March 21, 2012

populating a datagrid from a parameterized procedure during page load

I have a datagrid control on my page populated from a parameterized stored procedure. The parameter comes from a label control on my page as seen below. I cannot get the grid to populate. Any suggestions?

Dim LocalIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If LocalIP =""Then
LocalIP = Request.ServerVariables("REMOTE_ADDR")
EndIf
Label1.Text = LocalIP

TIA <JP>

Hi paskettj,

first check if Label1.text has been assigned to the correct value(mean, what's the value of " LocalIP" ? you can use debug, or response.write to monitor it);

secondly,check if your sotred procedure has returned the correct data;

and the third, pls check if you have use datagrid correctly. To get your datagrid control populated with corresponding data,First, you need to set the correct DataSource or DataSourceID, then call the Databind() method. It doesn't matter where you get your data from(whether it's from a table,backend database, from parameterized stored procedure...etc).

for more detailed information, i suggest you reading some material by clicking the following links:

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.basedatalist.datasource(VS.71).aspx (the sample within this page should help u use datagrid)

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.aspx (to a overview of datagird class)

hope my suggestion helps :)

Friday, March 16, 2012

Populating Controls - Form Load Dynamically

hi all,
how can I populate one aspx form when page is loading based on page ID? for
example:
loading page A (to search for VB code) would display labels and texboxes,
dropdown lists all related to VB, plus the address bar would show something
like this: www.somethinghere?id=3
and if you change number 3 from the address bar to (for example) 34 or 71
you would get different page with the same formatting like I.e:
www.somethinghere?id=34 would load the page related to C# topics and all
labels and texboxes and dropdown lists related to C# OR
www.somethinghere?id=71 would load the page related to ASP.NET for example
and labels and textboxes would be populated according to ASP.NET and so
on...

my question is, how do they do that? they must assign an ID number for each
topic but then how they populate all controls on that ONE page accordingly?

I am very much interested in this approach and wants to know how its done
and how can I get more info about it. I think its better than duplicating
pages if you were going to use them over and over?

what do you think?

please let me know

thanksid=XX part of the url is available as Request.QueryString.
Request.QueryString["id"] for www.somethinghere?id=71 will return "71".
PageLoad event code can read the id and setup the page controls accordingly.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

"netasp" <netasp@.newsgroups.nospamwrote in message
news:O6fQ523xGHA.3488@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

hi all,
how can I populate one aspx form when page is loading based on page ID?
for example:
loading page A (to search for VB code) would display labels and texboxes,
dropdown lists all related to VB, plus the address bar would show
something like this: www.somethinghere?id=3
>
and if you change number 3 from the address bar to (for example) 34 or 71
you would get different page with the same formatting like I.e:
www.somethinghere?id=34 would load the page related to C# topics and all
labels and texboxes and dropdown lists related to C# OR
www.somethinghere?id=71 would load the page related to ASP.NET for example
and labels and textboxes would be populated according to ASP.NET and so
on...
>
my question is, how do they do that? they must assign an ID number for
each topic but then how they populate all controls on that ONE page
accordingly?
>
I am very much interested in this approach and wants to know how its done
and how can I get more info about it. I think its better than duplicating
pages if you were going to use them over and over?
>
what do you think?
>
please let me know
>
thanks
>
>


thanks Eliyahu for your reply,

does this method considered to be a good or bad practice with visual studio
2005? because I also have seen some other web applications using different
pages for each category page, their address bar would show aspx pages I.e
employee.aspx, manager.aspx, supervisor.aspx instead of loading everything
into one template page.

I just want to know what is the best practice and recommendation for such a
scenario?

thanks again

"Eliyahu Goldin" <REMOVEALLCAPITALSeEgGoldDinN@.mMvVpPsS.orgwrote in
message news:%23mXBFJ5xGHA.4732@.TK2MSFTNGP03.phx.gbl...

Quote:

Originally Posted by

id=XX part of the url is available as Request.QueryString.
Request.QueryString["id"] for www.somethinghere?id=71 will return "71".
PageLoad event code can read the id and setup the page controls
accordingly.
>
--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
>
"netasp" <netasp@.newsgroups.nospamwrote in message
news:O6fQ523xGHA.3488@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

>hi all,
>how can I populate one aspx form when page is loading based on page ID?
>for example:
>loading page A (to search for VB code) would display labels and texboxes,
>dropdown lists all related to VB, plus the address bar would show
>something like this: www.somethinghere?id=3
>>
>and if you change number 3 from the address bar to (for example) 34 or 71
>you would get different page with the same formatting like I.e:
>www.somethinghere?id=34 would load the page related to C# topics and all
>labels and texboxes and dropdown lists related to C# OR
>www.somethinghere?id=71 would load the page related to ASP.NET for
>example and labels and textboxes would be populated according to ASP.NET
>and so on...
>>
>my question is, how do they do that? they must assign an ID number for
>each topic but then how they populate all controls on that ONE page
>accordingly?
>>
>I am very much interested in this approach and wants to know how its done
>and how can I get more info about it. I think its better than duplicating
>pages if you were going to use them over and over?
>>
>what do you think?
>>
>please let me know
>>
>thanks
>>
>>


>
>


Hello Netasp,

In ASP.NET, dynamically creating webcontrols is naturally supported. Here
are some web articles introducing this:

#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/kb/317794/EN-US/
#Adding Controls to a Web Forms Page Programmatically
http://authors.aspalliance.com/aspx...dingcontrolstow
ebformspageprogrammatically.aspx

#How to: Add Controls to an ASP.NET Web Page Programmatically
http://msdn2.microsoft.com/en-us/library/kyt0fzt1.aspx
for your scenario, you want to generate two page UI for different client
user requests(search for VB or c#), I think you need to consider the
following things:

Which one is more important for our application, flexibiilty or
performance?

When we dynamically create the page UI depend on different client requests,
it make the page very flexible and easy to extend when you want to add
additional UI template(such as search for J#...). However, the drawback
here is that dynamically creating control will add performance overhead
which is less efficient than statically compiled and loaded page/controls.
Also, making the page UI dynamically generated will make the line of code
in your page increase significantly.

IMO, for those pages which as definitely different code logic and business
purpose or the UI differ from each other obviously, I would recommend you
create separate page for each one.

If the UI of those pages are similar and easy to be templated, you can
consider creating a page to dynamically generate UI.

Please feel free to let me know if you have any further questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
thank you Steven for your reply,

how about this:
I create the page and add all the necessary controls to it, dropdown list,
an image box, labels and some textboxes (with generic names) -// so there is
not an overhead of creating controls dynamically

then, if the user clicks the ASP.NET link for example (and lets say it has
been assigned ID=1) then I go to my database and load the appropriate
control names and images for the C# page! OR if the user clicks C# link
(which lets also assume it has been assigned to ID=21) then I will load from
another table all the appropriate names for labels, textboxes and images for
that C# page.

what do you think? would that cut half of my overhead expenses? considering
the controls are already been created?

I would love to hear your input on this approach.

thank you very much for the links

"Steven Cheng[MSFT]" <stcheng@.online.microsoft.comwrote in message
news:hp8bsa$xGHA.4220@.TK2MSFTNGXA01.phx.gbl...

Quote:

Originally Posted by

Hello Netasp,
>
In ASP.NET, dynamically creating webcontrols is naturally supported. Here
are some web articles introducing this:
>
#HOW TO: Dynamically Create Controls in ASP.NET by Using Visual C# .NET
http://support.microsoft.com/kb/317794/EN-US/
>
#Adding Controls to a Web Forms Page Programmatically
http://authors.aspalliance.com/aspx...dingcontrolstow
ebformspageprogrammatically.aspx
>
#How to: Add Controls to an ASP.NET Web Page Programmatically
http://msdn2.microsoft.com/en-us/library/kyt0fzt1.aspx
>
for your scenario, you want to generate two page UI for different client
user requests(search for VB or c#), I think you need to consider the
following things:
>
Which one is more important for our application, flexibiilty or
performance?
>
When we dynamically create the page UI depend on different client
requests,
it make the page very flexible and easy to extend when you want to add
additional UI template(such as search for J#...). However, the drawback
here is that dynamically creating control will add performance overhead
which is less efficient than statically compiled and loaded page/controls.
Also, making the page UI dynamically generated will make the line of code
in your page increase significantly.
>
IMO, for those pages which as definitely different code logic and business
purpose or the UI differ from each other obviously, I would recommend you
create separate page for each one.
>
If the UI of those pages are similar and easy to be templated, you can
consider creating a page to dynamically generate UI.
>
Please feel free to let me know if you have any further questions.
>
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
>
This posting is provided "AS IS" with no warranties, and confers no
rights.
>


Thanks for your reply Netasp,

As for the new idea you mentioned, do you mean you predefine all the
controls on the page and assigned fixed ID for each one. and dynamically
load their string resources( such as Text ...) from file or database and
assign to their "Text" property? If this is the case, I think it doable.

Of course, if the controls are defined at design-time, it will save lots of
dynamic control creating and loading overhead.

Please let me know if you have any further questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
thanks Steven for your reply,

yeah, this is very much the idea, and I think its better than creating more
than one aspx page that does exactly the same thing.

it seems doable so far, and I will pick up any issues as I advance.

thanks for your help.

best regard,

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

Quote:

Originally Posted by

Thanks for your reply Netasp,
>
As for the new idea you mentioned, do you mean you predefine all the
controls on the page and assigned fixed ID for each one. and dynamically
load their string resources( such as Text ...) from file or database and
assign to their "Text" property? If this is the case, I think it doable.
>
Of course, if the controls are defined at design-time, it will save lots
of
dynamic control creating and loading overhead.
>
Please let me know if you have any further questions.
>
Sincerely,
>
Steven Cheng
>
Microsoft MSDN Online Support Lead
>
>
This posting is provided "AS IS" with no warranties, and confers no
rights.
>
>


Thanks for the followup Netasp,

Yes, this is a good idea. Actually, what you do is just how ASP.NET
application do localization support for web pages. ASP.NET support creating
resource files for each page or the whole application and we can load
string or other resource(like image, files) from resource file according
different CultureInfo. So for your case here, instead of depending on
CultureInfo, your page's resource are depend on other criteria.

Please feel free to post here if you meet any further problem.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.
now I have one final question,
what is the best reliable way of loading a control value from a database?
lets say I have label1 already sitting in my webform how can I load its
content "Books" from a table1 in sql 2005 database?

do I use a normal adapter to loop through? is there a good examples
illustrating how to accomplish that?

thanks
"netasp" <netasp@.newsgroups.nospamwrote in message
news:%235MSXCtyGHA.284@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

thanks Steven for your reply,
>
yeah, this is very much the idea, and I think its better than creating
more than one aspx page that does exactly the same thing.
>
it seems doable so far, and I will pick up any issues as I advance.
>
thanks for your help.
>
best regard,
>
"Steven Cheng[MSFT]" <stcheng@.online.microsoft.comwrote in message
news:9Q8DZ2nyGHA.4616@.TK2MSFTNGXA01.phx.gbl...

Quote:

Originally Posted by

>Thanks for your reply Netasp,
>>
>As for the new idea you mentioned, do you mean you predefine all the
>controls on the page and assigned fixed ID for each one. and dynamically
>load their string resources( such as Text ...) from file or database
>and
>assign to their "Text" property? If this is the case, I think it
>doable.
>>
>Of course, if the controls are defined at design-time, it will save lots
>of
>dynamic control creating and loading overhead.
>>
>Please let me know if you have any further questions.
>>
>Sincerely,
>>
>Steven Cheng
>>
>Microsoft MSDN Online Support Lead
>>
>>
>This posting is provided "AS IS" with no warranties, and confers no
>rights.
>>
>>


>
>


Hi Netasp,

how you'll read the static data from database depend on how will you use
them. If you will only load them at each page's startup time and will
requrey database if require them later, I suggest you use Sqlcommand +
DataReader to query the data since this will have better performance.

If you will load the data and also cached them in local memory for later
use, you can consider use TableAdpater and return them in the form of
DataTable, Dataset ...

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

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

Populating DIV tag from javascript via ASP.Net & ClientScript methods

I'm trying to run some clientside javascript on page load for one of my
ASP.Net pages. I am using the ClientScript.RegisterClient...() methods
to register an external javascript file within ASP.Net. When I put my
code on a ASP button and click it it works just find, runs my
javascript which popus up an alert and then changes the div text.
However, when I put this on a Page_Load event the alert comes up ok but
the changing of the div text does not occur. I have a feeling this has
something to do with not all the page elements being rendered or
avaiable in some way to the javascript. I've tried placing the code in
LoadComplete, and Init, with the same results. Any suggestions on how I
can get my javascript code to be triggered on page load from ASP and be
able to access page elements like div tags? I don't have my code in
front of me now but could come up with example if needed. Any advice is
appreciated.

David Mathew
mathewda@dotnet.itags.org.gmail.comusing masterpages? if so, it changes the client ID

Dave Mathew wrote:

Quote:

Originally Posted by

I'm trying to run some clientside javascript on page load for one of my
ASP.Net pages. I am using the ClientScript.RegisterClient...() methods
to register an external javascript file within ASP.Net. When I put my
code on a ASP button and click it it works just find, runs my
javascript which popus up an alert and then changes the div text.
However, when I put this on a Page_Load event the alert comes up ok but
the changing of the div text does not occur. I have a feeling this has
something to do with not all the page elements being rendered or
avaiable in some way to the javascript. I've tried placing the code in
LoadComplete, and Init, with the same results. Any suggestions on how I
can get my javascript code to be triggered on page load from ASP and be
able to access page elements like div tags? I don't have my code in
front of me now but could come up with example if needed. Any advice is
appreciated.
>
David Mathew
mathewda@.gmail.com


I think what you are running into is the javascript code being executed
before the elements of your page have been created in the page DOM.
Try to stick your javascript into the onload attribute of your body tag
and see if it will work.

If this does work, I think what you will have to do is register your
javascript with the onload event either by adding it as an attribute of
the body tag or via external script.

http://www.quirksmode.org/js/events_advanced.html
The above article is similar to what you could use to register your
javascript with the load event except of course the event you want to
register for is load (onload in IE) and I believe you want to use
window as your element.

I wrote a javascript register function that handles cross browser
registration a while ago but don't have it with me. Let me know if you
want it and I'll post it. I believe there are also similar examples
out there somewhere on the web.

Clint.

Dave Mathew wrote:

Quote:

Originally Posted by

I'm trying to run some clientside javascript on page load for one of my
ASP.Net pages. I am using the ClientScript.RegisterClient...() methods
to register an external javascript file within ASP.Net. When I put my
code on a ASP button and click it it works just find, runs my
javascript which popus up an alert and then changes the div text.
However, when I put this on a Page_Load event the alert comes up ok but
the changing of the div text does not occur. I have a feeling this has
something to do with not all the page elements being rendered or
avaiable in some way to the javascript. I've tried placing the code in
LoadComplete, and Init, with the same results. Any suggestions on how I
can get my javascript code to be triggered on page load from ASP and be
able to access page elements like div tags? I don't have my code in
front of me now but could come up with example if needed. Any advice is
appreciated.
>
David Mathew
mathewda@.gmail.com

populating dropdown in datagrid

I am populating a drop down column in a datagrid on page load. Here is
my code :
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList ID="ddlUserName" Font-Name="Verdana"
Font-Size="8pt" Runat=server
DataValueField="UserName"
DataTextField="UserName"
DataSource='<%# GetUserList() %>'>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
public DataSet GetUserList()
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strAtradius"]);
string strUserList = "select username from userlogin where jobrolekey
= 2 ";
strUserList += "order by username";
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strUserList,
objConnection);
objDataAdapter.Fill(dsUsers, "Users");
return dsUsers;
}
dsUsers is global to the page. The problem I have is that the first row
of the datagrid populates correctly, but the following lines append the
contents of the dropdown in the row above. How do I clear the contents
of the dropdown before populating each row?
Any help would be really appreciated.
Cheers,
Mike
*** Sent via Developersdex http://www.examnotes.net ***The code that you posted shows that you return an object named dsUsers but i
t
does not show when the object was instantiated. If the scope of that
variable "dsUsers" is outside the scope of the function GetUsersList then
your table would keep appending records for each row of the datagrid
processed. You should move the steps of populating the table to the
Page_Load if they are not dependent on any value in the datagrid item
otherwise you code has to change to return only a DataTable that is local in
scope to the "GetUsersList" function.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Mike P" wrote:

> I am populating a drop down column in a datagrid on page load. Here is
> my code :
> <asp:TemplateColumn>
> <ItemTemplate>
> <asp:DropDownList ID="ddlUserName" Font-Name="Verdana"
> Font-Size="8pt" Runat=server
> DataValueField="UserName"
> DataTextField="UserName"
> DataSource='<%# GetUserList() %>'>
> </asp:DropDownList>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> public DataSet GetUserList()
> {
> SqlConnection objConnection = new
> SqlConnection(ConfigurationSettings.AppSettings["strAtradius"]);
> string strUserList = "select username from userlogin where jobrolekey
> = 2 ";
> strUserList += "order by username";
> SqlDataAdapter objDataAdapter = new SqlDataAdapter(strUserList,
> objConnection);
> objDataAdapter.Fill(dsUsers, "Users");
> return dsUsers;
> }
> dsUsers is global to the page. The problem I have is that the first row
> of the datagrid populates correctly, but the following lines append the
> contents of the dropdown in the row above. How do I clear the contents
> of the dropdown before populating each row?
>
> Any help would be really appreciated.
>
> Cheers,
> Mike
>
>
> *** Sent via Developersdex http://www.examnotes.net ***
>

Populating Drop Down Lists Based on Another DDL

Sounds like your repopulating the first dropdown in the load event. Try
wrapping the code that populates the first drop down in a
"IsPostBack==False" type check.

"Ben Arthur" <skchbs@dotnet.itags.org.yahoo.com> wrote in message
news:uHDS6CHAEHA.4080@dotnet.itags.org.TK2MSFTNGP09.phx.gbl...
> OK...assume the first ddl has 2 values"USA" & Australia", when i select
> USA, it populates the states correctly in the second ddl, but then if i
> select australia also, as it is autopostback, it comes back to USA as
> selected n so i cannot get the second DDL to populate to values for
> Australia...the datagrid however does bind correctly according to the
> states selected...
> Please let me know if I shoudl explain better....
> Thanks for ur time
> Ben
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!Yes ! Thanks, that worked...but i m not sure of how to handle
postbacks....i.e i load the first ddl only the first time, it loads the
USA and australia, then i select one of them, it loads second ddl
accordingly and builds the datagrid,but if i select australia, it
populates the second ddl but does not bind the grid.....though the
binding is not related to postback but to the selectedindexhanged(i shud
mention there is only value for the second ddl in my DB for first
ddl=australia)...is it related to the postback ...or is it because there
is only one value?
Thanks again for ur help.
Ben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
My usual process for this is...

Put your grid-data-binding in a method/function/sub of its own. Call this
from the page load (wrapped in a isPostBack==False) then in each of your
drop down list events you can call the grid databind method.

BTW, do you really need a grid, they are very "heavy".

"Ben Arthur" <skchbs@.yahoo.com> wrote in message
news:u5jF9ZHAEHA.2800@.tk2msftngp13.phx.gbl...
> Yes ! Thanks, that worked...but i m not sure of how to handle
> postbacks....i.e i load the first ddl only the first time, it loads the
> USA and australia, then i select one of them, it loads second ddl
> accordingly and builds the datagrid,but if i select australia, it
> populates the second ddl but does not bind the grid.....though the
> binding is not related to postback but to the selectedindexhanged(i shud
> mention there is only value for the second ddl in my DB for first
> ddl=australia)...is it related to the postback ...or is it because there
> is only one value?
> Thanks again for ur help.
> Ben
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
thanks a lot...i had to put the binding code in postback part too n now
it works fine...

Thanks again
Ben

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!