Monday, March 26, 2012
Populate a label with text from a drop down list box
I'm trying to grab the text that appears in a drop down list server control
and place it in a label server control on the same page. I can get the
selected value, that's easy. But I want the text.
Thanksddl.SelectedItem.Text
Eliyahu
"Mardy" <Mardy@.discussions.microsoft.com> wrote in message
news:983E0BF1-2741-4B22-920F-6F7708CA712B@.microsoft.com...
> I know this is probably real easy but I can't figure it out.
> I'm trying to grab the text that appears in a drop down list server
control
> and place it in a label server control on the same page. I can get the
> selected value, that's easy. But I want the text.
> Thanks
>
Populate a label with text from a drop down list box
I'm trying to grab the text that appears in a drop down list server control
and place it in a label server control on the same page. I can get the
selected value, that's easy. But I want the text.
Thanksddl.SelectedItem.Text
Eliyahu
"Mardy" <Mardy@.discussions.microsoft.com> wrote in message
news:983E0BF1-2741-4B22-920F-6F7708CA712B@.microsoft.com...
> I know this is probably real easy but I can't figure it out.
> I'm trying to grab the text that appears in a drop down list server
control
> and place it in a label server control on the same page. I can get the
> selected value, that's easy. But I want the text.
> Thanks
Populate asp:label with text from db and retain formatting?
Im can't seem to figure this out OR find any documentation on if it's possib
le; I need to populate the value of an asp:label with text retrieved from th
e database. However when the text is populated I need to retain carriage ret
urns and indents that the t
ext contains. That way if I want to see this:
Some text with a manual
carriage return.
1. this is one.
2. this is two.
It doesn't look like this:
Some text with a manual carriage return. 1. this is one. 2. this is two.
I've tried enclosing the text in <pre></pre> tags before the text is assigne
d to the label and it works fine except for 2 things. The text is rendered i
n a non-styled font and in the instance that the user entered really long te
xt and didn't manullay hit
a carriage return- which causes the text continue to the right instead of au
tomatically wrapping even though I define a width on my label, the cell the
label is within, etc..
Does anyone know how to get this to work? I must have looked around on the n
et for hours!set the text box Text Mode property to MultiLine.
Av.
"Tony" <anonymous@.discussions.microsoft.com> wrote in message
news:7B9C3796-02D8-4180-AD93-8572746CD951@.microsoft.com...
> Hello,
> Im can't seem to figure this out OR find any documentation on if it's
> possible; I need to populate the value of an asp:label with text retrieved
> from the database. However when the text is populated I need to retain
> carriage returns and indents that the text contains. That way if I want to
> see this:
> Some text with a manual
> carriage return.
> 1. this is one.
> 2. this is two.
> It doesn't look like this:
> Some text with a manual carriage return. 1. this is one. 2. this is two.
> I've tried enclosing the text in <pre></pre> tags before the text is
> assigned to the label and it works fine except for 2 things. The text is
> rendered in a non-styled font and in the instance that the user entered
> really long text and didn't manullay hit a carriage return- which causes
> the text continue to the right instead of automatically wrapping even
> though I define a width on my label, the cell the label is within, etc..
> Does anyone know how to get this to work? I must have looked around on the
> net for hours!
>
TextMode is not a property for a label but a textbox. I have tried using a t
extbox control with this property set to multiline previously however withou
t specifically setting the height of the textbox I end up with vertical scro
llbars- which I cannot have
because this is for a printable report.
Hi, Tony
You might write a function to conver the strin, like:
'Get str from database
str = "Some text with a manual carriage return." & vbCr & vbTab & "1. this i
s one. " & vbNewLine & vbTab & "2. this is two."
str = Replace(str, " ", " ")
str = Replace(str, vbTab, " ") 'De
pend how many spaces do you want for a tab.
str = Replace(str, vbNewLine, "<br/>")
str = Replace(str, vbCr, "<br/>")
str = Replace(str, vbLf, "<br/>")
Label1.Text = str
Hope this helps,
Bin Song, MCP
even better ..use a StringBuilder :)
Swanand Mokashi
Microsoft Certified Solution Developer (.NET)
Microsoft Certified Application Developer (.NET)
http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"Bin Song, MCP" <anonymous@.discussions.microsoft.com> wrote in message
news:E9930659-B6E5-47F0-A42E-88BE5B97B7A9@.microsoft.com...
> Hi, Tony
> You might write a function to conver the strin, like:
> 'Get str from database
> str = "Some text with a manual carriage return." & vbCr & vbTab &
"1. this is one. " & vbNewLine & vbTab & "2. this is two."
> str = Replace(str, " ", " ")
> str = Replace(str, vbTab, " ") '
Depend
how many spaces do you want for a tab.
> str = Replace(str, vbNewLine, "<br/>")
> str = Replace(str, vbCr, "<br/>")
> str = Replace(str, vbLf, "<br/>")
> Label1.Text = str
> Hope this helps,
> Bin Song, MCP
>
The problem is that there is no universally accepted definition of how
preformatted ascii text should be rendered on the web. You just have
to munge it yourself. In the case you gave I would suggest something
like:
outputLabel.Text = dbText.Replace(@."\n", "<BR>").Replace(@."\t",
@." ");
There is no "easy way out" that I'm aware of.
Hope that helps
Ian
"Tony" <anonymous@.discussions.microsoft.com> wrote in message news:<7B9C3796-02D8-4180-AD93
-8572746CD951@.microsoft.com>...
> Hello,
> Im can't seem to figure this out OR find any documentation on if it's possible; I
need to populate the value of an asp:label with text retrieved from the database. Ho
wever when the text is populated I need to retain carriage returns and indents that
the
text contains. That way if I want to see this:
> Some text with a manual
> carriage return.
> 1. this is one.
> 2. this is two.
> It doesn't look like this:
> Some text with a manual carriage return. 1. this is one. 2. this is two.
> I've tried enclosing the text in <pre></pre> tags before the text is
assigned to the label and it works fine except for 2 things. The text
is rendered in a non-styled font and in the instance that the user
entered really long text and didn't manullay hit a carriage return-
which causes the text continue to the right instead of automatically
wrapping even though I define a width on my label, the cell the label
is within, etc..
> Does anyone know how to get this to work? I must have looked around on the net for
hours!
AWESOME!! Thanks Ian!! All I had to do is lose the '@.' sign (not sure if it'
s because I use C#) and it works. I cannot tell you how freakin ecstatic I a
m that your solution worked. Thank you!
Wednesday, March 21, 2012
Populating a dropdownlist
I am trying to figure out how to populate my dropdown list with data from my table. What happens is I call a method that expects an id by using this method I want to keep looping through the recordset and populate my dropdown list with teh values I require.
Here is teh code I have attempted to write this code appears in my page load
ddlVersions.Items.Clear()Dim _CVersion As Content = ContentManager.GetContentVersionList(Page.Request.Params("PID"))
Dim Version As ContentFor Each Version In _CVersion
ddlVersions.DataTextField = "CoPgVersion"
ddlVersions.DataValueField = "CoPgID"
ddlVersions.DataBind()
Next
Here is my method
Public Shared Function GetContentVersionList(ByVal CoPgID As Integer) As Content
' Get the list of Content form the datastore
Dim _Data As SqlDataReader
Dim _Content As New Content_Data = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings("DataStoreConnection"), CommandType.StoredProcedure, "Content_get", New SqlParameter("@dotnet.itags.org.CoPgID", CoPgID))
While _Data.Read
_Content = PopulateObjectFromSqlDataReader(_Data)
End While_Data.Close()
Return _Content
End Function
My stored procedure is basically the following SQL Statement
Select * from content where CoPgID = @dotnet.itags.org.CoPgID
I am simply getting teh syntax wrong but I am unsure on how to correctly code the dropdownlist so it populates.
While I am here once I get the drop down list to work is there a way I can make the drop down list highlight as if selected the most current record.
Basically the dropdownlist will display all teh version of my content if I am at version 4 I want version for to appear along with the rest of teh versions but for it to be select rather than "Please Select" is this doable?
I look forward to any help one may be able to give
Kind Regards
BradHi I will try to help a bit but take it with reservation as I am just beginner using Matrix which generates code for me, but just to say that when I was populating drop down list following instructions from the matrix book I did not loop through the recordset.
Steps were:
-to create function with sql statement in it and reterning the recordset
-to assign this function to data source of drop down list so:
dropdownlist.DataSource=above function
dropdownlist.DataBind()
these 2 lines are putting your recordset in the list already but they are always within some event
for example page load event
i hope this helps
anqa
Anga is quite correct. This is the way to bind data to controls in ASP.NET.
Seethis example on LearnASP.com for more details.
Friday, March 16, 2012
Populating a Textbox
How can I populate a textbox with a value retreived from a SQL database so that it is editable?
Thank you!There's no true data binding in ASP; you've got to write code to retrieve the value and more code to update it. The following code will retrieve the value. You'll also need an Update query to update the value.
Good luck
<code>
Dim oConnAsNew Data.SqlClient.SqlConnection(sConnect)
Dim oCmdAsNew Data.SqlClient.SqlCommand
Dim oDSAsNew DataSet
Dim oDAAsNew SqlClient.SqlDataAdapter
Try
With oCmd
.CommandText = "Select Value From Table1"
.Connection = oConn
EndWith
With oDA
.SelectCommand = oCmd
oConn.Open()
.Fill(oDS)Textbox1.text =ods.Tables(0).Rows(0).Item(0)
EndWith
</code>
What kind of object are you storing your value in? Is it just a single value you are getting back from the database? If so you could use the ExecuteScalar routine to put it into a string and then assign it to a text box. If you have it in a datatable, you just have to assign the proper cell to the text box:
textBox.Text = myDataTable.Rows(rowCounter).Cells("'myField")
As Mr Jkcnack said that if you are retrieving single value use ExecuteScaler method if you are retieving multiple values use dataReader.