How to put hidden input tag to the page?

  • Thread starter Thread starter momonga
  • Start date Start date
M

momonga

Hi
I'm really new to ASP.NET and would be very grateful any advice.
I want to search a database and put the each results into the value of
hidden field to refer them from javascript.
but I don't know how to it at all..


I'm thinking about the following approach.
(1) Search the database at Page_Load().
(2) Put the following html to the page somehow.
"<input type="hidden" id="totalrows" value=10>"
"<input type="hidden" id="row0" value="the result 0">"
"<input type="hidden" id="row1" value="the result 1">"
..
..
..
(3) Use the values from javascript as following.
function func()
{
totalRows = document.getElementById("totalrows").value;
for(i = 0 ; i < totalRows ; i++)
{
var str_id = "row" + i;
var x = document.getElementById(str_id);
alert(x.value);
}
}

My question is...
(a) Is my way good in the ASP.NET custom?
(b) How to put some hidden input tags to the page?(step(2))
 
I'm really new to ASP.NET and would be very grateful any advice.
I want to search a database and put the each results into the value of
hidden field to refer them from javascript.
but I don't know how to it at all..

I'm thinking about the following approach.
(1) Search the database at Page_Load().

That's probably the best solution, but you might want to search the database
only when the page first loads, not if/when it loads as a result of a
postback... In which case, simply surround the code which searches the
database with

if (!IsPostback)
{
// do the search here
}
(2) Put the following html to the page somehow.
"<input type="hidden" id="totalrows" value=10>"
"<input type="hidden" id="row0" value="the result 0">"
"<input type="hidden" id="row1" value="the result 1">"

How many rows might there possibly be...?

Also, ASP.NET has its own webcontrol version of the above:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx
(3) Use the values from javascript as following.
function func()
{
totalRows = document.getElementById("totalrows").value;
for(i = 0 ; i < totalRows ; i++)
{
var str_id = "row" + i;
var x = document.getElementById(str_id);
alert(x.value);
}
}

Hmm - do you *really* want to fire a load of alerts when the page loads?
That would really irritate me!
(a) Is my way good in the ASP.NET custom?

Difficult to tell without knowing what you're actually trying to achieve
with this...?
 
Hi!
Thanks for the advice.
That's probably the best solution, but you might want to search the database
only when the page first loads, not if/when it loads as a result of a
postback... In which case, simply surround the code which searches the
database with

if (!IsPostback)
{
    // do the search here

}
I think I need this.Thanks!
How many rows might there possibly be...?
In the most case, it will be less than about 10 or so.
It will be less than about 100 or so at maximum.
Hmm - do you *really* want to fire a load of alerts when the page loads?
That would really irritate me!
No.I don't want to call alert actually.
To tell the truth, In my page, there will be an ActiveX control and I
want to call the method with x.value.

Thansk!
 
In the most case, it will be less than about 10 or so.
It will be less than about 100 or so at maximum.
OK.

No.I don't want to call alert actually.
To tell the truth, In my page, there will be an ActiveX control and I
want to call the method with x.value.

What does the ActiveX control do...?
 
What does the ActiveX control do...?
The ActiveX control is streaming player and requests certain server
some contents.
I need to pass him the content identifier.

Now, I find "Page.ClientScript.RegisterHiddenField()" function.
And I worked it out.

Thanks a lot!
 
Howdy,

No need for using hidden fields, you can render the values directly to your
script:

<script type="text/javascript">

function func()
{
var values = new Array(<%= DelimitedValues %>);

for (var i = 0 ; i < values.length; i++)
{
alert(values);
}
}

</script>

-- code behind --

protected string DelimitedValues = "1, 2, 10, 'stringValue', 'whatever'";

// or even better populated once and stored in the viewstate

protected vpoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// retreive values from database
DelimitedValues = "'1','2','aaa'";
}
}

protected string DelimitedValues
{
get
{
return (string) ViewState["DelimitedValues"];
}
set
{
ViewState["DelimitedValues"] = value;
}
}


Hope this helps
 
The ActiveX control is streaming player and requests certain server
some contents.
I need to pass him the content identifier.

Now, I find "Page.ClientScript.RegisterHiddenField()" function.
And I worked it out.

Ah... As has been mentioned, no need at all for hidden fields here...
 
Back
Top