Thursday, March 29, 2012
Set colour of the text in DropDownList?
<option style="background-color:black;">blah</option>
Of course in your codebehind, since you'll be using ListItems, do this to the listitem.
ListItem.Attributes.Add("style","background-color:black;");
Simple CSS. You'd use... for example
<option style="background-color:black;">blah</option>
Of course in your codebehind, since you'll be using ListItems, do this to the listitem.
ListItem.Attributes.Add("style","background-color:black;");
Thanks for the solution!!!
Add Resolved, dude.
Monday, March 26, 2012
Set dropdownlist from static data
<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList
The values and text are static. I am reading from a table:
Select QuestionType from myTable where id = 10
and QuestionType will either be "MS", "MM" or "TF".
How do I bind the data to this object?
With a Label it would be something like:
<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" /
Thanks,
Tom.Databinding from a SQL table is easy, you should be able to find many
examples out there.
You need to know more about this database you are querying though.
1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104870094.287340.59580@.z14g2000cwz.googlegro ups.com...
> Databinding from a SQL table is easy, you should be able to find many
> examples out there.
> You need to know more about this database you are querying though.
> 1.) Is it a MS SQL server or is it another type?
> 2.) Do you have to login to the server, or does it accept inherited
> credentials?
> 3.) Do you have the database name you are connecting to?
Actuall, no.
How I get the data is not the issue here.
I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.
<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %
I want to do the same type of thing here where my selected item will display
based on the data returned.
Somehow, I need to do something like:
questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %
So that the value will show correctly in the dropdownbox.
Thanks,
Tom
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.
It may require you to go as far as using a custom control for what you
are trying to do.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104963175.704187.310580@.c13g2000cwb.googlegr oups.com...
> Oh so you want the dropdownlist to change the currently selected item
> to the one that's in the table? I have tried doing that in the past but
> haven't had any luck, I'm only intermediate in skill with asp.net so
> hopefully someone else can provide a solution for you.
> It may require you to go as far as using a custom control for what you
> are trying to do.
I hope not.
I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.
Thanks,
Tom.
Yeah, I completely agree with you and what you say makes sense.
Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If
So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group...557b5a7b406fa52
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.
Anyway, I put it in the sub Page_PreRender and it worked just fine.
Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub
<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIndexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList
Hope this helps!
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.
Ignore the above post, I made an error in my code because I was writing
it too fast.
Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1105051197.174515.91240@.f14g2000cwb.googlegro ups.com...
> Ignore the above post, I made an error in my code because I was writing
> it too fast.
> Anyway, this solution works fine, and it's all in sub code which I
> think is better... that way you don't have to scroll between variables
> in your html and your subs, it's all in one place. Again, I hope this
> helps.
I'll look at it and see how it works and let you know.
Thanks for the help,
Tom
set dropdownlist selectedindex in datagrid
I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.
I have a templatecolumn as follows:
<asp:TemplateColumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList id="DropDownList3" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
with an edit column as:
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit" CancelText="Cancel" EditText="Edit"></
asp:EditCommandColumn>
I would like to set the dropdownlist to the value found in the
database. my code behind uses ItemDataBound as follows:
public void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dd;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
}
}
upon loading the dropdownlist has the values as in the database.
However, when I press edit the selected index becomes 0. I cannot
figure how to set the value correctly.
my edit code looks like:
public void myDataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dd;
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
<-- dd.SelectedIndex looks fine here -->
...
}
dd.SelectedIndex looks ok in debug mode, but something changes it
before the dropdownlist is displayed. the values in the dropdownlists
not being edited are ok, it's only the one being edited that shows the
wrong selectedindex.
any help would be appreciated. thanks.pleaseexplaintome@.yahoo.com wrote:
Quote:
Originally Posted by
Hi all,
>
I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.
>
I have a templatecolumn as follows:
>
<asp:TemplateColumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList id="DropDownList3" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
There is a much easier way (at least in ASP.NET 2.0)
Set the property SelectedValue on the DrowDownlist
declaratively like this:
SelectedValue='<%# Bind("myDBField") %>'
This will take care of everything: showing the data and
updating the data after postback.
--
Riki
Set DropDownList Items Alignment
Hi
How can I set my dropdownlist Itmes alignement right rether than left?
I bound them from a database??
my code
<asp:DropDownList width="100%" Runat="Server" ID="ddlCollege" DataTextField="CollegeName" Style="text-align: center;" DataValueField="CollegeID" Autopostback="True" OnSelectedIndexChanged="ShowDepartmentsEvents"/>
on the behind code I select values from the database??
Can Someone help??
thanks in advance
Setting style="text-align:right" on the dropdown list will work in Firefox, but I don't think there is any way to do this on Internet Explorer 6.
regards
I'm afraid you cannot do that unless you create you own dropdownlist :(
Hope my suggestion helps.
Set dropdownlist from static data
<asp:DropDownList ID="ddlQuestionType" runat="server">
<asp:ListItem Value="MS" Text="Multiple Single" />
<asp:ListItem Value="MM" Text="Multiple Multiple" />
<asp:ListItem Value="TF" Text="True/False" />
</asp:DropDownList>
The values and text are static. I am reading from a table:
Select QuestionType from myTable where id = 10
and QuestionType will either be "MS", "MM" or "TF".
How do I bind the data to this object?
With a Label it would be something like:
<asp:label id="Question" Text='<%# DataBinder.Eval(Container.DataItem,
"QuestionUnique") %>' runat="server" />
Thanks,
Tom.Databinding from a SQL table is easy, you should be able to find many
examples out there.
You need to know more about this database you are querying though.
1.) Is it a MS SQL server or is it another type?
2.) Do you have to login to the server, or does it accept inherited
credentials?
3.) Do you have the database name you are connecting to?
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104870094.287340.59580@.z14g2000cwz.googlegroups.com...
> Databinding from a SQL table is easy, you should be able to find many
> examples out there.
> You need to know more about this database you are querying though.
> 1.) Is it a MS SQL server or is it another type?
> 2.) Do you have to login to the server, or does it accept inherited
> credentials?
> 3.) Do you have the database name you are connecting to?
>
Actuall, no.
How I get the data is not the issue here.
I can get the data fine. It will always be either MM, MS or TF. I also
have no problem binding to a textbox, label, datagrid etc.
<%# DataBinder.Eval(Container.DataItem, "QuestionUnique") %>
I want to do the same type of thing here where my selected item will display
based on the data returned.
Somehow, I need to do something like:
questionType.SelectedItem.value = <%# DataBinder.Eval(Container.DataItem,
"QuestionType") %>
So that the value will show correctly in the dropdownbox.
Thanks,
Tom
Oh so you want the dropdownlist to change the currently selected item
to the one that's in the table? I have tried doing that in the past but
haven't had any luck, I'm only intermediate in skill with asp.net so
hopefully someone else can provide a solution for you.
It may require you to go as far as using a custom control for what you
are trying to do.
"Chad Devine" <surr3al@.gmail.com> wrote in message
news:1104963175.704187.310580@.c13g2000cwb.googlegroups.com...
> Oh so you want the dropdownlist to change the currently selected item
> to the one that's in the table? I have tried doing that in the past but
> haven't had any luck, I'm only intermediate in skill with asp.net so
> hopefully someone else can provide a solution for you.
> It may require you to go as far as using a custom control for what you
> are trying to do.
>
I hope not.
I assume that other people must do this. The problem with other examples,
is that they assume you want to populate the list with the results from your
table. But if you are looking at data that was already input and want to
allow the user to make a change to that data later, you would set the
dropdown to the data they had already chosen and allow them to choose
something else from the dropdown.
Thanks,
Tom.
Yeah, I completely agree with you and what you say makes sense.
Many suggest that doing a
If not ispostback then
DropDownList.DataBind()
End If
So that the databind doesn't happen unless the person refreshes the
page, that way it keeps whatever the user selected, however in the case
of a table, when the user hits submit it refreshes the whole page so
that method doesn't work in this case. I really wish I could help you
in this, but I can't seem to find a property of the dropdownlist that
allows you to change the current selected item. You only seem to get
that chance right in the beginning when you define what options and
values to go into the list.
Look here, I think this person is doing what you want to do:
http://groups-beta.google.com/group...557b5a7b406fa52
I just figured out your answer... I did it for myself, but with
autopostback it presents a little problem, when calling it in the
Page_Load sub because it won't let you change the current selected
item, as it keeps trying to select the previous one posted for some
reason.
Anyway, I put it in the sub Page_PreRender and it worked just fine.
Here's the code:
Sub Page_PreRender
dropListOptions.SelectedValue = intPageNumber
End Sub
<asp:DropDownList
ID="dropListOptions"
AutoPostBack="True"
OnSelectedIndexChanged="dropListOptions_SelectedIndexChanged"
font-size="10"
Runat="Server" >
<asp:ListItem id="Default" Text="Default" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="75" Value="75" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="150" Value="150" />
<asp:ListItem Text="200" Value="200" />
</asp:DropDownList>
Hope this helps!
Another update, I found that only works for Value properties, so if
your databinding this, this would be a problem... and then we're back
to square one.
Ignore the above post, I made an error in my code because I was writing
it too fast.
Anyway, this solution works fine, and it's all in sub code which I
think is better... that way you don't have to scroll between variables
in your html and your subs, it's all in one place. Again, I hope this
helps.
Set dropdownlist value
to be the same as the value). I changed this to a character as that
is what I want to have in my database.
<asp:dropdownlist
style="border:none" id="recurrenceType"
runat="server">
<asp:listitem value="N"
>None</asp:listitem>
<asp:listitem value="M"
>Monthly</asp:listitem>
<asp:listitem value="P"
>Periodic</asp:listitem>
</asp:dropdownlist>
I was setting the value before (when the value was "0", "1" and "2").
It used to work like this:
recurrenceType.SelectedIndex = objDataReader("recurrenceType")
How would I set it with the value set to a char?
Thanks,
Tom.
Posted Via mcse.ms Premium Usenet Newsgroup Services
----
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----
http://www.mcse.msChange
recurrenceType.SelectedIndex = ...
To
recurrenceType.SelectedValue = ...
"tfs" wrote:
> I have a dropdownlist that I was setting to an index (which happened
> to be the same as the value). I changed this to a character as that
> is what I want to have in my database.
> <asp:dropdownlist
> style="border:none" id="recurrenceType"
> runat="server">
> <asp:listitem value="N"
> <asp:listitem value="M"
> <asp:listitem value="P"
> </asp:dropdownlist>
>
> I was setting the value before (when the value was "0", "1" and "2").
> It used to work like this:
> recurrenceType.SelectedIndex = objDataReader("recurrenceType")
> How would I set it with the value set to a char?
> Thanks,
> Tom.
>
> Posted Via mcse.ms Premium Usenet Newsgroup Services
> ----
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----
> http://www.mcse.ms
>
I just tried this:
recurrenceType.SelectedItem.value = "P"
The display on the screen is still "None".
Thanks,
Tom.
Posted Via mcse.ms Premium Usenet Newsgroup Services
----
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----
http://www.mcse.ms
I have tried all kinds of ways to get this to work and keep getting
strange (at least to me) results.
If I do a viewsource of the above statement (my last post):
recurrenceType.SelectedItem.value = "P"
I get the following:
<select name="recurrenceType" id="recurrenceType"
style="border:none">
<option selected="selected" value="P">None</option>
<option value="M">Monthly</option>
<option value="P">Periodic</option>
</select>
It seems to change the value, which I guess is what I am saying - but
this is how I saw it selected in one example. I am trying to get it
to move the selected="selected" to the 3rd option and it isn't doing
it.
I then tried:
recurrenceType.Items.FindByValue("P").selected = true
And got the following error:
A DropDownList cannot have multiple items selected.
Description: An unhandled exception occurred during the execution
of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: A DropDownList
cannot have multiple items selected.
Source Error:
Line 132: </table>
Line 133:
Line 134: <form runat="server">
Line 135: <table align="center">
Line 136: <tr
valign="baseline">
This is driving me crazy.
Thanks,
Tom.
Posted Via mcse.ms Premium Usenet Newsgroup Services
----
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----
http://www.mcse.ms
The syntax should be something close to this:
recurrenceType.SelectedItem=recurrenceType.Items.FindByValue("P")
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"tfs" <tfs@.dslextreme-dot-com.no-spam.invalid> wrote in message
news:40eb0582$1_1@.mcse.ms...
> I have a dropdownlist that I was setting to an index (which happened
> to be the same as the value). I changed this to a character as that
> is what I want to have in my database.
> <asp:dropdownlist
> style="border:none" id="recurrenceType"
> runat="server">
> <asp:listitem value="N"
> <asp:listitem value="M"
> <asp:listitem value="P"
> </asp:dropdownlist>
>
> I was setting the value before (when the value was "0", "1" and "2").
> It used to work like this:
> recurrenceType.SelectedIndex = objDataReader("recurrenceType")
> How would I set it with the value set to a char?
> Thanks,
> Tom.
>
> Posted Via mcse.ms Premium Usenet Newsgroup Services
> ----
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----
> http://www.mcse.ms
I think what you are looking for is -
((ListItem)recurrenceType.Items.FindByValue("P")).Selected = true;
Bobby Ryzhy
bobby@.w
[url]http://www.w
On Tue, 6 Jul 2004 14:43:16 -0700, "Steve C. Orr [MVP, MCSD]"
<Steve@.Orr.net> wrote:
>The syntax should be something close to this:
>recurrenceType.SelectedItem=recurrenceType.Items.FindByValue("P")
Yes, I believe that is more precise.
"Bobby Ryzhy" <bryzhy@.hotmail.com> wrote in message
news:vt8me0l7uanmgb9iln5o7nsvbal012e867@.
4ax.com...
> I think what you are looking for is -
> ((ListItem)recurrenceType.Items.FindByValue("P")).Selected = true;
> Bobby Ryzhy
> bobby@.w
> [url]http://www.w
>
> On Tue, 6 Jul 2004 14:43:16 -0700, "Steve C. Orr [MVP, MCSD]"
> <Steve@.Orr.net> wrote:
>
>
SelectValue was it.
Here is what I did that works fine.
recurrenceType.SelectedValue =
objDataReader("recurrenceType")
recurrenceDisplay.text = recurrenceType.SelectedItem.text
<td nowrap>Recurrance:</td>
<td>
<asp:label id="recurrenceDisplay"
runat="server" />
<asp:dropdownlist style="visibility:hidden"
id="recurrenceType" runat="server">
<asp:listitem value="N"
>None</asp:listitem>
<asp:listitem value="M"
>Monthly</asp:listitem>
<asp:listitem value="P"
>Periodic</asp:listitem>
</asp:dropdownlist>
</td>
Thanks,
Tom
Posted Via mcse.ms Premium Usenet Newsgroup Services
----
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----
http://www.mcse.ms
Set dropdownlist value
to be the same as the value). I changed this to a character as that
is what I want to have in my database.
<asp:dropdownlist
style="border:none" id="recurrenceType"
runat="server">
<asp:listitem value="N"
>None</asp:listitem>
<asp:listitem value="M"
>Monthly</asp:listitem>
<asp:listitem value="P"
>Periodic</asp:listitem>
</asp:dropdownlist
I was setting the value before (when the value was "0", "1" and "2").
It used to work like this:
recurrenceType.SelectedIndex = objDataReader("recurrenceType")
How would I set it with the value set to a char?
Thanks,
Tom.
Posted Via Usenet.com Premium Usenet Newsgroup Services
------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
------------------
http://www.usenet.comI have tried all kinds of ways to get this to work and keep getting
strange (at least to me) results.
If I do a viewsource of the above statement (my last post):
recurrenceType.SelectedItem.value = "P"
I get the following:
<select name="recurrenceType" id="recurrenceType"
style="border:none">
<option selected="selected" value="P">None</option>
<option value="M">Monthly</option>
<option value="P">Periodic</option>
</select
It seems to change the value, which I guess is what I am saying - but
this is how I saw it selected in one example. I am trying to get it
to move the selected="selected" to the 3rd option and it isn't doing
it.
I then tried:
recurrenceType.Items.FindByValue("P").selected = true
And got the following error:
A DropDownList cannot have multiple items selected.
Description: An unhandled exception occurred during the execution
of the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: A DropDownList
cannot have multiple items selected.
Source Error:
Line 132: </table>
Line 133:
Line 134: <form runat="server">
Line 135: <table align="center">
Line 136: <tr
valign="baseline"
This is driving me crazy.
Thanks,
Tom.
Posted Via Usenet.com Premium Usenet Newsgroup Services
------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
------------------
http://www.usenet.com
I just tried this:
recurrenceType.SelectedItem.value = "P"
The display on the screen is still "None".
Thanks,
Tom.
Posted Via Usenet.com Premium Usenet Newsgroup Services
------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
------------------
http://www.usenet.com
The syntax should be something close to this:
recurrenceType.SelectedItem=recurrenceType.Items.F indByValue("P")
--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"tfs" <tfs@.dslextreme-dot-com.no-spam.invalid> wrote in message
news:40eb0582$1_1@.Usenet.com...
> I have a dropdownlist that I was setting to an index (which happened
> to be the same as the value). I changed this to a character as that
> is what I want to have in my database.
> <asp:dropdownlist
> style="border:none" id="recurrenceType"
> runat="server">
> <asp:listitem value="N"
> >None</asp:listitem>
> <asp:listitem value="M"
> >Monthly</asp:listitem>
> <asp:listitem value="P"
> >Periodic</asp:listitem>
> </asp:dropdownlist>
>
> I was setting the value before (when the value was "0", "1" and "2").
> It used to work like this:
> recurrenceType.SelectedIndex = objDataReader("recurrenceType")
> How would I set it with the value set to a char?
> Thanks,
> Tom.
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ------------------
> http://www.usenet.com
I think what you are looking for is -
((ListItem)recurrenceType.Items.FindByValue("P")).Selected = true;
Bobby Ryzhy
bobby@.weekendtech.net
http://www.weekendtech.net
On Tue, 6 Jul 2004 14:43:16 -0700, "Steve C. Orr [MVP, MCSD]"
<Steve@.Orr.net> wrote:
>The syntax should be something close to this:
>recurrenceType.SelectedItem=recurrenceType.Items.F indByValue("P")
Yes, I believe that is more precise.
"Bobby Ryzhy" <bryzhy@.hotmail.com> wrote in message
news:vt8me0l7uanmgb9iln5o7nsvbal012e867@.4ax.com...
> I think what you are looking for is -
> ((ListItem)recurrenceType.Items.FindByValue("P")).Selected = true;
> Bobby Ryzhy
> bobby@.weekendtech.net
> http://www.weekendtech.net
>
> On Tue, 6 Jul 2004 14:43:16 -0700, "Steve C. Orr [MVP, MCSD]"
> <Steve@.Orr.net> wrote:
> >The syntax should be something close to this:
> >recurrenceType.SelectedItem=recurrenceType.Items.F indByValue("P")
SelectValue was it.
Here is what I did that works fine.
recurrenceType.SelectedValue =
objDataReader("recurrenceType")
recurrenceDisplay.text = recurrenceType.SelectedItem.text
<td nowrap align="right">Recurrance:</td>
<td>
<asp:label id="recurrenceDisplay"
runat="server" />
<asp:dropdownlist style="visibility:hidden"
id="recurrenceType" runat="server">
<asp:listitem value="N"
>None</asp:listitem>
<asp:listitem value="M"
>Monthly</asp:listitem>
<asp:listitem value="P"
>Periodic</asp:listitem>
</asp:dropdownlist>
</td
Thanks,
Tom
Posted Via Usenet.com Premium Usenet Newsgroup Services
------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
------------------
http://www.usenet.com
set dropdownlist selectedindex in datagrid
I have a datagrid with a dropdownlist and would like to have the
dropdownlist display a database value correctly while the grid is in
edit mode.
I have a templatecolumn as follows:
<asp:TemplateColumn HeaderText="New Route">
<HeaderStyle Width="0.5in"></HeaderStyle>
<ItemTemplate>
<asp:DropDownList id="DropDownList3" runat="server"
AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
with an edit column as:
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit" CancelText="Cancel" EditText="Edit"></
asp:EditCommandColumn>
I would like to set the dropdownlist to the value found in the
database. my code behind uses ItemDataBound as follows:
public void myDataGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dd;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
}
}
upon loading the dropdownlist has the values as in the database.
However, when I press edit the selected index becomes 0. I cannot
figure how to set the value correctly.
my edit code looks like:
public void myDataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DropDownList dd;
dd = (DropDownList)e.Item.Cells[7].FindControl("DropDownList3");
dd.SelectedIndex =
dd.Items.IndexOf(dd.Items.FindByText(e.Item.Cells[3].Text));
<-- dd.SelectedIndex looks fine here -->
...
}
dd.SelectedIndex looks ok in debug mode, but something changes it
before the dropdownlist is displayed. the values in the dropdownlists
not being edited are ok, it's only the one being edited that shows the
wrong selectedindex.
any help would be appreciated. thanks.pleaseexplaintome@.yahoo.com wrote:
> Hi all,
> I have a datagrid with a dropdownlist and would like to have the
> dropdownlist display a database value correctly while the grid is in
> edit mode.
> I have a templatecolumn as follows:
> <asp:TemplateColumn HeaderText="New Route">
> <HeaderStyle Width="0.5in"></HeaderStyle>
> <ItemTemplate>
> <asp:DropDownList id="DropDownList3" runat="server"
> AutoPostBack="True" OnSelectedIndexChanged="ddlselect">
> <asp:ListItem Value="A">A</asp:ListItem>
> <asp:ListItem Value="B">B</asp:ListItem>
> <asp:ListItem Value="C">C</asp:ListItem>
> <asp:ListItem Value="D">D</asp:ListItem>
> </asp:DropDownList>
> </ItemTemplate>
There is a much easier way (at least in ASP.NET 2.0)
Set the property SelectedValue on the DrowDownlist
declaratively like this:
SelectedValue='<%# Bind("myDBField") %>'
This will take care of everything: showing the data and
updating the data after postback.
Riki
Saturday, March 24, 2012
Set Focus to control after postback
autopostback event of a dropdownlist. When the user changes the selection of
my dropdownlist an onchange event occurs and the form does autopostback.
After the on change event I need to set focus to the next textbox. How do I
do this>On Oct 29, 1:08 pm, Victorious1
<Victorio...@.discussions.microsoft.comwrote:
Quote:
Originally Posted by
I am usingVB with .net 1.1 and I wish to set focus to a textbox after the
autopostback event of a dropdownlist. When the user changes the selection of
my dropdownlist an onchange event occurs and the form does autopostback.
After the on change event I need to set focus to the next textbox. How do I
do this>
This is C# syntax but the idea is the same for VB.
In the page load method have this:
Page.ClientScript.RegisterClientScriptBlock(this.G etType(),
"SomeName", <WebControlName>.focus());
Tuesday, March 13, 2012
set selected value declaratively to dropdownlist
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
</EditItemTemplate>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any promptsHi,
You can set the selectedvalue property on the dropdownlist as follows
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddl"
DataSourceID="SqlDataSource2"
DataValueField="ProductID"
DataTextField="ProductName" SelectedValue='<%# Bind("ProductID")
%>'></asp:DropDownList>
</EditItemTemplate>
Regards,
Mohamed Mosalem
"podi" wrote:
> Hi,
> I just wonder if there is a way to set a selected item in dropdown list
> which items are retreived from database without touching c# code ?
> I mean, i have in gridview, in edit mode one dropdownlist which
> selected value should be the same as a label text from normal view mode
> <EditItemTemplate>
> <asp:DropDownList ID="ddlGrdvFrequency" runat="server"
> DataTextField="Frequency"
> DataValueField="FrequencyId"
> DataSourceID="SqlDataSourceMembershipFrequency" />
> </EditItemTemplate>
> I was looking for sth like SelectedIndex, but intellisense did not give
> me any prompt for it. I use now C# code in databound event for gridview
> to set this item selected, but ...is there a way of not using this ?
> thanks for any prompts
>
hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.
i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
"podi" <lukasz.podolak@.gmail.com> wrote in message
news:1159037422.462578.221520@.d34g2000cwd.googlegroups.com...
> Hi,
> I just wonder if there is a way to set a selected item in dropdown list
> which items are retreived from database without touching c# code ?
> I mean, i have in gridview, in edit mode one dropdownlist which
> selected value should be the same as a label text from normal view mode
> <EditItemTemplate>
> <asp:DropDownList ID="ddlGrdvFrequency" runat="server"
> DataTextField="Frequency"
> DataValueField="FrequencyId"
> DataSourceID="SqlDataSourceMembershipFrequency" />
> </EditItemTemplate>
> I was looking for sth like SelectedIndex, but intellisense did not give
> me any prompt for it. I use now C# code in databound event for gridview
> to set this item selected, but ...is there a way of not using this ?
> thanks for any prompts
>
Thank you for responses, it helped
Tim_Mac wrote:
> hi podi,
> unfortunately the SelectedIndex cannot be set declaratively, it is a compi
le
> error. i'm just wondering how you could know in advance with a databound
> list which one to select?
> an alternative would be to sort the datasource so that the one you want to
> select is the first record, then it will be selected by default when the
> list is databound.
> i think the databound event is the correct (and best) place to achieve you
r
> task.
> i hope this helps
> tim
>
> "podi" <lukasz.podolak@.gmail.com> wrote in message
> news:1159037422.462578.221520@.d34g2000cwd.googlegroups.com...
set selected value declaratively to dropdownlist
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
</EditItemTemplate>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any promptsHi,
You can set the selectedvalue property on the dropdownlist as follows
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddl"
DataSourceID="SqlDataSource2"
DataValueField="ProductID"
DataTextField="ProductName" SelectedValue='<%# Bind("ProductID")
%>'></asp:DropDownList>
</EditItemTemplate>
Regards,
Mohamed Mosalem
"podi" wrote:
Quote:
Originally Posted by
Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
>
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
>
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
>
</EditItemTemplate>
>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts
>
>
hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.
i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
"podi" <lukasz.podolak@.gmail.comwrote in message
news:1159037422.462578.221520@.d34g2000cwd.googlegr oups.com...
Quote:
Originally Posted by
Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
>
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
>
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
>
</EditItemTemplate>
>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts
>
Thank you for responses, it helped
Tim_Mac wrote:
Quote:
Originally Posted by
hi podi,
unfortunately the SelectedIndex cannot be set declaratively, it is a compile
error. i'm just wondering how you could know in advance with a databound
list which one to select?
an alternative would be to sort the datasource so that the one you want to
select is the first record, then it will be selected by default when the
list is databound.
>
i think the databound event is the correct (and best) place to achieve your
task.
i hope this helps
tim
>
>
>
"podi" <lukasz.podolak@.gmail.comwrote in message
news:1159037422.462578.221520@.d34g2000cwd.googlegr oups.com...
Quote:
Originally Posted by
Hi,
I just wonder if there is a way to set a selected item in dropdown list
which items are retreived from database without touching c# code ?
I mean, i have in gridview, in edit mode one dropdownlist which
selected value should be the same as a label text from normal view mode
<EditItemTemplate>
<asp:DropDownList ID="ddlGrdvFrequency" runat="server"
DataTextField="Frequency"
DataValueField="FrequencyId"
DataSourceID="SqlDataSourceMembershipFrequency" />
</EditItemTemplate>
I was looking for sth like SelectedIndex, but intellisense did not give
me any prompt for it. I use now C# code in databound event for gridview
to set this item selected, but ...is there a way of not using this ?
thanks for any prompts