Thursday, March 29, 2012
Set DataList SelectedIndex to data record
record in the database. What is the easiest way to accomplish this??
Thanks,
David D. McCroryI know this is a simple request, but I could sure use some
help.....thanks...
"David D. McCrory" <dmccrory@.alltell.net> wrote in message
news:eylWxCV3DHA.1924@.TK2MSFTNGP10.phx.gbl...
> I want to set the SelectedIndex of a DataList control to equal a specific
> record in the database. What is the easiest way to accomplish this??
>
> Thanks,
> David D. McCrory
I'm not sure this is possible. What are you trying to accomplish? There
exists no dependency on a control and its durable datasource - at least not
yet. ASP 2.0 promises to change this.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"David D. McCrory" <dmccrory@.alltell.net> wrote in message
news:eylWxCV3DHA.1924@.TK2MSFTNGP10.phx.gbl...
> I want to set the SelectedIndex of a DataList control to equal a specific
> record in the database. What is the easiest way to accomplish this??
>
> Thanks,
> David D. McCrory
Set DataList SelectedIndex - 2nd Post
This is a fairly simple request...I am looking for some help...
I want to set the SelectedIndex of a DataList control to equal a specific
record in the database. What is the easiest way to accomplish this??
Thanks,
David D. McCroryDavid D. McCrory wrote:
> This is the second post for this question...please help!!
> This is a fairly simple request...I am looking for some help...
> I want to set the SelectedIndex of a DataList control to equal a specific
> record in the database. What is the easiest way to accomplish this??
>
> Thanks,
> David D. McCrory
I would add a handler for the ItemDataBound event of the DataList and
then the event args give you access to the current item and its data.
e.Item.DataItem gives the current item in the datasource; use this to
check for a certain value
e.Item.ItemIndex gives the current item's index
I believe you can set the selectedindex in this event; if not, set a
local/member var to the value and then after the DataBind, set it.
The other solution is to loop the items after the DataBind instead of
using the event, but doing the first way does one less loop.
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Thanks Craig...I will give it a try.....
"Craig Deelsnyder" <cdeelsny@.NO_SPAM_4_MEyahoo.com> wrote in message
news:OO0DxWv3DHA.2528@.tk2msftngp13.phx.gbl...
> David D. McCrory wrote:
> > This is the second post for this question...please help!!
> > This is a fairly simple request...I am looking for some help...
> > I want to set the SelectedIndex of a DataList control to equal a
specific
> > record in the database. What is the easiest way to accomplish this??
> > Thanks,
> > David D. McCrory
> I would add a handler for the ItemDataBound event of the DataList and
> then the event args give you access to the current item and its data.
> e.Item.DataItem gives the current item in the datasource; use this to
> check for a certain value
> e.Item.ItemIndex gives the current item's index
> I believe you can set the selectedindex in this event; if not, set a
> local/member var to the value and then after the DataBind, set it.
> The other solution is to loop the items after the DataBind instead of
> using the event, but doing the first way does one less loop.
> --
> Craig Deelsnyder
> Microsoft MVP - ASP/ASP.NET
Monday, March 26, 2012
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 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