472,122 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

getting value of server control

hi !
i have following code to display some text on a web form,after
getting it from database.

<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LinkButton ID="links" runat="server" Text='<%#
Eval("links") %>' OnClick="LinkButton1_Click"></asp:LinkButton><br />

<asp:Label ID="textLabel" runat="server" Text='<%#
Eval("text") %>' ForeColor="DarkSlateBlue"></asp:Label><br />

<asp:Label ID="urlsLabel" runat="server" Text='<%#
Eval("urls") %>' ForeColor="DarkSeaGreen"></asp:Label><br />

<asp:HiddenField ID="page" runat="server" Value='<%#
Eval("page_ref") %>'></asp:HiddenField><br />
<br />
</ItemTemplate>
</asp:DataList>

and i want to access the value of hiddenfield in my aspx.cs file for
some manipulation.for this purpose i'm using the following code:
for(i=0;i<DataList1.items.count;i++)
{
HiddenField hd = (HiddenField)DataList1.Items[2].FindControl("page");
string s = (hd.Value) as string;
}

its giving me the whole record of datalist.but i want to get only the
selected item against my record.
any one have idea about it,plz rply me.

Apr 6 '07 #1
4 4489
Good morning Bushi,

Take a look at your code again. You're looping through the items accessing
third item (Item[2]) ein every iteration. In addition to that, there is no
need for that as datalist has a property to check which item is currently
selected, it should be as following:

int index = DataList1.SelectedIndex;
string value = String.Empty;

if (index >= 0)
{
HiddenField hidden = (HiddenField)
DataList1.Items[index].FindControl("page");
value = hidden.Value;
}

--
Milosz
"bushi" wrote:
hi !
i have following code to display some text on a web form,after
getting it from database.

<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LinkButton ID="links" runat="server" Text='<%#
Eval("links") %>' OnClick="LinkButton1_Click"></asp:LinkButton><br />

<asp:Label ID="textLabel" runat="server" Text='<%#
Eval("text") %>' ForeColor="DarkSlateBlue"></asp:Label><br />

<asp:Label ID="urlsLabel" runat="server" Text='<%#
Eval("urls") %>' ForeColor="DarkSeaGreen"></asp:Label><br />

<asp:HiddenField ID="page" runat="server" Value='<%#
Eval("page_ref") %>'></asp:HiddenField><br />
<br />
</ItemTemplate>
</asp:DataList>

and i want to access the value of hiddenfield in my aspx.cs file for
some manipulation.for this purpose i'm using the following code:
for(i=0;i<DataList1.items.count;i++)
{
HiddenField hd = (HiddenField)DataList1.Items[2].FindControl("page");
string s = (hd.Value) as string;
}

its giving me the whole record of datalist.but i want to get only the
selected item against my record.
any one have idea about it,plz rply me.

Apr 6 '07 #2
On Apr 6, 4:46 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
thanks 4 rplying, there is still a problem,i want to get one selected
value at a time,but the code u sent returns the null index,i'm not
getting how to retrieve the slected value,lets i try to explain what i
want,
the following is my datalist result:

Get More Visitors (its a hyper link)
Need a Boost in Site Visitors? Choose How Many Yo
www.lexiesreviews.com/page2.html
Pay-per-click
Ni hittade hit! Vill ni också bli hittade? Kontak
www.resultatmedia.se
The own search engine
with ranking function & real bot AdSense power po
www.myengines.us
Wealthy Affiliate Scam
Raw Review of Wealthy Affiliate! The Big Nasty- E
www.review-one.net/1/?key=Y2

and i want to get the value like "Pay-per-click" once i click on
it,and using its index i want to get the whole record.but when i click
on it returns null refrence,so guide me what i should do?
Good morning Bushi,

Take a look at your code again. You're looping through the items accessing
third item (Item[2]) ein every iteration. In addition to that, there is no
need for that as datalist has a property to check which item is currently
selected, it should be as following:

int index = DataList1.SelectedIndex;
string value = String.Empty;

if (index >= 0)
{
HiddenField hidden = (HiddenField)
DataList1.Items[index].FindControl("page");
value = hidden.Value;

}

--
Milosz

"bushi" wrote:
hi !
i have following code to display some text on a web form,after
getting it from database.
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LinkButton ID="links" runat="server" Text='<%#
Eval("links") %>' OnClick="LinkButton1_Click"></asp:LinkButton><br />
<asp:Label ID="textLabel" runat="server" Text='<%#
Eval("text") %>' ForeColor="DarkSlateBlue"></asp:Label><br />
<asp:Label ID="urlsLabel" runat="server" Text='<%#
Eval("urls") %>' ForeColor="DarkSeaGreen"></asp:Label><br />
<asp:HiddenField ID="page" runat="server" Value='<%#
Eval("page_ref") %>'></asp:HiddenField><br />
<br />
</ItemTemplate>
</asp:DataList>
and i want to access the value of hiddenfield in my aspx.cs file for
some manipulation.for this purpose i'm using the following code:
for(i=0;i<DataList1.items.count;i++)
{
HiddenField hd = (HiddenField)DataList1.Items[2].FindControl("page");
string s = (hd.Value) as string;
}
its giving me the whole record of datalist.but i want to get only the
selected item against my record.
any one have idea about it,plz rply me.- Hide quoted text -

- Show quoted text -

Apr 9 '07 #3
Howdy,

There is no need to use hidden fields for this purpose. Use DataKeyFields
property to specify which column should be used as row identificator and get
its value via SelectedValue property and SelectedIndexChanged event:

<script runat="server">
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
// i assumed page_ref type is string
DataList dataList = (DataList)sender;
string pagerRef = (string) dataList.SelectedValue;
// use the selected page ref value here...
}
</script>

<asp:DataList ID="DataList1" runat="server" DataKeyField="page_ref"
OnSelectedIndexChanged="DataList1_SelectedIndexCha nged">
<ItemTemplate>
<asp:LinkButton CommandName="Select" ID="links" runat="server" Text='<%#
Eval("links") %>' /><br />
<asp:Label ID="textLabel" runat="server" Text='<%# Eval("text") %>'
ForeColor="DarkSlateBlue" /><br />
<asp:Label ID="urlsLabel" runat="server" Text='<%# Eval("urls") %>'
ForeColor="DarkSeaGreen" /><br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="red" />
</asp:DataList>

Hope this helps
--
Milosz
"bushi" wrote:
On Apr 6, 4:46 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
thanks 4 rplying, there is still a problem,i want to get one selected
value at a time,but the code u sent returns the null index,i'm not
getting how to retrieve the slected value,lets i try to explain what i
want,
the following is my datalist result:

Get More Visitors (its a hyper link)
Need a Boost in Site Visitors? Choose How Many Yo
www.lexiesreviews.com/page2.html
Pay-per-click
Ni hittade hit! Vill ni också bli hittade? Kontak
www.resultatmedia.se
The own search engine
with ranking function & real bot AdSense power po
www.myengines.us
Wealthy Affiliate Scam
Raw Review of Wealthy Affiliate! The Big Nasty- E
www.review-one.net/1/?key=Y2

and i want to get the value like "Pay-per-click" once i click on
it,and using its index i want to get the whole record.but when i click
on it returns null refrence,so guide me what i should do?
Good morning Bushi,

Take a look at your code again. You're looping through the items accessing
third item (Item[2]) ein every iteration. In addition to that, there is no
need for that as datalist has a property to check which item is currently
selected, it should be as following:

int index = DataList1.SelectedIndex;
string value = String.Empty;

if (index >= 0)
{
HiddenField hidden = (HiddenField)
DataList1.Items[index].FindControl("page");
value = hidden.Value;

}

--
Milosz

"bushi" wrote:
hi !
i have following code to display some text on a web form,after
getting it from database.
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LinkButton ID="links" runat="server" Text='<%#
Eval("links") %>' OnClick="LinkButton1_Click"></asp:LinkButton><br />
<asp:Label ID="textLabel" runat="server" Text='<%#
Eval("text") %>' ForeColor="DarkSlateBlue"></asp:Label><br />
<asp:Label ID="urlsLabel" runat="server" Text='<%#
Eval("urls") %>' ForeColor="DarkSeaGreen"></asp:Label><br />
<asp:HiddenField ID="page" runat="server" Value='<%#
Eval("page_ref") %>'></asp:HiddenField><br />
<br />
</ItemTemplate>
</asp:DataList>
and i want to access the value of hiddenfield in my aspx.cs file for
some manipulation.for this purpose i'm using the following code:
for(i=0;i<DataList1.items.count;i++)
{
HiddenField hd = (HiddenField)DataList1.Items[2].FindControl("page");
string s = (hd.Value) as string;
}
its giving me the whole record of datalist.but i want to get only the
selected item against my record.
any one have idea about it,plz rply me.- Hide quoted text -
- Show quoted text -


Apr 9 '07 #4
On Apr 10, 2:26 am, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
Howdy,

There is no need to use hidden fields for this purpose. Use DataKeyFields
property to specify which column should be used as row identificator and get
its value via SelectedValue property and SelectedIndexChanged event:

<script runat="server">
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
// i assumed page_ref type is string
DataList dataList = (DataList)sender;
string pagerRef = (string) dataList.SelectedValue;
// use the selected page ref value here...
}
</script>

<asp:DataList ID="DataList1" runat="server" DataKeyField="page_ref"
OnSelectedIndexChanged="DataList1_SelectedIndexCha nged">
<ItemTemplate>
<asp:LinkButton CommandName="Select" ID="links" runat="server" Text='<%#
Eval("links") %>' /><br />
<asp:Label ID="textLabel" runat="server" Text='<%# Eval("text") %>'
ForeColor="DarkSlateBlue" /><br />
<asp:Label ID="urlsLabel" runat="server" Text='<%# Eval("urls") %>'
ForeColor="DarkSeaGreen" /><br />
<br />
</ItemTemplate>
<SelectedItemStyle BackColor="red" />
</asp:DataList>

Hope this helps
--
Milosz

"bushi" wrote:
On Apr 6, 4:46 pm, Milosz Skalecki [MCAD] <mily...@DONTLIKESPAMwp.pl>
wrote:
thanks 4 rplying, there is still a problem,i want to get one selected
value at a time,but the code u sent returns the null index,i'm not
getting how to retrieve the slected value,lets i try to explain what i
want,
the following is my datalist result:
Get More Visitors (its a hyper link)
Need a Boost in Site Visitors? Choose How Many Yo
www.lexiesreviews.com/page2.html
Pay-per-click
Ni hittade hit! Vill ni också bli hittade? Kontak
www.resultatmedia.se
The own search engine
with ranking function & real bot AdSense power po
www.myengines.us
Wealthy Affiliate Scam
Raw Review of Wealthy Affiliate! The Big Nasty- E
www.review-one.net/1/?key=Y2
and i want to get the value like "Pay-per-click" once i click on
it,and using its index i want to get the whole record.but when i click
on it returns null refrence,so guide me what i should do?
Good morning Bushi,
Take a look at your code again. You're looping through the items accessing
third item (Item[2]) ein every iteration. In addition to that, there is no
need for that as datalist has a property to check which item is currently
selected, it should be as following:
int index = DataList1.SelectedIndex;
string value = String.Empty;
if (index >= 0)
{
HiddenField hidden = (HiddenField)
DataList1.Items[index].FindControl("page");
value = hidden.Value;
}
--
Milosz
"bushi" wrote:
hi !
i have following code to display some text on a web form,after
getting it from database.
<asp:DataList ID="DataList1" runat="server"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:LinkButton ID="links" runat="server" Text='<%#
Eval("links") %>' OnClick="LinkButton1_Click"></asp:LinkButton><br />
<asp:Label ID="textLabel" runat="server" Text='<%#
Eval("text") %>' ForeColor="DarkSlateBlue"></asp:Label><br />
<asp:Label ID="urlsLabel" runat="server" Text='<%#
Eval("urls") %>' ForeColor="DarkSeaGreen"></asp:Label><br />
<asp:HiddenField ID="page" runat="server" Value='<%#
Eval("page_ref") %>'></asp:HiddenField><br />
<br />
</ItemTemplate>
</asp:DataList>
and i want to access the value of hiddenfield in my aspx.cs file for
some manipulation.for this purpose i'm using the following code:
for(i=0;i<DataList1.items.count;i++)
{
HiddenField hd = (HiddenField)DataList1.Items[2].FindControl("page");
string s = (hd.Value) as string;
}
its giving me the whole record of datalist.but i want to get only the
selected item against my record.
any one have idea about it,plz rply me.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
thanx milosz!!!!!!!
i have followed ur instructions,they help me in solving
my problem.

Apr 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by tshad | last post: by
4 posts views Thread by regaliel | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
1 post views Thread by Roffers | last post: by
1 post views Thread by bushi | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.