getting value of server control

B

bushi

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

Guest

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

bushi

On Apr 6, 4:46 pm, Milosz Skalecki [MCAD] <[email protected]>
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 said:
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 -
 
G

Guest

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_SelectedIndexChanged">
<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 said:
On Apr 6, 4:46 pm, Milosz Skalecki [MCAD] <[email protected]>
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 said:
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 -
 
B

bushi

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_SelectedIndexChanged">
<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 said:
On Apr 6, 4:46 pm, Milosz Skalecki [MCAD] <[email protected]>
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
:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top